Pygame的Pygame.event.get()的键值在今天是一个荒谬的数字,为什么?

2024-05-19 08:37:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序曾经运行过下面的代码,当我按下左键时,它工作得非常好。但到今天为止,它突然停止工作,没有代码更改,没有重新安装,没有更新,因为它一直在工作并停止工作

#running on windows 10
while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            print(event)
            if event.key == 304:#left shift
                print("worked")

打印(事件)的输出过去是:

<Event(768-KeyDown {'unicode': '', 'key': 304, 'mod': 4096, 'scancode': 79, 'window': None})> now it is : <Event(768-KeyDown {'unicode': '', 'key': 1073742049, 'mod': 4096, 'scancode': 79, 'window': None})>

我知道如何修复它,或者用另一种方式,但是有人知道为什么'key'突然变成了一个可笑的数字吗?我的电脑有什么问题吗?它给出的键值搞乱了


Tags: key代码程序noneeventmodifunicode
2条回答

如果要检测是否按下了左,请使用pygame.K_LEFT

让我参考一下^{}模块的文档:

Portability note: The integers for key constants differ between pygame 1 and 2. Always use key constants (K_a) rather than integers directly (97) so that your key handling code works well on both pygame 1 and pygame 2.

通过^{}可以获得密钥的用户友好名称:

Get the descriptive name of the button from a keyboard button id constant.

键盘事件KEYDOWNKEYUP(请参见pygame.event模块)创建了一个带有附加属性的^{}对象。按下的键可以从key属性中获得。unicode属性提供键盘输入的Unicode表示

如果你不自己写密码的话会有帮助的。如果使用预定义的pygame密钥,效果会更好,例如:

while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            print(event)
            if event.key == pygame.K_LSHIFT:
                print("worked")

下面是pygame的预定义键列表

pygame documentation - pygame.key

相关问题 更多 >

    热门问题