Sending Keypresses without Activating Window

2,443 views
Skip to first unread message

Philip A. Chapman

unread,
Sep 20, 2012, 5:22:57 PM9/20/12
to autoke...@googlegroups.com
Everyone,

I am working on scripting AutoKey for a game I play.  The use case is this:  I have two instances of the game running.  On one, I have my main character.  On another, I have a secondary character which supports the main (heals, etc).  I have managed to create a script that when executed will cause the main character to execution an action, switch to the secondary char's window, and cause it to do something different, then switch focus back to the main character's window.  However, this means a lot of focus switching between windows, which is not ideal.  Ideally, I would prefer to simply send commands to the backgrounded alternate character's window without switching focus.

I am just learning Python (to script AutoKey), but feel comfortable with that part of it as I have learned quite a few languages and pick up on programming languages easily.   But before I got too in depth with this, I thought I'd give a shout out to see if anyone had attempted anything like this before.   While Python itself  seems pretty easy to learn, the Python X library is a bit daunting and I think I will need  something like keyboard.fake_keypress, only sending it to another window rather than the root window.  Any advice would be appreciated.

Thanks,
Philip A. Chapman

Steve

unread,
Dec 3, 2012, 4:16:38 AM12/3/12
to autoke...@googlegroups.com
There's probably a built-in way to do this but I have not found it.  Here's my solution:

1. Install xdotool to supplement the functionality of Autokey.
xdotool lets you programatically (or manually) simulate keyboard input and mouse activity, move and resize windows, etc.

2. Then in Autokey, create a hotkey script like the following to send keystrokes to an inactive window.
system.exec_command("xdotool search --name wow2 key 1", getOutput=True)

This command searches for the window titled "wow2" whether it's active or not and sends a keypress for the number 1 key.

Note for AutoHotKey emigrants: this is how I accomplish the ControlSend feature within linux.
Reply all
Reply to author
Forward
0 new messages