1

我最近刚刚编辑了一个我看到的脚本,使其更加动态和小。我似乎发现了一个我无法弄清楚的问题。对于输入,它有一个仅允许 AZ + 0-9 的选择脚本,所以我想知道,允许输入哪些键,例如键“ENTER”或“SHIFT”或“SPACE”是否有代码或“BACKSPACE”,因为我需要所有这些,所以我可以整合它们。

    @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

如果您有任何想法或建议使其工作以允许任何长度,请告诉。这似乎是最好的主意。

4

1 回答 1

4

选择非常有限,因此您需要另一种解决方案。

一种方法是使用自己的功能,xcopy用于键输入。

批处理 - 用户输入的颜色

: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

xcopy /L /W 尝试将批处理文件复制到自身。/W 是重要的选项,它需要在复制过程开始之前按下一个键,并显示该键。
复制本身总是失败,因为文件无法复制到自身。
因此错误流被重定向到 NUL 2>NUL

FOR /F 使用击键捕获整行。并且set "key=%key:~-1%"只选择该行的最后一个字符。

好消息是 xcopy 接受并显示几乎所有键(F1 .. F12 和其他一些仍然失败),甚至可以通过这种方式检测 Backspace 和 Enter。

于 2013-09-22T10:25:34.013 回答