如何在win7中挂钩窗口聚焦事件

时间:2014-07-04 07:22:18

标签: autohotkey

我想在切换到mintty窗口时关闭大写锁定。根据{{​​3}}我尝试使用autohotkey挂钩聚焦事件。但这不适用于我的电脑(win7-64bit)。

如何在win7上修复此问题,还是有更好的方法或解决方法?

我的代码与我引用的链接的答案完全相同:

#Persistent ; Don't close when the auto-execute ends

SetTitleMatchMode, 2 ; Partial title matching
WinGet, myHwnd, ID, AutoHotKeys; Get the handle to the your window

; Listen for activation messages to all windows
DllCall("CoInitialize", "uint", 0)
if (!hWinEventHook := DllCall("SetWinEventHook", "uint", 0x3, "uint",     0x3, "uint", 0, "uint", RegisterCallback("HookProc"), "uint", 0, "uint", 0,     "uint", 0))
{
    MsgBox, Error creating shell hook
    Exitapp
}

;MsgBox, Hook made
;DllCall("UnhookWinEvent", "uint", hWinEventHook) ; Remove the     message listening hook
return

; Handle the messages we hooked on to
HookProc(hWinEventHook, event, hwnd, idObject, idChild,     dwEventThread, dwmsEventTime)
{
    global myHwnd
    static lastHwnd
    WinGetTitle, title, ahk_id %hwnd%

    if (hwnd == myHwnd) ; If our window was just activated
    {
        tooltip, Gained focus
    }
    else if (lastHwnd == myHwnd) ; If our window was just     deactivated
    {
        tooltip, Lost focus
    }

    lastHwnd := hwnd
}

请注意,我正在尝试使用notepad窗口的句柄来测试功能。如果这有效,我会将'记事本'替换为'薄荷'。

AHK小组的信息是:

Script lines most recently executed (oldest first).  Press [F5] to refresh.  The seconds elapsed between a line and the one after it is in parentheses to the right (if not 0).  The bottommost line's elapsed time is the number of seconds since it executed.

025: SetTitleMatchMode,2
026: WinGet,myHwnd,ID,AutoHotKeys; Get the handle to the your window
029: DllCall("CoInitialize", "uint", 0)  
030: if (!hWinEventHook := DllCall("SetWinEventHook", "uint", 0x3, "uint",     0x3, "uint", 0, "uint", RegisterCallback("HookProc"), "uint", 0, "uint", 0,     "uint", 0))  
038: Return (1.28)
045: WinGetTitle,title,ahk_id %hwnd%
047: if (hwnd == myHwnd)  
051: if (lastHwnd == myHwnd)  
053: ToolTip,Lost focus (0.03)
054: }
056: lastHwnd := hwnd
057: } (0.05)
045: WinGetTitle,title,ahk_id %hwnd%
047: if (hwnd == myHwnd)  
051: if (lastHwnd == myHwnd)  
056: lastHwnd := hwnd
057: } (2.14)

Press [F5] to refresh.

2 个答案:

答案 0 :(得分:1)

我不知道这是否是你需要的。

我已经使用了这段代码

#Persistent

SetTimer, testing,200
return

testing:
IfWinExist Calculadora
{
    SetCapsLockState ,Off
}
else
{
    SetCapsLockState ,On
}
return

当我打开Windows calc时,CapsLock会关闭,反之亦然。

问候!

答案 1 :(得分:0)

#Persistent

SetTitleMatchMode, 2   ; use RegEx for finer control

Loop
{
    WinWaitActive, Notepad
    {
        WinGet, opVar, ID
        CapsLockState := GetKeyState("CapsLock", "T")
        SetCapsLockState, AlwaysOff   ; or just "Off"
    }

    WinWaitNotActive, ahk_id %opVar%
        SetCapsLockState, % CapsLockState ? "On" : "Off"
}