在python中检测按键?
我在 python中制作秒表类型程序,我想知道如何检测是否按下了一个键(例如p表示暂停,s表示停止),我不希望它像raw_input那样等待用户在继续执行之前的输入.任何人都知道如何在while循环中执行此操作?

另外,我想制作这个跨平台,但如果不可能,那么我的主要开发目标是linux

最佳答案
Python有一个具有许多功能的 keyboard模块.安装它,也许使用此命令:

pip3 install keyboard

然后在代码中使用它:

import keyboard  # using module keyboard
while 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('q'):  # if key 'q' is pressed 
            print('You Pressed A Key!')
            break  # finishing the loop
        else:
            pass
    except:
        break  # if user pressed a key other than the given key the loop will break
点击查看更多相关文章

转载注明原文:在python中检测按键? - 乐贴网