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)
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