Python Windows`msvcrt.getch()`仅检测到每3次按键? [英] Python Windows `msvcrt.getch()` only detects every 3rd keypress?
本文介绍了Python Windows`msvcrt.getch()`仅检测到每3次按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下:
import msvcrt
while True:
if msvcrt.getch() == 'q':
print "Q was pressed"
elif msvcrt.getch() == 'x':
sys.exit()
else:
print "Key Pressed:" + str(msvcrt.getch()
此代码基于此问题;我用它来熟悉getch
.
This code is based on this question; I was using it to acquaint myself with getch
.
我已经注意到,按3次3键需要一次输出文本.为什么是这样?我正在尝试将其用作事件循环,这太滞后了...
I've noticed that it takes 3 pressing the key 3 times to output the text once. Why is this? I'm trying to use it as an event loop, and that's too much of a lag...
即使我输入3个不同键,它也只会输出第3个按键.
Even if I type 3 different keys, it only outputs the 3rd keypress.
如何迫使它运行得更快?有没有更好的方法来实现我要实现的目标?
How can I force it to go faster? Is there a better way to achieve what I'm trying to achieve?
谢谢!
逃避
推荐答案
,您在循环中调用了3次该函数.尝试仅这样调用一次:
you call the function 3 times in your loop. try calling it only once like this:
import msvcrt
while True:
pressedKey = msvcrt.getch()
if pressedKey == 'q':
print "Q was pressed"
elif pressedKey == 'x':
sys.exit()
else:
print "Key Pressed:" + str(pressedKey)
这篇关于Python Windows`msvcrt.getch()`仅检测到每3次按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文