I want to make a game in MS-DOS and I'm stuck with a problem:
How to ask the user for input without stopping the console?
I've considered stopping it for a few milliseconds and giving the illusion that the console didn't stop:
@echo off
:inicio
choice /c awds0 /t 0.30 /d 0
if %errorlevel%==1 echo izquierda.
goto inicio
pause
exit
But it gives me an error:
Only a number from -1 to 999 is allowed
I have also tried to place:
choice /c awds0 /t 0 /d 0
But the key is not detected even if I put it in a bucle
goto:
@echo off
:inicio
choice /c awds0 /t 0 /d 0
if not %errorlevel%==5 goto end
goto inicio
:end
echo se terminó el programa
pause
exit
input: a....
output: Infinity loop
The other option I think is to capture the click event of the window, but I don't know how and I can't find how to do it.
Note: I use windows 7 32bits. The console that I use is the one that comes by default with the operating system.
External links:
- Answer from: Read key in batch? : This answer is pretty close to what I'm looking for, but running it freezes the console until I type the key.
Surprise in 2021 a game in msdos command language . You give few clues about the version of msdos (or mswindows ) and whether it is 32 or 64 bit .
So that the command
CHOICE
can capture something from the keyboard, and not immediately return the default value, it waits an integer number of seconds: between 1 and 999. If something is typed before the time expires, it captures it and evaluates it as you already know. The minimum is one second (with 0 it would immediately return a default value and not anything we type).But since you indicate that YES you can add your own command. This C++ code, once compiled, does what you need:
I compile it for my tests with the name PatxiRBGetKey.exe
The program when it is executed does NOT stop. If nothing has been typed, it returns a 0. If something has been typed, it consumes a character and returns the ASCII code that corresponds to it (nothing will be displayed on the screen).
You can see the return value with
For example, if uppercase F was typed errorlevel would have a 70 and if lowercase f had been typed errorlevel would have a 102 and if nothing had been typed errorlevel would have a 0
Putting it all together and slightly modifying the #Sal BAT
Cheer up and good luck.
Regarding How to ask the user for input without stopping the console?, I see that you use choice but you get the error
I suggest you use:
For example, this is how you would wait for a key to continue the program:
Now with regard to knowing which key the user pressed.
editing...
In ms-dos input handling is extremely limited, the furthest you'd get would be something like this: