1

I am just starting to learn python for a project, which requires keyboard input. From what I've read, pygame is the best way to do this. Unfortunately I've been unable to get KEYDOWN to work. The only event pygame.event.get() returns is MOUSEMOTION. The code I need would go something like this:

import pygame
def main():
    pygame.init()
    while True:
        for event in pygame.event.get()
            if(event.type == pygame.KEYDOWN)
                 if(event.key==pygame.K_LEFT)
                     cry_because_it_worked()

I'm not sure what I'm doing wrong. I've read a few other questions about keyboard input, and all of them suggest using code that as far as I can tell is identical. For example, the first code block of the first answer to this question: How to get keyboard input in pygame? pygame.init() returns (6,0), which I believe means that all modules were initialized correctly. I'm using python 3.4.3 and pygame 1.9.2. Any help would be greatly appreciated.

Edit: Here is the actual loop in my code:

import pygame
...
def handleKeyInput(enigma):
    pygame.init()
    print("before loop")
    while True:

        for event in pygame.event.get():
            print(event.type) #Always prints 4, which is the value of MOUSEMOTION 
            print(event.type == pygame.KEYDOWN) #Always prints false
            if event.type == pygame.KEYDOWN:
                #and then a bunch of if statements for all letters
Community
  • 1
  • 1
PythonNoob
  • 11
  • 1
  • 4

1 Answers1

0

A correct code for using pygame.event.get would look like this:

import pygame
from pygame.locals import *

for event in pygame.event.get():
    if event.type == QUIT: # if closing application
        pygame.quit()
        sys.exit()
    elif event.type == KEYDOWN:
        if event.key == K_LEFT:
            cry_because_it_worked()

Pygame events are not only key presses. They can be mouse movements and other stuff as well. It is important to first check the event type first. This is why we put the following:

if event.type == KEYDOWN:

The next thing you need to do is to check the key that is being pressed. You do this by reading the key attribute of the event object:

if event.key == K_LEFT:

So in your program you check event.type to make sure the event is a key press, then you check event.key to see what key was pressed.

cweb
  • 382
  • 2
  • 11
  • Thanks for the response. When rewriting my code, I accidentally combined `if event.type == pygame.KEYDOWN` and `if event.key == K_LEFT`. Any idea why `pygame.event.get()` only returns `MOUSEMOTION`? – PythonNoob Mar 16 '15 at 01:57
  • If you have a return statement in your `event.get` loop, it will stop looping through the other events and only return the first event. For you the first event was probably a `MOUSEMOTION` event. – cweb Mar 16 '15 at 02:12
  • Would that be the only cause? Because I don't have any return statements anywhere near my loop. – PythonNoob Mar 16 '15 at 02:23
  • What code are you using to get `MOUSEMOTION` as an output. `pygame.event.get()` should return a list of event objects, not just one. Post you event loop and I will see if I can find what's wrong with it. – cweb Mar 16 '15 at 02:35
  • 1
    Did you focus your pygame window? If you have a pygame window that is not focused, it will log `KEYDOWN` events but will still log `MOUSEMOTION` events. Try clicking on your pygame window to focus it. – cweb Mar 16 '15 at 21:32