Advertisement
tabnation

python hotkeys

Jan 20th, 2022
1,717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #pip install pynput
  2.  
  3. import time
  4. from pynput import keyboard
  5.  
  6. # The key combination to check
  7. COMBINATIONS = [
  8. {keyboard.KeyCode(char='s')},
  9. {keyboard.Key.shift, keyboard.KeyCode(char='A')}
  10. ]
  11.  
  12. # The currently active modifiers
  13. current = set()
  14.  
  15.  
  16. def execute():
  17. print ("Do Something")
  18. time.sleep(5)
  19.  
  20.  
  21. def on_press(key):
  22. if any([key in COMBO for COMBO in COMBINATIONS]):
  23. current.add(key)
  24. if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
  25. execute()
  26.  
  27. def on_release(key):
  28. if any([key in COMBO for COMBO in COMBINATIONS]):
  29. current.remove(key)
  30.  
  31. with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
  32. listener.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement