0

I'm aware there are related questions to this topic. I have seen different ways of checking if user pressed particular (any) key, but some solutions seem too long or a bit difficult to modify. I tried pynput, pygame, etc..

My question is: what is the easiest way to check if user inputs particular key?

I want something like this (raw):

if key.pressed.right_arrow:
    print("right arrow")

if key.pressed.window:
    print("window")
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Scott
  • 4,974
  • 6
  • 35
  • 62

1 Answers1

0

Python has a keyboard module.

Here is an example how to use it:

import keyboard

keyboard.press_and_release('shift+s, space')

keyboard.write('The quick brown fox jumps over the lazy dog.')

keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))

# Blocks until you press esc.
keyboard.wait('esc')

# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')
# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@', 'my.long.email@example.com')

# Block forever, like `while True`.
keyboard.wait()
kunci115
  • 11
  • 6