How to create a Hotkey in Python?

24 Jan, 2021
This article is about How to create a HotKey Using Python. But first, let’s discuss what is a Hotkey. A Hotkey is an assigned key or sequence of keys programmed to execute a command or perform a selected task during a software application: For instance, on Windows computers, the hotkey Ctrl+S are often wont to quickly save a file.
By reducing such sequences to a few keystrokes, can often save the user time, hence “shortcut” and make computing a lot easier for people with disabilities.
Method 1: Using pynput (This library allows you to control and monitor input devices.)
Approach Used:
We import keyboard from pynputThen we create a set to keep track of which key inputs are currently pressedCreate a list of which Hotkey is needed to be pressed to perform the desired operation. Here we wanted the Hotkeys to be Shift+A and Shift+aWe create a function execute() that runs our desired program while pressing the Hotkey. Here we wished to print “Detected Hotkey”Create a function on_press() that checks that any key pressed in under the given that we have. If yes, we need to add to the set and then look if all keys and particular combinations are in the current set. If yes, then we execute our operation.Create a function on_release() that checks that any key released under the given combinations that we have. If yes, we need to remove it from the current set.At last, run the program.
We import keyboard from pynput
Then we create a set to keep track of which key inputs are currently pressed
Create a list of which Hotkey is needed to be pressed to perform the desired operation. Here we wanted the Hotkeys to be Shift+A and Shift+a
We create a function execute() that runs our desired program while pressing the Hotkey. Here we wished to print “Detected Hotkey”
Create a function on_press() that checks that any key pressed in under the given that we have. If yes, we need to add to the set and then look if all keys and particular combinations are in the current set. If yes, then we execute our operation.
Create a function on_release() that checks that any key released under the given combinations that we have. If yes, we need to remove it from the current set.
At last, run the program.
Python3
from pynput import keyboard  cmb = [{keyboard.Key.shift, keyboard.Key(char='a')},{keyboard.Key.shift, keyboard.Key(char='A')}]  current = set()  def execute():  print("Detected hotkey")  def on_press(key):  if any([key in z for z in cmb]):    current.add(key)    if any(all(k in current for k in z) for z in cmb):      execute()  def on_release(key):  if any([key in z for z in cmb]):    current.remove(key)  with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:  listener.join()
Output :
Method 2: Using keyboard (Refer to the Article: Keyboard module in Python)
Python provides a library named keyboard which is employed to urge full control of the keyboard. It’s a little Python library that may hook global events, register hotkeys, simulate key presses, and far more.
It helps to enter keys, record the keyboard activities and block the keys until a specified key is entered and simulate the keys.
It captures all keys, even onscreen keyboard events also are captured.
The keyboard module supports complex hotkeys.
Using this module we will listen and send keyboard events.,It works on both Windows and Linux operating systems.
Python3
# Keyboard module in Python import keyboard   # press ctrl+shift+z to print "Hotkey Detected" keyboard.add_hotkey('ctrl + shift + z', print, args =('Hotkey', 'Detected'))   keyboard.wait('esc') 
Output:
Hotkey Detected
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function