Search code examples
scriptingautohotkey

Simple AHK script, trying to understand if this is expected or if I'm doing something wrong


Here is the code:

*Lalt::return

#if GetKeyState("Lalt", "P")

a::
   
   b::
   
   if GetKeyState("shift", "P")
   
   send ^+{%A_ThisHotkey%}
   
   else
   
      Send ^{%A_ThisHotkey%}
return

#if  

        

in the code above, when I press Lalt+a it sends Ctrl+a which is expected, but when I press Lalt+Shift+a it only sends Shift+a, shouldn't it send Ctrl+Shift+a ?!. I also noticed that if I press the Shift key first before pressing on Lalt it sends Alt+Shift+a. It sounds to me like the Shift key trumps Alt or it gets sent to my machine before going through AH K.

tried to use the * modifier and {blind} with send, but still when I press Lalt+shift+a, it only sends Shift+a


Solution

  • AHK (and Windows) is very finicky with modifiers keys, so it's normal to have issues when working with them.

    First thing is that when you press Shift, the AHK hook changes from a:: to +a::, as now another hot key combination is being pressed.

    The other issue is that {%A_ThisHotkey%} in the context of +a:: don't work as expected, returning +a, it just seems to return nothing.

    Finally, this would be the result:

    *Lalt::return
    
    #if GetKeyState("Lalt", "P")
    
    
    a::
    
       b::
    
          Send ^{%A_ThisHotkey%}
          
    return
    
    +a::  
       +b::
          Send ^%A_ThisHotkey%
       
    return
    
    #if  
    

    For debuging, I used this env configurations:

    #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    ; #Warn  ; Enable warnings to assist with detecting common errors.
    SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    KeyHistory