asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.45k stars 1.26k forks source link

Feature request: possibility to send keystrokes only to specified process #836

Open HerbCSO opened 9 months ago

HerbCSO commented 9 months ago

Currently when sending keystrokes (e.g. via pyautogui.press()), they go to the system as a whole. Problem here is that if the focused window switches (e.g. user hits Alt-Tab), the keystrokes start going to the new window, which can create havoc.

It would be nice to have the option to send keystrokes to only a specific processes. I've only looked on Mac so far, but it seems like using CGEventPostToPSN instead of CGEventPost might be able to accomplish this there. Not sure about the other platforms, but I imagine it should be possible to do there as well.

I might be able to help out with some PRs, however before diving into that I'd like to know if this is something you would consider or not. I imagine you have some ideas on where to take something like this and I'd hate to go off on a tangent and have to rewrite a bunch of stuff. ;]