如何批量捕获ENTER键? [英] How can I trap an ENTER keystroke in batch?
问题描述
该网站和一般脚本的新手.我知道您可以像这样创建用户输入:
New to the site and to scripting in general. I am aware that you can create user inputs like this:
set input=
set /p input=[y/n]: %=%
然后,您可以使用如果"来询问它们是否是某些响应.示例:
then you can ask using "if" to see if they are certain responses. Example:
if "%input%"=="y" goto a:
并通过说来检查是否不是输入内容
and check if it isn't said input by saying
if "%input%" NEQ "y" goto b:
由于我没有受过教育,所以我不知道该怎么做,但是我希望能够确定输入是否为回车键. 我尝试通过使用
Because I'm uneducated, I don't know how to accomplish this, but I want to be able to determine if the input was an enter keystroke. I have tried to accomplish this by using
if "%input%"==[ENTER] goto a:
和
if "%input%"==enter
以及我想出的用于确定按键是否为[ENTER]的所有方法.我可能只是个笨蛋,但是有办法吗?谢谢.
and every method I could think of to determine if the keystroke was an [ENTER]. I am probably just stupid, but is there a way to do this? Thanks.
推荐答案
至少有两种方法可以执行此操作:检查值或检查操作
There are at least two ways to do that: check the value or check the operation
set /p
检索类型化的输入,将其存储在变量中,但是当没有输入时,只需按 Enter 键,就不会对该变量进行任何存储操作,因此它会保持其先前的值.
set /p
retrieves the typed input storing it inside a variable, but when there is no input, just an Enter press, there is no storage operation on the variable so it keeps its previous value.
测试值的通常方法是清除变量内容,使用set /p
并测试变量是否已定义,即它是否具有内容
The usual way to test the value is to clear the variable contents, use the set /p
and test if the variable is defined, that is, it has content
set "var="
set /p "var=[y/n]?"
if not defined var goto :noUserInput
我们还可以测试操作本身.在批处理文件中,大多数命令都设置为某个值,内部errorlevel
变量指示命令执行成功或失败.
We can also test the operation itself. In batch files most of the commands set to some value the internal errorlevel
variable indicating the sucess or failure of the execution of the command.
对于set /p
,如果已检索到数据,则将errorlevel
设置为0,如果没有输入,则将其设置为1.然后if errorlevel
构造可用于确定是否有输入
In the case of the set /p
it will set errorlevel
to 0 if data has been retrieved and will set it to 1 if there is no input. Then the if errorlevel
construct can be used to determine if there was input
set /p "var=[y/n]?"
if errorlevel 1 goto :noUserInput
正如Aacini指出的那样,这可能会失败.如果批处理文件的扩展名为.bat
,则读取数据的set /p
不会重置(设置为0)先前的errorlevel
值,因此必须首先执行一些命令将errorlevel
设置为0./p>
As Aacini points, this can fail. If the batch file has .bat
extension a set /p
that reads data will not reset (set to 0) a previous errorlevel
value, so it is necessary to first execute some command to set the errorlevel
to 0.
ver > nul
set /p "var=[y/n]?"
if errorlevel 1 goto :noUserInput
如果批处理文件以.cmd
扩展名保存,则不需要此文件.在这种情况下,set /p
将在读取数据时将errorlevel
设置为0.
If the batch file is saved with .cmd
extension this is not needed. In this case the set /p
will set the errorlevel
to 0 when it reads data.
还可以使用条件执行运算符(&&
和||
).如果前一个命令成功,则&&
执行下一个命令. ||
如果前一个命令失败,将执行下一个命令.
Also conditional execution operators (&&
and ||
) can be used. &&
executes the next command if the previous one was sucessful. ||
will execute the next command if the previous one failed.
set /p "var=[y/n]?" || goto :noUserInput
这篇关于如何批量捕获ENTER键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!