Python有一个具有许多功能的 键盘
模块。安装它,也许使用以下命令:
pip3 install keyboard
然后在如下代码中使用它:
import keyboard #Using module keyboardwhile True:#making a loop try: #used try so that if user pressed other than the given key error will not be shown if keyboard.is_pressed('a'): #if key 'a' is pressed print('You Pressed A Key!') break #finishing the loop else: pass except: break #if user pressed other than the given key the loop will break您可以设置多个按键检测:
if keyboard.is_pressed('a') or keyboard.is_pressed('b') or keyboard.is_pressed('c'): #then do this


