4

im designing a collection of video games for the command line (such as deal or no deal, tic tac toe, racing, maze puzzle, connect four, wack a mole, etc.) however it would really make things easier for me if i could make it so that when the user makes a selection (such as what direction to move in the game) , that as soon as they press the arrow keys then it carries out the IF statements that follow. Instead of having to press enter after each selection. something like...

:one1
set /p direction1= :
IF %direction1%== {ARROW KEY LEFT} goto two2
IF %direction1%== {ARROW KEY RIGHT} goto three3
IF %direction1%== {ARROW KEY UP} goto four4
IF %direction1%== {ARROW KEY DOWN} goto five5
goto one1

Any Ideas?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
daniel11
  • 2,027
  • 10
  • 38
  • 46
  • WTF has batch to do with FP? It barely has subroutines. Also, you don't want to write a complex program in such a language - write it in a language that has e.g. loops. –  Dec 03 '10 at 14:57
  • 1
    FP means what exactly? and the reason i am using batch for this is because not alot of people have tried making a primitive gui in a batch file before so i thought id try it out :P – daniel11 Dec 03 '10 at 18:46
  • FP means functional programming, which you tagged this as. I removed the tag because delnan is right: this question has nothing to do with functional programming. – sepp2k Dec 03 '10 at 20:51
  • Does the choice command work with Win7? – basic_man Mar 27 '18 at 19:46

3 Answers3

5

It depends of your windows version.

If you use Vista, you can use choice to receive single keys directly, but with pure batch it seems not to be possible to detect the arrow keys.

EDIT

:one1
choice /c awsd /n /m "MOVE with A S D w"
IF %errorlevel%==1 goto two2
IF %errorlevel%==2 goto three3
IF %errorlevel%==3 goto four4
IF %errorlevel%==4 goto five5
goto one1
jeb
  • 78,592
  • 17
  • 171
  • 225
  • hmmm, ive seen some people do it but i didnt understand how to use their script with mine without getting errors. – daniel11 Dec 03 '10 at 00:35
2
:start

choice /c:HPKM /n "Move with arrow keys"

if "%errorlevel%"=="1" {COMMAND}

if "%errorlevel%"=="2" {COMMAND}

if "%errorlevel%"=="3" {COMMAND}

if "%errorlevel%"=="4" {COMMAND}
Sirko
  • 72,589
  • 19
  • 149
  • 183
12345
  • 96
  • 1
  • 7
0

http://winsupport.org/packages/choice.exe

That is a custom choice that allows for arrow keys to be used.

they are used like so: choice /c:HPKM /n "Move with arrow keys"

The only problem being that you can also use H, P, K and M...