简体   繁体   中英

Pygame only detects mouse events, does not detect KEYDOWN events

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()

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

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.

If you're working in a virtualenv, don't use the virtualenv command.

Use python3 -m venv .

Then install pygame ( eg pip3 install hg+http://bitbucket.org/pygame/pygame ).

See this thread for more details on this issue.

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

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

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

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

Firstly i doubt you are but pygame only registers inputs when your focused on the pygame screen so there's that. I don't have a direct answer to your question so sorry but i do have my solution or work around to it. Because i dislike the normal event system i use pygame.key.get_pressed() ( https://www.pygame.org/docs/ref/key.html ) just because i think it looks better and more readable. This is probably just a bad habit of mine though sooo.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM