我想知道如何在python中按下键
我尝试过:
import msvcrt as keys
while True:
key = keys.getch()
if key == "a":
print("You have pressed a")
有谁知道如何修复它?
答案 0 :(得分:0)
这可能会对您有所帮助:
import msvcrt
while True:
if msvcrt.kbhit() and msvcrt.getch() == chr(97): # chr(97) = 'a'
print("You have pressed a")
注意:您的代码和我的代码在许多Python IDE中都不起作用!你需要执行python文件,例如在命令窗口中。