Search code examples
keypressautohotkeyblocking

Autohotkey check for Ctrl pressed error


I've only recently gotten back into using AHK after taking a break from it for a few years, so forgive me if there's a very easy answer for this.

I'm writing a script which tracks keypresses while control is held down, and then allows one of 9 hotkeys to be used with 2 keypresses, so that pressing Q twice will activate the 1-1 hotkey, W then Q will activate the 1-2 hotkey etc.

my problem is that checking if Control is pressed using a hotkey for LControl blocks LControl being sent to the system, so things like Ctrl+A don't work anymore.

the only solution i've been able to think of would be to have Q W and E mapped as a hotkey only once, with a bunch of logic inside each of them.

Is there a better way I can do this?

LControl:: Set:=4
LControl Up:: Set:=0

#If (Set=4)
{
    q:: Set:=1
    w:: Set:=2
    e:: Set:=3
}

#If (Set=1)
{
    q:: SendEvent Different Words Here
    w:: SendEvent Different Words Here
    e:: SendEvent Different Words Here
}

#If (Set=2)
{
    q:: SendEvent Different Words Here
    w:: SendEvent Different Words Here
    e:: SendEvent Different Words Here
}

#If (Set=3)
{
    q:: SendEvent Different Words Here
    w:: SendEvent Different Words Here
    e:: SendEvent Different Words Here
}

Basically, how can I stop LControl:: Blocking the sending of Ctrl to the system.


Solution

  • Figured it out.

    LControl:: ;if ctrl is pressed
    Set:=4 ;enter first selection group
    SendEvent {Ctrl Down} ;don't interrupt normal hotkeys
    Return
    
    LControl Up:: ;if ctrl is released
    Set:=0 ;"unbind" q/w/e hotkeys
    SendEvent {Ctrl Up} ;and inform the system
    Return
    
    #If (Set=4)
    {
        ^q:: Set:=1
        ^w:: Set:=2
        ^e:: Set:=3
    }