msvcrt getch暂停脚本,必须继续

问题描述 投票:1回答:2

PYTHON 3.3,msvcrt] >>
import sys, msvcrt
print("Please press a key to see its value")
while 1:
    key = msvcrt.getch()
    print("the key is")
    print(key)
    if ord(key) == 27: # key nr 27 is escape
        sys.exit()

这是我的代码,仅作为示例。该代码在到达key = msvcrt.getch()*, or *key = ord(getch())时暂停,在这里我使用了第一个。我想让此代码不断打印键是当我提供新的输入时(当我按下一个键时)而不仅仅是打印键是

所以打印输出看起来像这样:

the key is
the key is
the key is
the key is
the key is
the key is
77
the key is
the key is
the key is

如果您想制作类似[[snake

之类的东西,则不需要每次都想暂停getch的游戏,而您不想让它暂停,等待输入。PYTHON 3.3,msvcrt import sys,msvcrt print(“请按一个键以查看其值”),而1:key = msvcrt.getch()print(“ key is”)print(key)if ord(key) == 27:#键nr 27是...
python msvcrt
2个回答
1
投票
使用msvcrt.kbhit检查是否已按下键:

0
投票
另一个使Python程序停止在特定级别并等待用户按下Enter键的示例,并且/或者可以使用pygame生成“ No”的空格。作为示例,我使用Space表示“否”,但可以将touche Escape表示为“否”。您可能不需要一些导入的库。制作井字游戏时需要它们。
© www.soinside.com 2019 - 2024. All rights reserved.