2012-03-08 68 views
2

我想寫一個腳本,其中有一個循環,每兩秒鐘按一次上方向鍵。當我按下空格鍵時,必須激活循環,​​而當我再次按下空格鍵時,循環將被禁用。我現在正在使用這個。使用GetKeyState()和循環

$Space:: 
if GetKeyState("Space", "P") 
{ 
    Loop 
    { 
     Sleep 2000 
     Send {Up} 

     if GetKeyState("Space", "P") 
     { 
      return 
     } 
    } 
} 

出於某種原因,內循環if條件不工作,即我不能走出循環。我希望任何人都可以幫助我...

回答

0

如何使用SetTimer

; Create timer. 
SetTimer, SendUp, 2000 

; Set timer to 'Off' at start of script. 
SetTimer, SendUp, Off 
TimerEnabled := False 

; When Space is pressed toggle the state of the timer. 
$Space:: 
    If TimerEnabled 
     { 
     SetTimer, SendUp, Off 
     TimerEnabled := False 
     } 
    Else 
     { 
     SetTimer, SendUp, On 
     TimerEnabled := True 
     } 

; Label called by timer to send {Up} key. 
SendUp: 
    Send, {Up} 
return 
1

你不會需要第一if GetKeyState("Space", "P")
,你將需要保持空間當循環到了第二個
它打破;你需要用break替換return

但是我同意加里,但我會寫這樣的:

; (on:=!on) reverses the value of variable 'on' 
; the first press of space reverses on's value (nothing) to something (1) 
; the second press reverses on's value from (1) to (0) 
; when (on = 1) delay will be set to 2000, and Off when (on = 0) 

space::SetTimer, Action, % (on:=!on) ? ("2000") : ("Off") 

Action: 
Send, {up} 
Return 

%開始的表達式。

http://l.autohotkey.net/docs/Variables.htm


三元運營商
這是運營商的if-else語句的簡寫更換。
它評估其左側的條件以確定
其兩個分支中的哪一個將成爲最終結果。
例如,var:= x> y?如果x大於y,則2:3將2存儲在Var中;否則它存儲3.

+0

哦,很好。比我的更乾淨。 – 2012-03-12 09:39:41