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:
hexKeyCode
is the virtual keyboard mapping as defined by the Windows API. The list of codes is available on MSDN: Virtual-Key Codes (Windows)I know this is an old question but it's still at the top of google.
For both python3 and python2 you can use
pyautogui
(pip install pyautogui
)macOS
Here is the more complete version of @Phylliida answer in form of class with code example:
Here is the demo code using above class:
which will simulate typing
Hello World!
text on the current window.You can run above code as a shell script. Check the link to the
keyboard.py
file.user648852's idea at least for me works great for OS X, here is the code to do it:
For Python2.7(windows32) I installed only pywin32-223. And I wrote simple python`s code:
It can be checked if you run the code and immediately go to the notepad window (where the text already exists) and place the cursor on the top line.
Every platform is going to have a different approach to being able to generate keyboard events. This is because they each need to make use of system libraries (and system extensions). For a cross platform solution, you would need to take each of these solutions and wrap then into a platform check to perform the proper approach.
For windows, you might be able to use the pywin32 extension. win32api.keybd_event
You will need to investigate pywin32 for how to properly use it, as I have never used it.