2013-09-26 67 views
3

進出口尋找某種使得當用戶按下一個按鈕終止循環的方法。我明白,如果你有一個循環,你可以按控制c,但這不是我想讓用戶必須做的。我不認爲這可能在批處理文件中,但我期待有人在這裏證明我錯了!使循環停止時,用戶按下按鈕

我有另外的想法。怎麼樣使用自動腳本?在批處理文件的開頭,您將啓動自動腳本。該腳本使用熱鍵功能,當按下熱鍵時程序關閉。回到批處理文件中,您經常檢查每個腳本以查看autoit腳本是否正在運行。如果不是,那麼你知道熱鍵被按下了。 我已經測試過它,和它的作品,但我不喜歡創建的每個我檢查wkey.exe的tasklist.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 

回答

3

運行這一點,當你按下控制(CTRL)鍵循環將停止執行 - 它適用於32 bit machinescertutil是不是本機到XP它似乎很Vista和更高版本。

@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 

它使用一本雜誌.com文件實用程序(.COM文件在32臺機器只工作)稱爲KBFLAG.COM監視鍵盤和設置,你可以分支上的錯誤級別。

它是一個12字節的文件的順序B8 40 00 8E D8 A0 17 00 B4 4C CD 21和批處理文件創建和刪除它(它被編碼到證書數據)。

您可以找到一個64 bit system一個類似的工具,它不是一個.com文件。

這裏是文檔文件:

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. 
+0

我敢肯定它的工作原理,但運行的是64位系統,它不會爲我工作。 – coltonon

相關問題