[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Problem with event-handling
Hello Kai
If you only want to check keyboard inputs you dont need to use pump
and pull. Those are, as far as I understand the description, getting
ANY running event from the event queue. So for checkin any key input
you directly can use: "if event.type == KEYDOWN". If then any key
was pressed you can check for the ones you are interested in. The
following link of Pygame doc shows a list of all available keys you
can check for.
Here is a code that works in the way I described.
-------------------------------------------------------------------
import sys, pygame
from pygame.locals import *
pygame.init()
while True:
for event in pygame.event.get():
if (event.type == KEYDOWN): # Key was pressed
# Now checking which key was pressed (the ones the event
should care of)
if event.key == K_UP:
# Cursor up was pressed (include further actions/calls
here)
print "Cursor key up pressed"
if event.key == K_DOWN:
# Cursor up was pressed (include further actions/calls
here)
print "Cursor key down pressed"
if event.key == K_ESCAPE:
# Exit program
sys.exit()
-------------------------------------------------------------------
Hope this helps
Grüsschen
Farai
Am 25.10.2006 um 22:34 schrieb Kai Kuehne:
Hi list!
I wanted to create a function which get a key and returns
true/false whether the key is currently pressed:
def is_pressed(key):
pygame.event.pump()
event = pygame.event.poll()
if event.type == KEYDOWN:
if event.key == key:
return True
return False
When I use this function: is_pressed(K_SPACE), it doesn't work.
The variable "key" always contains 27 (ESC). Can anybody
point me to my mistake(s)?
Thanks
Kai