我可以在 Cmd 中创建一个带有计时器的 'set/p ""=' 命令吗? [英] Can I create a 'set /p ""=' command with a timer in Cmd?

查看:24
本文介绍了我可以在 Cmd 中创建一个带有计时器的 'set/p ""=' 命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 'set/p ""=' 命令创建一个计时器,这样如果您没有在所需的时间空间内输入内容,它就会移动到不同的标签.

I want to create a timer for the 'set /p ""=' command so that if you don't input something in the required time space it moves to a different label.

例如

echo You have 3 seconds to type 'go'
(??if not typed in 3 seconds goto fail??)
set /p input=
if %input%==go goto win
goto fail

是否可以命令程序在set/p"之前设置一个 3 秒的计时器?不将其与 C# 等其他语言混合使用?

Is it possible to command the program to set a 3 second timer before the 'set /p'? Without mixing it with another language like C# etc?

推荐答案

Squashman 的建议也是我想到的最好的建议.保存以下 .bat 脚本并运行它,看看它是否提供了您想要的用户体验.

Squashman's suggestion is the best one that comes to mind for me as well. Save the following .bat script and run it, and see whether it offers the user experience you had in mind.

@echo off
setlocal

set /P "=You have 3 seconds to type 'go': "<NUL

rem // capture an extended character as a default option for "choice"
for /f %%I in ('forfiles /p "." /m "%~nx0" /c "cmd /c echo 0x99"') do set "char=%%~I"

rem // for each letter in "go"...
for %%I in (g o) do (
    choice /c %%I%char% /t 3 /d %char% >NUL

    rem // Extended character probably isn't on the keyboard.
    rem // If the result was the extended char, it was timed out.
    if errorlevel 2 goto fail

    rem // visual response of user input w/o new line
    set /P "=%%I"<NUL
)

echo;
echo You did it!
pause & goto :EOF

:fail
echo;
echo Too slow.
pause

这篇关于我可以在 Cmd 中创建一个带有计时器的 'set/p ""=' 命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆