我如何批量收听击键

时间:2014-09-01 02:41:41

标签: batch-file

我需要我的代码循环子程序,直到按下一个键。我不知道该怎么做。有帮助吗?

5 个答案:

答案 0 :(得分:3)

您可以使用选择命令的超时默认扩展名 EX:

:start
@echo off
cls
:loop1 
cls
echo press 1 for option 1 
echo press 2 for option 2 
echo press 3 for option 3 
echo press any other number key to tell the date
echo.
echo %time%
choice /t 1 /c 123456789q /d q >nul
if %errorlevel% EQU 10 goto loop1
if %errorlevel% EQU 1 goto 1
if %errorlevel% EQU 2 goto 2
if %errorlevel% EQU 3 goto 3
if %errorlevel% GEQ 4 goto end
goto loop1
:1
echo.
echo.
echo option 1 
echo.
pause 
exit 
:2
echo.
echo.
echo option 2
echo.
pause 
exit 
:3
echo.
echo.
echo option 3
echo.
pause 
exit 
:end
echo %date%
pause
Exit

在这种情况下, Choice 命令会每隔1秒自动选择Q作为默认选项,因此如果用户输入Q,它将在循环中跳过1秒。但是,如果他们键入除1,2,3之外的任何数字键,程序将告知日期。如果用户键入1,则程序将执行选项1,同样执行2和3 任何其他的钥匙都不会做任何事情。唯一的缺点是你必须等待最多1秒,你必须为循环保留一个键

答案 1 :(得分:1)

或者你可以输入:

@echo off
color 0a
title KeyStrokeSim
goto Start

:Start
cls
echo Press D or F to continue...
choice /c df /n
if %errorlevel%==1 goto Finished
if %errorlevel%==2 goto Finished

:Finished
echo You have Pressed D or F!
pause
goto Start

答案 2 :(得分:0)

你可以试试这个:

Stroke.bat

@echo off
Echo. > Temp.txt
:: You require choice.exe for this
choice /c 1234567890qwertyuiopasdfghjklzxcvbnm
Echo 1 > Temp.txt

Main.bat

@echo off
set var=
:: Code Prior to Loop


:: Loop starts below
start /b stroke.bat
:loop

::: Loop Code Goes here
:: 
:: You will not be able to take user-input in loop
:: Do not tamper with "var"
:: To force exit the loop just "goto end"
:: Note if you force exit the loop you will need to exit this bat
::   and start a new window (since Stroke will be running)
::
::: End of loop

<Temp.txt set /p var=
if "%var%" NEQ "1" goto loop
:end

:: Post loop code
Exit

这应该适用于任何字符或数字,但不能使用标点符号或Enter,Space等。

您可以通过将choice替换为set /p来修改此项仅适用于输入:

@echo off
Echo. > Temp.txt
set /p v=
Echo 1 > Temp.txt

如果您没有enter

,可以将其用于choice.exe

答案 3 :(得分:0)

最新答案,但使用choiceSET /P,但使用replace
这是主意:

@echo off
SETLOCAL EnableDelayedExpansion
echo Press any key...

>keystroke.txt replace ? . /u /w
FOR /F delims^=^ eol^= %%K in (
'2^>nul findstr /R "^" "keystroke.txt"'
) do set "key=%%K"

del /f /q "keystroke.txt"
echo You pressed: [!key!]

该脚本利用了REPLACE/W选项。

编辑:现在支持;(默认eol

答案 4 :(得分:0)

如果您希望在一定时间内未按任何键来定义默认操作,则可以使用以下选项来定义键:

 in train
  pre_test, rec_test, f1_test = compute_f1(predLabels, correctLabels, self.idx2Label)
  File "...", line 10, in compute_f1
label_pred.append([idx2Label[element] for element in sentence])
   File "...", line 10, in <listcomp>
label_pred.append([idx2Label[element] for element in sentence])
  TypeError: unhashable type: 'numpy.ndarray'

上面的代码将返回与ErrorLevel相反的文字键(注意:将所有字母都以大写形式返回)。

通过在:Label子例程中使用call并定义所有选择选项,可以通过在Call Outcome上使用@Echo off :ExampleLoop Echo.[A] Action [B] Alternate Action For /F "delims=" %%C in ('Choice /T 10 /N /C:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /D B') Do ( CLS & Set "Choice=%%C" & Call :Choice[1-%%C] || (CLS & Echo. %%C is not a valid Choice) ) Goto :ExampleLoop :Choice[1-A] Echo %~0 Echo Elective Command Exit /B 0 :Choice[1-B] ECHO %~0 Echo Default Command Exit /B 0 条件测试来避免烦人的错误提示音并输出自定义消息。当存在多个选项时,Label方法还避免了对复杂的,通常是凌乱的条件测试的需求。