2
votes

I want to turn off capslock when I switch to the mintty window. And according Autohotkey: Toggle caps lock on/off on activating certain windows I try to hook the focused event using autohotkey. But this does not work on my pc(win7-64bit).

How to do to fix this on win7, or is there a better way or workaround?

My code is exactly the same as the answer to the link I referenced:

#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
}

Note that I am trying to use a handle of the window of notepad to test the functionality. And if this works I will replace the 'notepad' to 'mintty'.

And the info in the panel of AHK is:

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
Please post your code. What did your debugging attemps yield?MCL
@MCL I've updated my question. See if anything can help debugging the problem.Joey.Z

2 Answers

1
votes

I don't know if this is that you need.

I've used this code

#Persistent

SetTimer, testing,200
return

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

When I open the Windows calc the CapsLock turns off and vice versa.

Greetings!

0
votes
#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"
}