Last active
April 1, 2020 21:56
-
-
Save brentvollebregt/8bae897739aed3b36f6b835fa3727237 to your computer and use it in GitHub Desktop.
Detect when keys are pressed to simulate a hotkey detection script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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