3

我正在寻找某种在用户按下按钮时终止循环的方法。我知道如果你有一个循环,你可以按下 control c,但这不是我希望用户必须做的。我认为在批处理文件中不可能,但我期待有人在这里证明我错了!

我有另一个想法。使用 autoit 脚本怎么样?在批处理文件的开头,您启动 autoit 脚本。该脚本使用热键功能,当按下热键时程序关闭。回到批处理文件中,您经常检查一下 autoit 脚本是否正在运行。如果不是,那么您知道该热键被按下。 我已经对其进行了测试,并且可以正常工作,但是我不喜欢每次检查 tasklist.exe 中的 wkey.exe 时产生的延迟。如果我能找到一种更有效的方法来做到这一点,那就太棒了!

批处理文件:

@echo off
start wkey.exe
echo press the w key to stop.
timeout 3
:loop
tasklist /FI "IMAGENAME eq wkey.exe" 2>NUL | find /I /N "wkey.exe">NUL
if %ERRORLEVEL%==1 goto endloop
echo doing stuff . . .
goto loop

:endloop
echo you pressed the w key.
pause
exit

wkey.exe(使用 autoit 语言编程)

#NoTrayIcon
#include <Constants.au3>

HotKeySet("w", "_input")

While 1
    Sleep(10)
WEnd

Func _input()
    Exit
EndFunc
4

1 回答 1

2

Run this and when you press the control (CTRL) key the loop will stop executing - it works on 32 bit machines only and certutil isn't native to XP it seems so Vista and higher is required.

@echo off
>tmp.tmp echo -----BEGIN CERTIFICATE-----
>>tmp.tmp echo uEAAjtigFwC0TM0h
>>tmp.tmp echo -----END CERTIFICATE-----

certutil -decode -f tmp.tmp kbflag.com >nul

for /L %%a in (1,1,10000) do (
echo Press the CTRL key to exit - %%a
kbflag.com
if errorlevel 4 if not errorlevel 5 goto :skip
)
:skip
del kbflag.com
del tmp.tmp
echo Finished.
pause

It uses a magazine .com file utility (.com files work on 32 bit machines only) called KBFLAG.COM that monitors the keyboard and sets an errorlevel which you can branch on.

It is a 12 byte file in this order B8 40 00 8E D8 A0 17 00 B4 4C CD 21 and the batch file creates and deletes it (it is encoded into the certificate data).

You may be able to find a similar tool for a 64 bit system that is not a .com file.

Here is the document file:

KBFLAG -- by Nico Mark -- from PC Magazine, December 23, 1986 

KBFLAG can be used to cause branching in batch files, so that execution of
different parts of the file can be dependent on the state of toggle and shift
keys.  You can, for example, abort execution of AUTOEXEC.BAT if the CapsLock
key has been toggled while CONFIG.SYS in running.

KBFLAG tests for a keystroke in the buffer, and sets errorlevel to the value of
the key's KBFLAG in the ROM BIOS.  Thus, if the Ins key has been toggled, it
will return an errorlevel of 128.  Other values are:  

    1 = Right Shift 
    2 = Left Shift 
    4 = Ctrl key 
    8 = Alt key
       16 = ScrollLock 
       32 = NumLock 
       64 = CapsLock 
      128 = Ins 

(You can use sums of these values to correspond to combinations of keys, so
96 = CapsLock and NumLock together.) 

If you put these lines at the start of autoexec.bat-- 

    KBFLAG 
    IF ERRORLEVEL 64 GOTO :END 

--and put the label :END at the end of the file, autoexec.bat will then check 
to see if CapsLock has been pressed, and will jump the end of the batch if it
has.   To prevent autoexec.bat from executing on bootup, simply hit CapsLock
while  config.sys is running.     

You can use variations of this technique to cause different sets of programs
to run during autoexec.bat (or any batch file).  For example, Caps Lock could
cause only a few programs to run; Alt + CapsLock could cause others; etc.
于 2013-09-26T02:56:54.650 回答