Search code examples
user-interfacesendautohotkey

Send from Gui to last active window


-Extreme Beginner-

I made a Button that sends µ whenever it's pressed, but I can hear Windows Error because the script is sending µ to the GUI where there is no text field to type in into. How can I click on the GUI and make it send text to my last active window.

#SingleInstance Force

Gui, Add, Button, w100 gKey, Micro
Gui, Show
return

Key:
send, µ
return

Solution

  • You need to switch back to the previous window through this:

    Key:
    IfWinActive, YourAppWindowName ; IfWinActive is not absolutely required, but if you use this test, then you need "SetTitleMatchMode, 2" at the top of your script.
        Send, !{Esc} ; Switch back to previous window
    Sleep, 100 ; Wait 0.1 sec for windows to switch back
    send, µ
    return