Pygame 只检测鼠标事件,不检测 KEYDOWN 事件 [英] Pygame only detects mouse events, does not detect KEYDOWN events

查看:63
本文介绍了Pygame 只检测鼠标事件,不检测 KEYDOWN 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下简单程序,该程序应该打印出 pygame.event.get() 检测到的所有事件.

I have written the following simple program which should print out all events detected by pygame.event.get().

import pygame, sys
from pygame.locals import *

display = pygame.display.set_mode((300, 300))
pygame.init()

while True:
    for event in pygame.event.get():
        print(event)
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

但是当我运行它时,我只有鼠标事件,当我按下大写锁定两次时,我只有鼠标事件和 KEYDOWN 和 KEYUP 事件,在终端中打印.当我使用任何其他键时,它们只会打印到终端,就像我在终端窗口中书写一样.

But when I run this I only have mouse events, and a KEYDOWN and KEYUP event when I hit caps-lock twice, being printed in terminal. When I use any other keys they only print to terminal as if I was writing in the terminal window.

<Event(4-MouseMotion {'pos': (102, 15), 'buttons': (0, 0, 0),
 'rel': (-197, -284)})>
<Event(2-KeyDown {'unicode': '', 'scancode': 0, 'key': 301, 'm
 od': 8192})>
<Event(3-KeyUp {'key': 301, 'scancode': 0, 'mod': 0})>
wasd

我使用的是 Mac OSX 10.12.1、python 3.5.2 和 pygame 1.9.4.dev0.

I am using Mac OSX 10.12.1, python 3.5.2, and pygame 1.9.4.dev0.

我想我错过了一些直接的东西,但我在网上找不到类似的东西.任何帮助将非常感激.

I assume I'm missing something straight forward, but I found nothing similar online. Any help would be much appreciated.

推荐答案

对于仍在为此苦苦挣扎的任何人,该问题已在 git 上记录并已修复.https://github.com/pygame/pygame/issues/203

For anyone still struggling with this, the issue is documented here on git and is fixed. https://github.com/pygame/pygame/issues/203

只需从您的 venv 中卸载 pygame,然后安装以下版本.

Just uninstall pygame from your venv, then install below version.

pip install -U https://github.com/pygame/pygame/archive/master.zip

刚刚试过了,终于可以在pygame中使用关键事件了.

Just tried this and can finally use key events in pygame.

这篇关于Pygame 只检测鼠标事件,不检测 KEYDOWN 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆