0

I need a way to detect when the user presses the arrow keys in a windows batch file. I think the easiest way would be to use a command line tool that echoes the decimal value of whatever key has been pressed, and go from there. It would be easier to set the key value directly to a batch variable, but I can manage to do so without (via a FOR loop to set a command output to a variable)

The only other thing I got is using a keylogger, and checking the log file for the arrow keys, but this does not work well, and I don't like (and neither would a client) keyloggers.

here is an example of how I might use it:

for /f "tokens=1,2 delims=[]" %%A in ('foo.exe') do set input=%%B
:: the above justs sets the output of the command "foo.exe" the variable %input% 


if %input%==37 echo you pressed the left arrow key.
if %input%==38 echo you pressed the up arrow key.
if %input%==39 echo you pressed the right arrow key.
if %input%==40 echo you pressed the left arrow key.

so I just need a program where when I type some command from the command prompt, foo.exe, the program waits for the user to press a button, and whatever button is pressed, is recorded and outputed in it's decimal form (Virtual key code, and you can look up a list here.) like this 37 (The key for the left mouse button)

  • Users don't press arrow keys in batch files. Consoles have two modes, high level which is lines of text (arrow keys aren't text) and is how batch files work, and low level. You can do what you want. But you need to write the program. – Noodles Jul 02 '14 at 00:57
  • I know that. I just need a program to give me the virtual key code of whatever key was pressed. –  Jul 02 '14 at 01:03
  • What kind of shell script (batch file) are you writing? – Bill_Stewart Jul 02 '14 at 03:48
  • A really complicated and quite pointless one :) –  Jul 02 '14 at 05:04

2 Answers2

0

I already wrote such a program. I called it GetKey.exe, but it returns the keycode of the key pressed via %errorlevel% value, because it is more efficient this way than via a for /F ... command (that require to execute a copy of cmd.exe each time). It read both normal keys (Ascii characters) and extended keys (like the arrow keys) and differentiate these last ones with a negative errorlevel value. You may download GetKey.exe from this site, look for program # 3.

The program below (SHOWKEYCODES.BAT) display the codes returned by GetKey for all special keys in the keyboard, including Shift-, Ctrl- and Alt- combinations. You may run this program and copy just the specific codes you need.

@echo off
setlocal EnableDelayedExpansion
(for /F "delims==" %%a in ('set') do (
   echo %%a
)) > vars.txt
call :DefineKeyCodes
set a=a
< vars.txt (
for /F "tokens=1* delims==" %%a in ('set') do (
   if "!a!" equ "%%a" (
      set /P a=
   ) else (
      echo %%a=%%b
   )
))
del vars.txt
goto :EOF


:DefineKeyCodes
rem Definition of key codes via key names
rem Antonio Perez Ayala

rem Require Delayed Expansion. Modify "i" variable.
rem Can not use Setlocal because its purpose is to create global variables

for %%a in ("BackSpace=8" "TabKey=9" "Ctrl_Enter=10" "EnterKey=13" "EscKey=27" "Ctrl_@=-3") do (
   set %%a
)
set i=-14
for %%a in (Alt_BackSpace Shift_Tab) do (
   set %%a=!i!
   set /A i-=1
)
rem Currently: i=-16
for %%a in (Q W E R T Y U I O P LeftBracket RightBracket) do (
   set Alt_%%a=!i!
   set /A i-=1
)
set i=-30
for %%a in (A S D F G H J K L Semicolon Apostrophe BackQuote) do (
   set Alt_%%a=!i!
   set /A i-=1
)
set i=-43
for %%a in (BackSlash Z X C V B N M Comma Dot Slash "" GrayStar) do (
   set Alt_%%~a=!i!
   set /A i-=1
)
set i=-59
for %%a in (F1 F2 F3 F4 F5 F6 F7 F8 F9 F10) do (
   set %%a=!i!
   set /A i-=1
)
set i=-71
for %%a in (HomeKey UpArrow PageUp Alt_GrayDash LeftArrow KeyPad5 RightArrow
            Alt_GrayPlus EndKey DownArrow PageDown InsKey DelKey) do (
   set %%a=!i!
   set /A i-=1
)
rem Currently: i=-84
for %%a in (Shift Ctrl Alt) do (
   for %%b in (F1 F2 F3 F4 F5 F6 F7 F8 F9 F10) DO (
      set %%a_%%b=!i!
      set /A i-=1
   )
)
rem Currently: i=-114
for %%a in (PrtSc LeftArrow RightArrow End PageDown Home) do (
   set Ctrl_%%a=!i!
   set /A i-=1
)
rem Currently: i=-120
for %%a in (1 2 3 4 5 6 7 8 9 0 Dash Equal) do (
   set Alt_%%a=!i!
   set /A i-=1
)
rem Currently: i=-132
for %%a in (Ctrl_PageUp F11 F12 Shift_F11 Shift_F12 Ctrl_F11 Ctrl_F12 Alt_F11 Alt_F12) do (
   set %%a=!i!
   set /A i-=1
)
rem Currently: i=-141
for %%a in (UpArrow GrayDash KeyPad5 GrayPlus DownArrow Ins Del Tab GraySlash GrayStar) do (
   set Ctrl_%%a=!i!
   set /A i-=1
)
rem Currently: i=-151
for %%a in (Home UpArrow PageUp "" LeftArrow KeyPad5 RightArrow "" End
            DownArrow PageDown Ins Del GraySlash) do (
   set Alt_%%~a=!i!
   set /A i-=1
)
set Alt_=
set i=

exit /B
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

If all you need is to catch arrow keys (and the Enter key), you can use getch.exe which you can download here.

It returns its value on the %errorlevel% variable, according to the following table:

  • 0 for the UP arrow
  • 1 for the DOWN arrow
  • 2 for the RIGHT arrow
  • 3 for the LEFT arrow
  • 4 for the Enter key
honk
  • 9,137
  • 11
  • 75
  • 83
jgodoy
  • 1
  • 1
  • Nice. I also found another program that does similar, but with all keys. –  Jul 17 '15 at 22:31