Skip to content

Instantly share code, notes, and snippets.

@brentvollebregt
Last active April 1, 2020 21:56
Show Gist options
  • Save brentvollebregt/8bae897739aed3b36f6b835fa3727237 to your computer and use it in GitHub Desktop.
Save brentvollebregt/8bae897739aed3b36f6b835fa3727237 to your computer and use it in GitHub Desktop.
Detect when keys are pressed to simulate a hotkey detection script
# from https://github.com/moses-palmer/pynput/issues/20
from pynput import keyboard
# The key combination to check
COMBINATIONS = [
{keyboard.Key.shift_r, keyboard.KeyCode(char='a')},
{keyboard.Key.shift_r, keyboard.KeyCode(char='A')}
]
# The currently active modifiers
current = set()
def on_press(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.add(key)
if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
print('All modifiers active!')
def on_release(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.remove(key)
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment