How can I return a single character from the console in a batch script without pressing enter?

前端 未结 1 1771
悲哀的现实
悲哀的现实 2021-01-20 04:33

I\'d like to read/return a single character from a batch script without having to hit the enter key, like getChar() in C/C++. How would I do this?

相关标签:
1条回答
  • 2021-01-20 05:29

    Use the CHOICE command

    Example borrowed from this site

       @echo off
       :menu
       cls
       echo.
       echo       A - Text for item A
       echo       B - Text for item B
       echo       C - End
       echo.
       choice /c:ABC > nul
       if errorlevel 3 goto end
       if errorlevel 2 goto B
       if errorlevel 1 goto A
       echo Error... choice not installed
       goto end
       :A
       echo Commands for item A
       pause
       goto menu
       :B
       echo Commands for item B
       pause
       goto menu
       :end
    
    0 讨论(0)
提交回复
热议问题