我想制作一个程序,在该程序中,我按下某个热键,然后会检测到某个按键并告诉我按下了哪个键,但是每当我按下该快捷键时,程序都不会响应任何按键,即使是退出键和继续运行。
import keyboard
def dostuff():
print("Mew")
key = keyboard.read_key()
print('I have detected', type(key))
keyboard.add_hotkey('a', lambda: dostuff())
keyboard.wait('esc')
谁能告诉我什么问题?
答案 0 :(得分:0)
如果您不想使用键盘模块,则可以使用pynput,它具有相同的作用,
使用pip安装pynput
pip install pynput
这是代码:-
from pynput import keyboard
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener when esc key is pressed
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()