我的代碼如下: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()
這個代碼是基於this question;我用它來熟悉getch
。
我注意到需要3次按鍵3次才輸出一次文本。爲什麼是這樣?我試圖用它作爲一個事件循環,這是一個滯後太多......
即使我鍵入3 不同鍵,它只輸出第三個按鍵。
我該如何強制它變得更快?有沒有更好的方法來實現我想要實現的目標?
謝謝!
evamvid
爲什麼它'var pressedKey = msvcrt.getch()'?不要以爲它是有效的Python語法 –
@ wizard23可能有他的javascript帽子,但他有正確的想法。只是刪除'var' – jdigital
我看到答案已經修復:) tnx!是的js滑入...我無法測試它,因爲我沒有在這裏的窗口,msvcrt是win32的具體;) – wizard23