Attempt to catch arrow keystrokes
Posted: 12 May 2014 16:15
Wouldn't it be awesome to work with arrow keys in a Batch file?
As we know PAUSE is waiting for user input and immediately returns on any keystroke. Recently I stumble on a side effect. Arrow keys (as well as function keys that are not assotiated with ASCII characters) seem to send two characters to PAUSE where one PAUSE command only processes one character.
As you can see such keys let the loop iterate twice. Not realy surprising to me since I know that behavior from C programming. E.g. the (nonstandard) getch function has to be called twice to get the character read from an arrow key where the first call returns 0.
Having this in mind I tried to let one PAUSE>NUL eat the 0 and preserve the second character read for another command.
What are the choices? I vainly tried SET /P. The XCOPY technique reads funny things out from the process memory ...
... but all it reads is not reasonable to distinguish between the keys that where pressed (and has probably nothing to do if you pressed an arrow key or any other).
Is there anything else we could try or is it just a hopeless task?
Regards
aGerman
As we know PAUSE is waiting for user input and immediately returns on any keystroke. Recently I stumble on a side effect. Arrow keys (as well as function keys that are not assotiated with ASCII characters) seem to send two characters to PAUSE where one PAUSE command only processes one character.
Code: Select all
@echo off &setlocal
set "n=0"
:loop
set /a "n+=1"
pause>nul
echo %n%
goto loop
As you can see such keys let the loop iterate twice. Not realy surprising to me since I know that behavior from C programming. E.g. the (nonstandard) getch function has to be called twice to get the character read from an arrow key where the first call returns 0.
Having this in mind I tried to let one PAUSE>NUL eat the 0 and preserve the second character read for another command.
What are the choices? I vainly tried SET /P. The XCOPY technique reads funny things out from the process memory ...
Code: Select all
@echo off &setlocal
for /f "delims=" %%i in ('pause^>nul^|xcopy /lwq "%~fs0" :\') do echo "%%i"
pause
... but all it reads is not reasonable to distinguish between the keys that where pressed (and has probably nothing to do if you pressed an arrow key or any other).
Is there anything else we could try or is it just a hopeless task?
Regards
aGerman