How to Make Hotkeys in Python

This demonstrates how to make a script to detect combinations of keypresses or even single keys pressed in Python. The script will use pynput to detect keypresses and will work on windows, mac and linux.

PIP

Installing Pynput

We will be using the punput 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.

Creating the Script

First import keyboard from pynput and create a variable called current and set it to a set object to track what keys are pressed currently.

frompynputimportkeyboard# The currently active modifierscurrent=set()

Next we will setup a listener for the keyboard and set on press and release methods to it. Setup the listener in a with statement and join the listener thread to the main thread using .join().

Next we need to the the combinations to look out for. I this example I will use Shift + A. I will need to use two variants, Shift + A and Shift + a so the combination will be detected no matter what order they are pressed. If A is pressed first, 'a' will be given to the listener but if shift is pressed before A then 'A' will be given to the listener. Make a list called COMBINATIONS and add the keys in sets as shown below.

# The key combination to checkCOMBINATIONS=[{keyboard.Key.shift,keyboard.KeyCode(char='a')},{keyboard.Key.shift,keyboard.KeyCode(char='A')}]

Now create a method called execute. This is what will be executed when a combination is detected. You can put anything in here but for the demonstration I will print a simple string.

defexecute():print("Do Something")

Now we will edit the on_press method. We need to first check if the key that has just been pressed is in any of the combinations that we have. If it is we need to add it to the current key set and then loop though all the combinations checking if all the keys in a particular combination are in the 'current' set. If one of the combinations has all their keys down, we need to call execute().

You can now run the script and when Shift and 'A' are pressed at the same time, the sting will be printed to the console.

If you want to call another script, do it in execute() by doing something like an os.system call on your script or importing it and then calling it.

This script has been modified from the example given by Moses Palmer on issue 20 of pynput

Adding Hotkeys

To add more hotkeys or different hotkeys you will need to add another set of keys to the COMBINATIONS list. To do this use curly braces and make sure to separate the keys by commas. Also to make sure to split each set by a comma or you will be given an error when running the script.

To use keys like shift and control, you need to provide the key. Keys can be found in the documentation. In this we can see that if we want to use shift, we need to use keyboard.Key.shift. Another example is the scroll lock: keyboard.Key.scroll_lock. To use characters on the keyboard like 'a', 'b', 'c' ect... you will need to use the method keyboard.KeyCode() passing the character as the char parameter. Examples follow below.

You can have as many keys as you like in one combination and accidentally adding two of the same combination will not cause any errors.

Final Script

frompynputimportkeyboard# The key combination to checkCOMBINATIONS=[{keyboard.Key.shift,keyboard.KeyCode(char='a')},{keyboard.Key.shift,keyboard.KeyCode(char='A')}]# The currently active modifierscurrent=set()defexecute():print("Do Something")defon_press(key):ifany([keyinCOMBOforCOMBOinCOMBINATIONS]):current.add(key)ifany(all(kincurrentforkinCOMBO)forCOMBOinCOMBINATIONS):execute()defon_release(key):ifany([keyinCOMBOforCOMBOinCOMBINATIONS]):current.remove(key)withkeyboard.Listener(on_press=on_press,on_release=on_release)aslistener:listener.join()

Common Issues and Questions

How can I add different hotkeys for different functions?

I have not developed this myself yet so it will be something you need to think about. The best way to do this would be to have two lists, one of combinations and one of definitions for each combination. Test each of the combinations on a keypress and if all the keys are pressed, execute the definition in the same index in the definition list.

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 these 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.