检查是否按下特定键的最简单方法



我知道这个话题有相关的问题。我见过检查用户是否按下特定(任何)键的不同方法,但有些解决方案似乎太长或有点难以修改。我试过pynput, pygame等。

我的问题是:什么是最简单的方法来检查用户是否输入特定的键?

我想要这样的东西(raw):

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

Python有一个键盘模块。

下面是一个如何使用它的例子:

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()

最新更新