Question

I have recently just worked on editing a script i saw to make it more dynamic and small. I seem to have found a problem which i can't figure out. For input it has a Choice script which allows only A-Z + 0-9, so i was wondering, What are the keys allowed to be entered, like is there a code for the key "ENTER" or "SHIFT" or "SPACE" or "BACKSPACE" because i need all of those so i can integrate them.

    @echo off
    color 05
    mode con cols=30 lines=8
    echo.
    echo Welcome.
    pause >nul
    cls
  :startover
    set variable=
    set counter=-1
  :password
**choice /c:abcdefghijklmnopqrstuvwxyz0123456789 /n /m "Type password please:%variable%"
    echo.**
    echo %ERRORLEVEL%

call :variable

if errorlevel 255 goto Error
if errorlevel 36 set letter=%letter%9&goto PWcheck
if errorlevel 35 set letter=%letter%8&goto PWcheck
if errorlevel 34 set letter=%letter%7&goto PWcheck
if errorlevel 33 set letter=%letter%6&goto PWcheck
if errorlevel 32 set letter=%letter%5&goto PWcheck
if errorlevel 31 set letter=%letter%4&goto PWcheck
if errorlevel 30 set letter=%letter%3&goto PWcheck
if errorlevel 29 set letter=%letter%2&goto PWcheck
if errorlevel 28 set letter=%letter%1&goto PWcheck
if errorlevel 27 set letter=%letter%0&goto PWcheck
if errorlevel 26 set letter=%letter%z&goto PWcheck
if errorlevel 25 set letter=%letter%y&goto PWcheck
if errorlevel 24 set letter=%letter%x&goto PWcheck
if errorlevel 23 set letter=%letter%w&goto PWcheck
if errorlevel 22 set letter=%letter%v&goto PWcheck
if errorlevel 21 set letter=%letter%u&goto PWcheck
if errorlevel 20 set letter=%letter%t&goto PWcheck
if errorlevel 19 set letter=%letter%s&goto PWcheck
if errorlevel 18 set letter=%letter%r&goto PWcheck
if errorlevel 17 set letter=%letter%q&goto PWcheck
if errorlevel 16 set letter=%letter%p&goto PWcheck
if errorlevel 15 set letter=%letter%o&goto PWcheck
if errorlevel 14 set letter=%letter%n&goto PWcheck
if errorlevel 13 set letter=%letter%m&goto PWcheck
if errorlevel 12 set letter=%letter%l&goto PWcheck
if errorlevel 11 set letter=%letter%k&goto PWcheck
if errorlevel 10 set letter=%letter%j&goto PWcheck
if errorlevel 9 set letter=%letter%i&goto PWcheck
if errorlevel 8 set letter=%letter%h&goto PWcheck
if errorlevel 7 set letter=%letter%g&goto PWcheck
if errorlevel 6 set letter=%letter%f&goto PWcheck
if errorlevel 5 set letter=%letter%e&goto PWcheck
if errorlevel 4 set letter=%letter%d&goto PWcheck
if errorlevel 3 set letter=%letter%c&goto PWcheck
if errorlevel 2 set letter=%letter%b&goto PWcheck
if errorlevel 1 set letter=%letter%a&goto PWcheck
goto Error
:pwcheck
echo.
echo Your letters are %letter%
if %counter%==10 goto finish
goto password
:variable
set /a counter=%counter%+1
if %counter%==0 set variable=
if %counter%==10 goto finish
set variable=%variable%*
goto :eof
cls
:finish
echo Your password is %letter%
pause>nul
cls
choice /t 10 /c yn /d y /m "Start over "
if errorlevel 2 cls&echo CLOSING&exit
if errorlevel 1 goto startover
:Error
  title Test v1.1 *** Error *** %time%
  echo Error: Critical Error.
  pause >nul
goto :eof
exit

If you have any ideas or suggestions to make it work so that any length is allowed, please do tell. This seems to be the best idea.

Was it helpful?

Solution

Choice is very limited, so you need another solution.

One way is to use an own function which uses xcopy for the key input.

batch - color input by user

:GetKey
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
set "key=%key:~-1%"
exit /b

The xcopy /L /W tries to copy the batch-file to itself. The /W is the important option, it requires a key press before the copy process starts, and the key is shown.
The copy itself fails always, as the file can't copy to itself.
Therefor the error stream is redirected to NUL with 2>NUL.

The FOR /F captures the complete line with the keystroke. And the set "key=%key:~-1%" picks only the last character from the line.

The good thing is that xcopy accepts and shows nearly all keys (F1 .. F12 and some other still fails), even Backspace and Enter can be detected this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top