"Toggle" Hotkey for the Immediate Window

Am I the only one who finds it annoying that [Ctl] + [G] doesn't *toggle* between the immediate window and the code window? Let's fix that.

"Toggle" Hotkey for the Immediate Window

I try to use the keyboard as much as possible when developing.  Something that always bugged me about the Immediate Window in VBA is that there are two different hotkeys to move back and forth between code and the immediate window.

To move from code to the immediate window, you press [Ctl] + [G].  To move back to the code window you would press [F7].  This always felt awkward to me, so I created a simple hotkey in my standard Autohotkey script so that I can use [Ctl] + [G] to move back and forth in both directions.

I use the #IfWinActive directive in Autohotkey to limit this hotkey to running only inside the VBA IDE:

; VBA IDE
#IfWinActive ahk_class wndclass_desked_gsk
^g::   ; Ctl + g: Toggle immediate window
    WinGet, WindowUniqueID, ID, A
    ControlGetFocus, ControlID, ahk_id %WindowUniqueID%
    ControlGet, ControlHwnd, Hwnd,, %ControlID%, ahk_id %WindowUniqueID%
    ControlTextSize = 18  ; expand capacity to hold 9 two-byte (i.e., Unicode) characters in buffer "I-m-m-e-d-i-a-t-e"
    VarSetCapacity(ControlText, ControlTextSize)
    SendMessage, 0xD, ControlTextSize, &ControlText,, ahk_id %ControlHWND%  ; 0xD is WM_GETTEXT.
    If (ControlText="Immediate")
        Send {F7}
    Else
        Send ^g
    Return

; Turn off context-sensitivity
#IfWinActive

Image modified from Wikimedia Commons by Mike Wolfe

All original code samples by Mike Wolfe are licensed under CC BY 4.0