2013-04-28 616 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(...)来检查热键定义是否被按下。