Solution For Sending Keypresses With AutoHotkey to IntelliJ IDEs

2019-09-23 Reading time: 2 to 6 minutes, depending on if you are working along

For a very long time, I had problems with AutoHotkey input in IntelliJ IDEs including PhpStorm and WebStorm. It's probably related how Java AWT works or something. My solution might help if you have problems with sending keys in other Java apps aswell.

Every time I sent keypresses to the IntelliJ, it acted weirdly: typed in other characters and lost keyboard focus. So I tried delaying keyup, but with no results. Then I read about AHK's BlockInput, it will wait until I release keys and then sends input to the IDE. AHK by default doesn't block hotkeys used in detection. That was causing the problem - IntelliJ got too much key input and went crazy.

Here is what I ended up doing to fix the issue:

<^<!a::
    SetKeyDelay, 0, 50    ; extra stability when key stays down for a while
    ;KeyWait Control
    ;KeyWait Alt
    KeyWait, a            ; it was enough to just wait for the last detection key
    BlockInput On
    Sleep 40              ; extra stability with sleep
    ;Send, {Asc 124}      ; sometimes worked
    ;SendInput, {Asc 124} ; sometimes worked
    ;SendPlay, {Asc 124}  ; didnt work at all
    SendRaw, |            ; most stable, no issues at all
    BlockInput Off
return

As you can see, I didn't need to wait for modifier keys, only the last detection key. SendRaw was the only one which did work for me. If you want to send backtick ` with SendRaw, you need to put it twice as backtick is an escape symbol for SendRaw (see my final code below). Also, I did add IfWinActive hotstring as I don't want those keys to be "sleepy" in other apps.

#IfWinActive, ahk_class SunAwtFrame
    input blocking here, see full example below
#IfWinActive

It's still not perfect tho. It sometimes swaps my Windows keyboard layout as if I press win+space. Dunno where it comes from, maybe its just me with my fat fingers pressing all the keys at once.

Why did I do all this?

I use Estonian keyboard layout, but my keyboard doesn't have that extra key between Shift and Z which some keyboards have. I've been using this layout for my entire life, and it is so hard to re-learn. So I wrote an AutoHotkey script which will map <, > and | symbols to the Z key.

Estonian keyboard layout with an extra key between Shift and Z. Original image by Indrek
Here is my final code if somebody is interested. I have Photoshop detection too because Ctrl+Alt+Z is toggling the last action in Photoshop and the script will otherwise break that. You can make < symbol by pressing Ctrl+Alt+Z, > symbol with Ctrl+Alt+X or Ctrl+Alt+Shift+Z and | symbol with Ctrl+Alt+A

#IfWinNotActive, ahk_class Photoshop
; <
<^<!z::
   #IfWinActive, ahk_class SunAwtFrame
      SetKeyDelay, 0, 50
      KeyWait, a
      BlockInput On
      Sleep 40
   #IfWinActive
   SendRaw, <
   BlockInput Off
return

; >
<^<!x::
<^<!+z::
   #IfWinActive, ahk_class SunAwtFrame
      SetKeyDelay, 0, 50
      KeyWait, z
      BlockInput On
      Sleep 40
   #IfWinActive
   SendRaw, >
   BlockInput Off
return
#IfWinNotActive

; |
<^<!a::
   #IfWinActive, ahk_class SunAwtFrame
      SetKeyDelay, 0, 50
      KeyWait, a
      BlockInput On
      Sleep 40
   #IfWinActive
   SendRaw, |
   BlockInput Off
return

; Backtick ` for JS template literal
<^<!'::
   #IfWinActive, ahk_class SunAwtFrame
      SetKeyDelay, 0, 50
      KeyWait, '
      BlockInput On
      Sleep 40
   #IfWinActive
   SendRaw, ``
   BlockInput Off
return

SetKeyDelay, 0