虽然循环无法检测到msvcrt.getch()按键

时间:2018-12-07 00:31:26

标签: python python-3.x windows keypress msvcrt

import msvcrt
x: int = 0
while not msvcrt.getch() == ' ':
    if x <= 10000:
        print(x)
        x += x
    else:
        print("space")

按下“空格”时,循环不会停止。

2 个答案:

答案 0 :(得分:1)

msvcrt.getch()返回一个字节字符串而不是字符串,因此当您按空格键时将返回b' ',而不是' '

因此更改:

while not msvcrt.getch() == ' ':

收件人:

while not msvcrt.getch() == b' ':

答案 1 :(得分:0)

import msvcrt
x = 0
while not msvcrt.getch() == b' ':
    if x <= 10000:
        print(x)
        x += 1
    else:
        print("space")

感谢您的关注

相关问题