Search code examples
autohotkey

How do I prevent the menu bar from being activated/focused when I press alt?


Pressing or holding a modifier key (without pressing another) triggers a keyboard shortcut:

~LControl::
    keyWait,LControl,t0.1
    if !errorLevel
        Sendinput, ^{Space}             ; open command pallete
    else{
        KeyWait, LControl
        if (A_PriorKey="LControl")
            SendInput, ^{f}              ; ope find bar
    }
    return

~lalt::
    keyWait,lalt,t0.1
    if !errorLevel
        Sendinput, ^{f1}             ; open color pallete
    else{
        KeyWait, lalt
        if (A_PriorKey="lalt")
            SendInput, ^{f2}              ; ope search bar
    }
    return

It works with any modifier key except lalt that triggers the Windows menu bar. I tried holding F2 so it thinks I pressed another key with Alt which does not work:

~lalt::
    !{blind}{f22 down} ; this shortcut is not bound to a command
    keyWait,lalt,t0.1
    if !errorLevel
        Sendinput, ^{f1}             ; open color pallete
    else{
        KeyWait, lalt
    !{blind}{f22 up} ; this shortcut is not bound to a command
        if (A_PriorKey="lalt")
            SendInput, ^{f2}              ; ope search bar
    }
    return

I need the "~" prefix to see if a modifier key is down for operations like Ctrl + left click + drag.


Solution

  • To prevent Alt triggering the menu bar permanently use

    ~LAlt::Send {Blind}{vkE8}
    

    See MenuMaskKey

    In this case

    ~lalt::
        Send {Blind}{vkE8}
        keyWait,lalt,t0.1
        if !errorLevel
            Sendinput, ^{f1}             ; open color pallete
        else{
            KeyWait, lalt
            if (A_PriorKey="lalt")
                SendInput, ^{f2}              ; ope search bar
        }
        return
    

    Untested.