AutoHotkey(AHK):GetKeyState无法识别ControlSend产生的按键

时间:2014-12-21 02:57:57

标签: state autohotkey

我有一个非常复杂的脚本,它会按下一个键,并且需要在某些位置检查该键是否仍然被按下,所以GetKeyState看起来很完美,但我无法让它工作,所以我制作了一个简单的脚本只做那个,它仍然不承认状态。

脚本如下:

    #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    ~#Right::
    ControlSend,, {d down}, ahk_pid 6920
    Loop{
        GetKeyState, dState, d
        ;MsgBox, d Key State: %dState%
        SplashTextOn,300,50, AutoNavigatorInfo, d Key State: %dState%
        WinMove, AutoNavigatorInfo, , 300, 0  ; Move the splash window to the top left corner.
    }

可悲的是,我使用的splashText窗口不断将dState中继为U。在我正在使用的测试窗口中看到很奇怪,它与正在按下的d键正确交互。

2 个答案:

答案 0 :(得分:2)

GetKeyState不适用于controlsend,因为GetKeyState获取键的全局系统状态,但controlsend仅在本地设置状态,即只为一个控件或窗口设置键状态。

答案 1 :(得分:2)

我同意 blackholyman " GetKeyState不适用于controlsend,因为GetKeyState获取密钥的全局系统状态,但controlsend仅在本地设置状态,即密钥状态仅设置为1控制或窗口。"

但是如果你需要" ControlSend用于某些窗口功能,例如通过PID发送命令"我想你也可以用Send命令来做。使用WinActivate激活您需要发送密钥的窗口,并在使用Send发送密钥后使用。您可以使用WinActivate命令而不是Wintitle参数来使用PID,更多信息请参见:http://ahkscript.org/docs/misc/WinTitle.htm

试试这段代码:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
~#Right::
WinActivate, ahk_pid 6920
Send, {d down}
Loop{
    GetKeyState, dState, d
    ;MsgBox, d Key State: %dState%
    SplashTextOn,300,50, AutoNavigatorInfo, d Key State: %dState%
    WinMove, AutoNavigatorInfo, , 300, 0  ; Move the splash window to the top left corner.
}
相关问题