short summary:
I am trying to create a program that will send keyboard events to the computer that for all purposes the simulated events should be treated as actual keystrokes on the keyboard.
original post:
I am looking for a way to generate keyboard events using python.
Assume that the function receives a key that it must simulate pressing, like so:
keyboardevent('a') #lower case 'a'
keyboardevent('B') #upper case 'B'
keyboardevent('->') # right arrow key
def keyboardevent(key):
#code that simulated 'key' being pressed on keyboard
The above are obviously examples, but what I am looking for is a library, module, or whatever, which I can use to simulate keyboard events.
note: This is different than sending characters to notepads, or inputting text into fields or such. I want the python script to simulate an actual keyboard event, the computer will think that there is really a keyboard event.
Extra Note:
I don’t want to send keystrokes to the active window – I want the system to believe the keyboard’s keys are being pressed, subtle difference, as some active-windows do not accept certain key-combinations, or if I wanted to use keyboard shortcuts for background processes through my script, they don’t need to go through the active-window
So far I have looked at these things:
Generate keyboard events for the frontmost application
How to generate keyboard keypress events through Python?
Which were all about apple and didn’t help at all.
And this:
Which is the easiest way to simulate keyboard and mouse on Python?
Which seems like it might be what I need, but I can not find the library for it or any documentation.
I have searched more places as well, but have yet to find a solution.
It can be done using ctypes:
hexKeyCodeis the virtual keyboard mapping as defined by the Windows API. The list of codes is available on MSDN: Virtual-Key Codes (Windows)