2013-04-28 618 views
2

所以我試圖創建一個腳本,將滾動左側和右側,同時按住鼠標中鍵。但是,無論鼠標中鍵是否按下,滾動左右滾動。它總是執行。我需要幫助來解決這個問題。(AHK)如果GetKeyState語句不工作?

(我在第21行注意到有一點太多的空間,忽略) 代碼:

; Hold the scroll wheel and scroll to scroll horizontally 
; Scroll up = left, scroll down = right 

#NoEnv 
;#InstallMouseHook 

#HotkeyInterval 1 
#MaxHotkeysPerInterval 1000000 ; Prevents the popup when scrolling too fast 

GetKeyState, ScrollState, MButton 

if(ScrollState = U) 
{ 
     ;return 
} 
else if(ScrollState = D) 
{ 
     WheelUp::Send {WheelLeft} 
     return 

     WheelDown::  Send {WheelRight} 
     return 
} 
return 

回答

3

這種方法保持一切正常中間點擊功能,但按下時簡單地切換變量state。只要使用Wheelup或Wheeldown,就會檢查此變量。

~Mbutton:: 
    state := 1 
Return 

~Mbutton up:: 
    state := 0 
Return 

WheelUp:: Send % (state) ? "{WheelLeft}" : "{WheelUp}" 
WheelDown:: Send % (state) ? "{WheelRight}" : "{WheelDown}" 

/* 
The ternary operators are short for: 
If state = 1 
    Send {WheelLeft} 
else 
    Send {WheelUp} 
*/ 
+0

感謝您的支持。如果你能解釋最後兩行的含義(特別是%和?符號),那會很好。 – 2013-04-28 22:27:01

+0

它使用所謂的三元運算符,這是一個縮短if/then/else。因此,對於Wheelup,如果'state = 1',則發送結果'{WheelLeft}'或發送'{WheelUp}'。爲了清楚起見,我加入了我的答案。 – 2013-04-28 22:31:12

1

熱鍵,通過雙冒號所定義,沒有被正規if語句控制。要製作熱鍵上下文敏感,您需要使用#If(或#IfWinActive#IfWinExist)。從文檔(上下文相關的熱鍵節)的一個例子:

#If MouseIsOver("ahk_class Shell_TrayWnd") 
WheelUp::Send {Volume_Up}  ; Wheel over taskbar: increase/decrease volume. 
WheelDown::Send {Volume_Down} ; 

你也可以把經常if邏輯熱鍵(這裏是從熱鍵提示的例子,說明部分):

Joy2:: 
if not GetKeyState("Control") ; Neither the left nor right Control key is down. 
    return ; i.e. Do nothing. 
MsgBox You pressed the first joystick's second button while holding down the Control key. 
return 

經由#If上下文靈敏度旨在用於控制應用程序的熱鍵是在激活狀態。普通if邏輯插件ide熱鍵定義適用於任意條件。你想做的事情適合後者。

在很多情況下,兩者都有用。例如,如果您只想在瀏覽器中使用左/右行爲,但不使用Microsoft Word,則可以使用#If將熱鍵活動限制在瀏覽器中,然後使用if GetKeyState(...)來檢查熱鍵定義是否被按下。