Open In App

How to create a Hotkey in Python?

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 is often used 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:

  1. We import keyboard from pynput
  2. Then we create a set to keep track of which key inputs are currently pressed
  3. 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
  4. We create a function execute() that runs our desired program while pressing the Hotkey. Here we wished to print “Detected Hotkey”
  5. 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.
  6. 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.
  7. 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads