在批处理文件中实现定时输入。 (倒计时一分钟)

Implementing timed input in batch file. (countdown to a minute)

我正在使用 windows 8 编写批处理文件,但在批处理文件中实现计时器时遇到了困难。我想征求用户的意见,并给他们一分钟的时间来输入他们的意见。一旦时间到达一分钟,然后显示 'the time is over' 之类的消息。因此,时间将从 1 秒开始到 60 秒结束,或者从 60 秒开始到 0 秒。要么工作得很好。 此外,我希望计时器显示在屏幕上的某个位置,以便他们可以看到倒计时。此外,当计时器为 运行 时,我希望用户能够键入一个单词并按回车键。该程序不会让用户等待,但它会等到时间结束或用户输入单词(以先到者为准)。在他们输入一个有效的单词后,我想将该单词存储在某个变量中并执行类似的操作(转到 VALIDWORD 或 echo That is a valid word!) 我不知道这在批处理文件中是否可行,并且有更高级的语言可以使用,但我想使用批处理脚本来完成这个程序。谢谢你。 以下是我的概念:

@echo off
:Start
color EC
set /a time =60

:loop
cls
if %time% EQU 0 goto Timesup
if %time% LEQ 60 goto CONTINUE

:CONTINUE
ping localhost -n 2 >nul
set /a time-=1
set /p cho=     Enter your word: 
echo Remaing Time: %time%
goto loop

:Timesup
echo The time is over!
exit

如有任何想法或支持,我们将不胜感激。再次感谢!

批处理文件不是为此类任务设计的,但可以执行某些高级管理,尽管方式有限。下面的子程序使用choice命令获取输入键,因此不允许使用Enter键结束输入,也不允许使用BackSpace删除字符;它使用 01 键来完成此类任务。

@echo off
setlocal

call :ReadTimedInput 60 word=
echo Word read: "%word%"
goto :EOF


:ReadTimedInput seconds result=
setlocal EnableDelayedExpansion

set /A seconds=100+%1
set "letters=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"

echo Enter a word; press 0 to End, press 1 to Clear
:clear
set /P "=X!CR!                                     !CR!" < NUL
set "result="
:loop
   set /P "=X!BS! !CR!%seconds:~-2%: %result%" < NUL
   choice /C ¡10%letters% /N /CS /T 1 /D ¡ > NUL
   if %errorlevel% equ 1 goto tick
   if %errorlevel% equ 2 goto clear
   if %errorlevel% equ 3 echo/& goto endInput
   set /A char=%errorlevel%-4
   set "result=%result%!letters:~%char%,1!"
   :tick
   set /A seconds-=1
if %seconds% gtr 100 goto loop
echo !CR!00
set "result=Time out"
:endInput
endlocal & set "%2=%result%"
exit /B