[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Keyboard Problem



try this:

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    doWhatYouWant()

In you main loop, this way yyou process all the event queue... this is good way to do it normally....

This do internally a pump, so you don't need to call it manually

Anyway, from documentation:

Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed - get the state of all keyboard buttons. There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality.


2010/1/7 <don@xxxxxxxxxxxxxxxxx>
Hi,

I believe you need to pump the pygame event system for the keyboard
(and other parts of pygame) to work properly.
see http://www.pygame.org/docs/ref/event.html#pygame.event.pump

hope that helps.
yours
//Lorenz


On Thu, 07 Jan 2010 03:05:41 -0500, Kris Schnee <kschnee@xxxxxxxxxx>
wrote:
> I'm having a problem with pygame.key.get_pressed(). Any ideas? Sample
code:
>
> <code>
>
> import pygame
> screen = pygame.display.set_mode((100,100))
> pygame.init()
>
> def Go():
>      while True:
>          keys = pygame.key.get_pressed()
>          if keys[ pygame.K_SPACE ]:
>              return
>          pygame.display.update()
>
> Go()
>
> </code>
>
> Result: Program doesn't react to hitting Space, and won't quit till
forced.



--
Nota: Tildes omitidas para evitar incompatibilidades.

:wq