Multiple scripts trying to set same hotkey?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jesseleite
Posts: 3
Joined: 13 Jul 2018, 12:46

Multiple scripts trying to set same hotkey?

13 Jul 2018, 13:03

So I have these two scripts caps-hjkl.ahk, and game-specific-caps-mappings.ahk. The first script there works as expected:

Code: Select all

; Disable default CapsLock functionality
SetCapsLockState, AlwaysOff

; Post Esc if pressed alone
CapsLock::
    KeyWait, CapsLock
    If (A_PriorKey="CapsLock")
        Send {Esc}
return

; Map CapsLock modified hotkeys
#If, GetKeyState("CapsLock", "P")
h::Left
j::Down
k::Up
l::Right
In the second script, want to override CapsLock mappings for very specific active windows:

Code: Select all

#IfWinActive ahk_class Starcraft
CapsLock::Send {Insert}
#IfWinActive ahk_class AOE2
CapsLock::Send {Pause}
However, I can't get the two scripts to play together nicely. It seems whichever order they are executed/opened in, affects precedence. I know I could do this if I combined the scripts into one, but I'm trying to keep functionality separate so that I can share the first script on github, but still have my custom window specific overrides locally. Any idea if this is possible?
Qysh
Posts: 143
Joined: 24 Apr 2018, 09:16

Re: Multiple scripts trying to set same hotkey?

13 Jul 2018, 13:38

Try those
Or this
jesseleite
Posts: 3
Joined: 13 Jul 2018, 12:46

Re: Multiple scripts trying to set same hotkey?

13 Jul 2018, 14:45

Thanks for reply! Yeah I see what you are doing in the second example, but trying to keep them separate files if I can so that I can share the hjkl functionality on github, and have my game overrides a more local/personal thing. The first example doesn't seem to work. If I reload the first caps+hjkl script, the second override script stops working still.
jesseleite
Posts: 3
Joined: 13 Jul 2018, 12:46

Re: Multiple scripts trying to set same hotkey?

16 Jul 2018, 16:34

Figured it out! Now I have this in my main autohotkey script:

Code: Select all

#include includes/caps-hjkl-or-esc.ahk

OverrideCapsLock()
{
    If WinActive("Brood War")
        return "."
    If WinActive("Age of Empires")
        return "."
}
And this is my caps-hjkl-or-esc.ahk script:

Code: Select all

#NoEnv
SendMode Input

; Disable default CapsLock functionality
SetCapsLockState, AlwaysOff

; Post Esc if pressed alone
CapsLock::

    ; If you want to override CapsLock functionality for specific windows, etc.
    ; Just define an OverrideCapsLock() function where you #include this script!
    OverrideFunction := "OverrideCapsLock"
    
    ; Call the function dynamically to allow for silent failure.
    if (Overridden := %OverrideFunction%()) {
        Send {%Overridden%}
        return
    }
    
    KeyWait, CapsLock
    if (A_PriorKey="CapsLock")
        Send {Esc}
return

; Map CapsLock modified hjkl movement
#If, GetKeyState("CapsLock", "P")
h::Left
j::Down
k::Up
l::Right
#If
Now I can share that caps+hjkl script on github for other vim users, but still completely override it locally in another script for specific games, etc. The dynamic function call there fails silently if the user doesn't define an OverrideCapsLock() function. If anyone is interested, https://github.com/JesseLeite/dotfiles/ ... or-esc.ahk

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR and 259 guests