February 8, 2021

AutoHotkey Pitfall: System Tray actions remove window focus

I have again stumbled upon this tricky behavior in AutoHotkey: when clicking on a script tray icon the focus of the active window will be removed.

Solution

The trick to workaround this issue, is to add SendInput, !{Esc} at the beginning of the System Tray Callback.

You may have a system tray action that will later on check for the Active Window.
I had this for example in the Teams Shortcuts Powertool, where I call toggle mute or video by clicking on the tray icon.
After switching to the Teams window, the function will restore the focus to the previously active window.

For the function to run properly I had to add in the System Tray Callback SendInput, !{Esc}

NotifyTrayClick_208:   ; Middle click (Button up)
SendInput, !{Esc} ; for call from system tray - get active window
Teams_Mute()
Menu_Show(MenuGetHandle("TrayMute"), False, Menu_TrayParams()*)
Return 
 

Credit

No comments:

Post a Comment