`如果WinActive`和`GetKeyState`

时间:2017-05-02 16:05:23

标签: autohotkey

任何人都可以解释,为什么我会得到"错误"消息框?

我认为,代码可以自我解释。我很惊讶"当我发现它由于某种原因而无法工作时。

(当你在打开的记事本窗口中按F1时,你会看到"它有效!"消息。相反,我得到一个"错误"消息。)

我已经尝试了不同的方法来解决它,即百分比,变量分配,括号,但目前它仍然无法正常工作。

#SingleInstance, Force
SetTitleMatchMode, 2

f1::
+f1::
GetKeyState, shift_state, Shift
msgbox, %shift_state%
if (WinActive("Notepad") and shift_state = D)
    msgbox, It works! By the way, the Shift key is pressed.
else if (WinActive("Notepad") and shift_state = U)
    msgbox, It works! The Shift key is not pressed.
else
    msgbox, error
return

1 个答案:

答案 0 :(得分:1)

在表达模式中,文字字符串必须用双引号括起来,以区别于变量。

https://autohotkey.com/docs/Variables.htm#Expressions

表达方式:

#SingleInstance, Force 
SetTitleMatchMode, 2

f1::
+f1::
GetKeyState, shift_state, Shift
    msgbox, %shift_state%
if (WinActive("Notepad") and shift_state = "D")
    msgbox, It works! By the way, the Shift key is pressed.
else if (WinActive("Notepad") and shift_state = "U")
    msgbox, It works! The Shift key is not pressed.
else
    msgbox, error
return

传统模式:

#SingleInstance, Force 
SetTitleMatchMode, 2

f1::
+f1::
GetKeyState, shift_state, Shift
    msgbox, %shift_state%
IfWinActive Notepad
{
    If shift_state = D
        msgbox, It works! By the way, the Shift key is pressed.
    else If shift_state = U
        msgbox, It works! The Shift key is not pressed.
}
else
    msgbox, error
return