Re: [pygame] Problem with event-handling

2006-10-25 Thread Farai Aschwanden

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




Re: [pygame] Problem with event-handling

2006-10-25 Thread Kai Kuehne

Hi,

On 10/25/06, Farai Aschwanden [EMAIL PROTECTED] wrote:

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.


I know this, but I simply didn't want to use it that way.
The problem was that I pulled for ESC on the of the loop and later
I pulled for SPACE. Logically, the queue is always empty, when
I check whether SPACE is pressed.

I would be great if I had a key-system that works like, e.g.:
input.is_pressed(KEY, FUNCTION)

and internally it uses a list and calls the given function
every time the key is pressed. The internal things should be
hidden from the programmer. But I'm not that pro and have
no clue (yet) about this callback-function-thing.

Thanks :)
Kai


Re: [pygame] Problem with event-handling

2006-10-25 Thread Farai Aschwanden
Sorry, I didnt get the point  I dont know any Pygame funtion that gives you the char back that was pressed. I guess you have to program it out, means, you have to check for every single pressed key (what about "shift" + "a", etc.?). This can be very expensive inside a loop when checking ALL key inputs. So its definately smarter and faster to check only the ones you really want to check.In the Pygame doc they write:...Both events have a key attribute that is a integer id representing every key on the keyboardThats what it does but as far as I've check it the int value is not compatible to the ASCII values what would have made it easier to create a key check function. Maybe you use a kinda internal value translator between the returned event.key value and the ASCII table values. I've quickly checked key "a" and key "b" from event.key:a = 97b = 98They seem to fit the ASCII table list. That way you can directly return the int value converted to ascii value by: chr(event.key)By default the any key input  is checked in the event queue, so I dont see any reason why to use poll and pump. If the order of the pressed keys is important (what it normally does) you should use pygame.KEYDOWN and not pygame.key.get_pressedHope I could help this timeGreetingsFaraiAm 25.10.2006 um 23:55 schrieb Kai Kuehne:Hi,On 10/25/06, Farai Aschwanden [EMAIL PROTECTED] wrote: Hello KaiIf you only want to check keyboard inputs you dont need to use pumpand pull. Those are, as far as I understand the description, gettingANY running event from the event queue. So for checkin any key inputyou directly can use:  "if event.type == KEYDOWN". If then any keywas pressed you can check for the ones you are interested in. Thefollowing link of Pygame doc shows a list of all available keys youcan  check for. I know this, but I simply didn't want to use it that way.The problem was that I pulled for ESC on the of the loop and laterI pulled for SPACE. Logically, the queue is always empty, whenI check whether SPACE is pressed.I would be great if I had a key-system that works like, e.g.:input.is_pressed(KEY, FUNCTION)and internally it uses a list and calls the given functionevery time the key is pressed. The internal things should behidden from the programmer. But I'm not that pro and haveno clue (yet) about this callback-function-thing.Thanks :)Kai