(AHK)如果GetKeyState的语句不起作用?

时间:2013-04-28 05:42:25

标签: autohotkey

所以我正在尝试创建一个脚本,在按住鼠标中键的同时左右滚动。但是,无论是否按住鼠标中键,左右滚动都会滚动。它总是执行。我需要帮助来解决这个问题。

(我注意到第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

2 个答案:

答案 0 :(得分: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}
*/

答案 1 :(得分: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逻辑适用于任意条件。你想要做的是适合后者。

在许多情况下,两者都很有用。例如,如果您只想在浏览器中而不是在Microsoft Word中使用左/右行为,则可以使用#If将热键活动限制为浏览器,然后在热键定义中将if GetKeyState(...)限制为检查是否按下了滚动按钮。

相关问题