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

让人想犯罪 __ 提交于 2019-12-20 01:58:12

问题


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:


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


来源:https://stackoverflow.com/questions/13166100/how-can-i-return-a-single-character-from-the-console-in-a-batch-script-without-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!