我可以使用Cmd中的计时器创建'set / p“”='命令吗?(Can I create a 'set /p “”=' command with a timer

编程入门 行业动态 更新时间:2024-10-07 05:31:05
我可以使用Cmd中的计时器创建'set / p“”='命令吗?(Can I create a 'set /p “”=' command with a timer in Cmd?)

我想为'set / p“”='命令创建一个计时器,这样如果您不在所需的时间空间中输入某些内容,它会移动到不同的标签。

例如。

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#等另一种语言混合?

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.

Eg.

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

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脚本并运行它,并查看它是否提供了您想到的用户体验。

@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

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

                    
                     
          

更多推荐

本文发布于:2023-08-07 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   可以使用   命令   Cmd   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!