如果不按Enter键,这段代码如何捕获用户输入?

时间:2014-10-28 23:59:51

标签: file batch-file choice

在游戏Snake中,用户输入选项,游戏将您带到该选项,而无需按Enter键。你是怎么做到的?

但请记住我是初学者,所以请向我解释一下你认为很难的事情。这个游戏可以在http://www.dostips.com/forum/viewtopic.php?f=3&t=4741

找到

1 个答案:

答案 0 :(得分:2)

dbenham制作的蛇码非常复杂,但由于你的问题仅限于输入机制,因此不存在问题。

这个蛇游戏使用choice命令,该命令只需要按下指定的键,而不按下按键。根据用户按下的键,变量errorlevel将更改为选项列表中该字符的数字位置。

在帮助屏幕中输入choice /?。这是一段摘录:

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Description:
    This tool allows users to select one item from a list
    of choices and returns the index of the selected choice.

Parameter List:
   /C    choices       Specifies the list of choices to be created.
                       Default list is "YN".

   /N                  Hides the list of choices in the prompt.
                       The message before the prompt is displayed
                       and the choices are still enabled.

   /CS                 Enables case-sensitive choices to be selected.
                       By default, the utility is case-insensitive.

   /T    timeout       The number of seconds to pause before a default
                       choice is made. Acceptable values are from 0 to
                       9999. If 0 is specified, there will be no pause
                       and the default choice is selected.

   /D    choice        Specifies the default choice after nnnn seconds.
                       Character must be in the set of choices specified
                       by /C option and must also specify nnnn with /T.

   /M    text          Specifies the message to be displayed before
                       the prompt. If not specified, the utility
                       displays only a prompt.

   /?                  Displays this help message.

以下是一个例子:

C:\> set errorlevel=-1
C:\> choice /c "YN" /m "Yes or No"
Yes or No: [Y,N]? Y

C:\> Echo %errorlevel%
1

C:\>