3

ユーザーがボタンを押したときにループを終了させる何らかの方法を探しています。ループが進行している場合、コントロール 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

これを実行すると、コントロール (CTRL) キーを押すとループの実行が停止します。これは XP で32 bit machinesのみ機能し、certutilネイティブではなく、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.
于 2013-09-26T02:56:54.650 に答える