Simulate Keypresses In Python
This demonstrates how to press keys with Python. Using pynput we are able to simulate key presses into any window. This will show you how to press and release a key, type special keys and type a sentence.
If you haven’t used or setup pip before, go to my tutorial at how-to-setup-pythons-pip to setup pip.
We will be using the pynput module to listen to mouse events. To install this module execute pip install pynput in cmd. Watch the output to make sure no errors have occurred; it will tell you when the module has been successfully installed.

To double-check that it was installed successfully, open up IDLE and execute the command import pynput ; no errors should occur.

Create a new script and save it somewhere so you can easily run the script. Import Key and Controller from pynput.keyboard .
Make a variable called keyboard and set it to an instance of Controller . Now using the keyboard variable we can press and release keys.
Pressing and Releasing Keys
Using keyboard.press we can press keys and with keyboard.release we can release a key. This allows us to type a key by pressing and releasing. You can only supply this method with one key at a time. Here is an example of how to type the letter ‘a’.
Pressing and Releasing Special Keys
For special keys that can’t be put into a string like shift or control, you will need to refer to the page here to look at the Key class for supported keys. Using these in the press or release methods will press/release the key matching it. For example, if I wanted to press the Windows key, I would look at that page for the key. ‘cmd’ has the description «A generic command button. On PC platforms, this corresponds to the Super key or Windows key, and on Mac it corresponds to the Command key» which is what I am looking for. Now for the code.
This method also allows us to press a key while holding another key, for example, ctrl+c to copy. To do this we will need to press ctrl, press and release c and then release ctrl.
Here are a few other common special keys:
-
: Left ALT : Backspace : Left Ctrl : Delete : Enter : Escape : F1 : F5 : Play/Pause : Page Down : Up Arrow Key
- The rest can be found in the pynput docs for the Key class.
Typing Multiple Keys
A cool feature supplied by the class is the type method. This method allows us to type more than one key at a time but it has to be a string of characters. So if we wanted to type «Nitratine» we would execute:
This method does also support spaces but when it comes to enters, use a new line character (\n) and a tab character (\t) for tabs.
Putting A Random Delay Between Each Keypress
To put a random delay between each keypress, you can use time.sleep with a random number passed to it. Here is a small example function I made:
Common Issues and Questions
How can I use the keyboard and mouse controllers at the same time?
When you import the classes, Controller will be set to the last one imported. To show what the issue was, ask yourself, what controller did you use to set the mouse and what one to set the keyboard? You would have used the same, but they need to be from the different classes. So then you should use:
Now when you want to use the controller for the mouse use MouseController and KeyboardController for the keyboard.
ModuleNotFoundError/ImportError : No module named ‘pynput’
Did you install pynput? This error will not occur if you installed it properly. If you have multiple versions of Python, make sure you are installing pynput on the same version as what you are running the script with.
I got a SyntaxError
Syntax errors are caused by you and there is nothing I can offer to fix it apart from telling you to read the error. They always say where the error is in the output using a ^. Generally, people that get this issue have incorrect indentation, brackets in the wrong place or something spelt wrong. You can read about SyntaxError on Python’s docs here.
The Key Presses Work in Notepad But Not My Game
pynput uses a Win32API function called SendInput . The SendInput function will insert input events into the same queue as a hardware device but the events are marked with a LLMHF_INJECTED flag that can be detected by hooks and then filtered. To avoid this flag you probably have to write a custom driver (ref: stackoverflow/Anders).
It would be ideal for most games to look for these events if they want to reduce ‘bot’ activity as it stops packages like these being used.
In github.com/Gautam-J/Self-Driving-Car I had seen that the file directkeys.py contained the following:
The license for this piece of code can be found here.
This file demonstrates how we can press keys using DirectX key codes. The link in the file no longer exists but it can still be found on the wayback machine. This page provides other codes for keys that can be simulated.
Unfortunately it will only work on Windows (due to the usage of ctypes.windll ) but some may find that it solves their issues with the LLMHF_INJECTED flag.
Owner of PyTutorials and creator of auto-py-to-exe. I enjoy making quick tutorials for people new to particular topics in Python and tools that help fix small things.
How To Install python3-pynput on Ubuntu 22.04
In this tutorial we learn how to install python3-pynput on Ubuntu 22.04.
What is python3-pynput
This library allows you to control and monitor input devices. Currently, mouse and keyboard input and monitoring are supported.
This package installs the library for Python 3.
There are three ways to install python3-pynput on Ubuntu 22.04. We can use apt-get , apt and aptitude . In the following sections we will describe each method. You can choose one of them.
Install python3-pynput Using apt-get
Update apt database with apt-get using the following command.
After updating apt database, We can install python3-pynput using apt-get by running the following command:
Install python3-pynput Using apt
Update apt database with apt using the following command.
After updating apt database, We can install python3-pynput using apt by running the following command:
Install python3-pynput Using aptitude
If you want to follow this method, you might need to install aptitude first since aptitude is usually not installed by default on Ubuntu. Update apt database with aptitude using the following command.
After updating apt database, We can install python3-pynput using aptitude by running the following command:
How To Uninstall python3-pynput on Ubuntu 22.04
To uninstall only the python3-pynput package we can use the following command:
Uninstall python3-pynput And Its Dependencies
To uninstall python3-pynput and its dependencies that are no longer needed by Ubuntu 22.04, we can use the command below:
Remove python3-pynput Configurations and Data
To remove python3-pynput configuration and data from Ubuntu 22.04 we can use the following command:
Remove python3-pynput configuration, data, and all of its dependencies
We can use the following command to remove python3-pynput configurations, data and all of its dependencies, we can use the following command:
References
Summary
In this tutorial we learn how to install python3-pynput package on Ubuntu 22.04 using different package management tools: apt, apt-get and aptitude.
Evil Python Lesson 6: Keyboard Input!
The function input returns a variable of type str . If x = input() is executed and 5 Enter is typed, then type(x) will yield <class ‘str’> .
Notice that the variable age stores a number but its type is str .
Keyboard Listening Using the pynput Package
Recall that we can create a keyboard listener using the pynput package’s keyboard module. We define two callback functions to which we set the on_press and on_release parameters of the keyboard.Listener function, which returns a keyboard listener. The callback functions press_callback and release_callback will then execute every time a key is pressed or released, respectively.
The reason we need to use l.start() and l.join() is a bit complicated, so just remember that pynput keyboard listeners need to do both!
Continuous Input: pynput
Recall from the previous lesson’s assignments that games can be created using Python’s built-in input function, while more advanced (and more fun) games can be created in which the user does not have to press Enter after every key press.
Installing pynput
The pynput package allows Python to “listen” to keyboard input continuously.
To install pynput , complete the following:
- Navigate to folder containing the .py file using cd foldername1/foldername2 , etc.
- Read the PACKAGE INSTALL NOTE below, then type pip install pynput
- Run a game called (for example) filename.py using python filename.py .
- If you get an error that contains the words user or permissions , run the program using sudo python filename.py . You may be prompted for a password.
- Type python -m venv venv
- Type source venv/bin/activate
- Type pip install pynput
Overview: “Listening” to Input
So far, we have written Python code that prompts the user for textual input through the command prompt with the built-in Python input function. While typing our responses into the command prompt, we can navigate away from the window or type and erase, and Python won’t know the difference until we press Enter .
By contrast, pynput is a keyboard listener, which means it interacts with your operating system (Windows, Mac OS, or Linux) so that it knows about all keyboard actions. In fact, pynput can even control the keyboard and trigger automated key-presses.
You can view the official pynput keyboard documentation here.
Events and Callbacks
The way pynput handles keyboard input is by being aware of all keyboard actions, called “events”, and to respond to them by executing functions, called “callbacks”.
The events that pynput can respond to are key-presses and key-releases.
A very simple key-press callback function that prints the pressed key looks like this:
Callbacks take a parameter — here it is called key . When we register this function as a callback, pynput sends the identity of the pressed key as an argument to this function when it executes.
To register this function as a callback, we create an object of the the pynput.keyboard.Listener class and its start and join method. For now, just copy this syntax to implement a keyboard listener:
The keyboard.Listener method takes a keyword parameter called on_press , which is here set to our callback press_callback . This function is executed whenever any key is pressed, with an argument that gives the identity of the pressed key, which becomes the parameter of the callback, key .
Keyboard Listening: pynput.keyboard.Listener
“Press” and “Release”: on_press and on_release
We can implement a more complicated listener that responds to key-presses as well as key-releases.
The key Variable is not a Python String: key.char
We used the following key-press callback:
When we use str.format to print the pressed key, the key variable automatically returns a Python string to be printed (using a __str__ method). But if we had tried:
we would see TypeError: unsupported operand type(s) for +: ‘KeyCode’ and ‘str’ .
So, the key variable holds more than just a string identifying the key. However, that’s usually what we want. To access the str identifier for the pressed key, you can use the key.char attribute:
Note that in the first version of the code, we would see output like:
That’s because key.__str__ returns a nicely-formatted string identifying the key pressed, whereas key.char contains just the character associated with that key.
Unfortunately, key.char can only be called for keys that are associated with a character (like A , 5 , and = ). If key is referring to the Enter key, for example, then key simply does not have the attribute char .
Python has a built-in function called hasattr (“has attribute”) that checks whether a variable has a certain attribute, and it comes in handy here:
Responding To Individual Keys
Using key.char , we can respond to individual keypresses:
Some special keys, like esc , have special pynput variables associated with them:
Stop Listening to Me!
We can always stop a keyboard listener by returning False from a callback function:
Keyboard Control: pynput.keyboard.Controller
We can instruct pynput to press a key with the following:
Make sure to always release keys that you press using code!
The possibilities are great by just pressing character keys, but we can also press special keys:
Pressing a key once is no big deal, but code lets us iterate!
Even better? There is a type function that will type a whole string:
Controling and Listening
This code autocompletes a word!
Assignments
Describe what the following code does:
Make an autocomplete tool. Write some code so that when you type the first letter of your name, the rest of your name is typed out. If your name contains two copies of the letter it starts with, you will have difficulty, so pick another word!
- Try Making code that autocompletes only after you’ve typed the first TWO letters of your name!
Return to the previous lesson’s assignment 3 and mod some of the advanced games with continuous input!
pynput 1.7.6
This library allows you to control and monitor input devices.
Currently, mouse and keyboard input and monitoring are supported.
See here for the full documentation.
Controlling the mouse
Use pynput.mouse.Controller like this:
Monitoring the mouse
Use pynput.mouse.Listener like this:
A mouse listener is a threading.Thread , and all callbacks will be invoked from the thread.
Call pynput.mouse.Listener.stop from anywhere, raise StopException or return False from a callback to stop the listener.
When using the non-blocking version above, the current thread will continue executing. This might be necessary when integrating with other GUI frameworks that incorporate a main-loop, but when run from a script, this will cause the program to terminate immediately.
The mouse listener thread
The listener callbacks are invoked directly from an operating thread on some platforms, notably Windows.
This means that long running procedures and blocking operations should not be invoked from the callback, as this risks freezing input for all processes.
A possible workaround is to just dispatch incoming messages to a queue, and let a separate thread handle them.
Handling mouse listener errors
If a callback handler raises an exception, the listener will be stopped. Since callbacks run in a dedicated thread, the exceptions will not automatically be reraised.
To be notified about callback errors, call Thread.join on the listener instance:
Toggling event listening for the mouse listener
Once pynput.mouse.Listener.stop has been called, the listener cannot be restarted, since listeners are instances of threading.Thread .
If your application requires toggling listening events, you must either add an internal flag to ignore events when not required, or create a new listener when resuming listening.
Synchronous event listening for the mouse listener
To simplify scripting, synchronous event listening is supported through the utility class pynput.mouse.Events . This class supports reading single events in a non-blocking fashion, as well as iterating over all events.
To read a single event, use the following code:
To iterate over mouse events, use the following code:
Please note that the iterator method does not support non-blocking operation, so it will wait for at least one mouse event.
The events will be instances of the inner classes found in pynput.mouse.Events .
Ensuring consistent coordinates between listener and controller on Windows
Recent versions of _Windows_ support running legacy applications scaled when the system scaling has been increased beyond 100%. This allows old applications to scale, albeit with a blurry look, and avoids tiny, unusable user interfaces.
This scaling is unfortunately inconsistently applied to a mouse listener and a controller: the listener will receive physical coordinates, but the controller has to work with scaled coordinates.
This can be worked around by telling Windows that your application is DPI aware. This is a process global setting, so _pynput_ cannot do it automatically. Do enable DPI awareness, run the following code:
Controlling the keyboard
Use pynput.keyboard.Controller like this:
Monitoring the keyboard
Use pynput.keyboard.Listener like this:
A keyboard listener is a threading.Thread , and all callbacks will be invoked from the thread.
Call pynput.keyboard.Listener.stop from anywhere, raise StopException or return False from a callback to stop the listener.
The key parameter passed to callbacks is a pynput.keyboard.Key , for special keys, a pynput.keyboard.KeyCode for normal alphanumeric keys, or just None for unknown keys.
When using the non-blocking version above, the current thread will continue executing. This might be necessary when integrating with other GUI frameworks that incorporate a main-loop, but when run from a script, this will cause the program to terminate immediately.
The keyboard listener thread
The listener callbacks are invoked directly from an operating thread on some platforms, notably Windows.
This means that long running procedures and blocking operations should not be invoked from the callback, as this risks freezing input for all processes.
A possible workaround is to just dispatch incoming messages to a queue, and let a separate thread handle them.
Handling keyboard listener errors
If a callback handler raises an exception, the listener will be stopped. Since callbacks run in a dedicated thread, the exceptions will not automatically be reraised.
To be notified about callback errors, call Thread.join on the listener instance:
Toggling event listening for the keyboard listener
Once pynput.keyboard.Listener.stop has been called, the listener cannot be restarted, since listeners are instances of threading.Thread .
If your application requires toggling listening events, you must either add an internal flag to ignore events when not required, or create a new listener when resuming listening.
Synchronous event listening for the keyboard listener
To simplify scripting, synchronous event listening is supported through the utility class pynput.keyboard.Events . This class supports reading single events in a non-blocking fashion, as well as iterating over all events.
To read a single event, use the following code:
To iterate over keyboard events, use the following code:
Please note that the iterator method does not support non-blocking operation, so it will wait for at least one keyboard event.
The events will be instances of the inner classes found in pynput.keyboard.Events .
Global hotkeys
A common use case for keyboard monitors is reacting to global hotkeys. Since a listener does not maintain any state, hotkeys involving multiple keys must store this state somewhere.
pynput provides the class pynput.keyboard.HotKey for this purpose. It contains two methods to update the state, designed to be easily interoperable with a keyboard listener: pynput.keyboard.HotKey.press and pynput.keyboard.HotKey.release which can be directly passed as listener callbacks.
The intended usage is as follows:
This will create a hotkey, and then use a listener to update its state. Once all the specified keys are pressed simultaneously, on_activate will be invoked.