I'm looking for a way to simulate a key press in an out of focus window.

More specifically, I need to press a key every x seconds in a browser page for a given amount of time. It should look like a simple key press while on the webpage without any input field selected.

I'm currently doing this with a simple AHK script, but I can't use the laptop meanwhile because I need to leave the browser's page in focus.

Do you have any ideas about that? Thank you.

edit: If it can help, there are two options in this webpage that I can select using the keys "0" and "1", and I would like to be able to select one of them while the window is minimized or in background.

New contributor
Francesco Villano is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Doesn't your script work without focus? It would be a good idea to show us this script. – harrymc Jan 9 at 21:25
  • My original script uses a simple "Send" command. I also tried ControlSend as suggested in the other answer, but it doesn't seem to fit my needs. – Francesco Villano Jan 10 at 13:05


Simulate a key press (send the key "a") in an (out of focus or minimized) notepad window every 2 seconds:

    ControlSend,, a, ahk_class Notepad
    Sleep, 2000 ; 2 seconds


Instead of Loop you can use SetTimer.

  • I tried this method but it doesn't work. I'll try to be more specific: in the browser page there are two options that I can select with the keys "0" and "1"; with ControlSend I found that an option is selected only if the windows is in focus. – Francesco Villano Jan 10 at 13:01

Your Answer

Francesco Villano is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.