[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] key press question



Frank Raiser wrote:

> while 1:
>     event = pygame.event.wait()
>     if event.type == KEYDOWN:
>         if pygame.key.get_mods() & KMOD_LALT: print "meta down"
>         else: print "just down"
> 
> I don't know about the interals of pygame very well.. maybe Pete can
> lighten it up a little.. how does key.get_mods work? 


pygame.key.get_mods() works a lot like the get_pressed(), it just 
returns the state of all the modifiers in a bit-packed integer, instead 
of a list of values. your use of the bitmask KMOD_LALT is correct above.

as far as the amount of processing used goes, i'd "guess" the function 
get_mods() would be a little quicker, but i don't really imaging there 
would be any difference. (especially considering the following 
information...)

and now for the "it's even easier than that" department!

the KEYDOWN events come with a "mod" attribute. this is the similar to 
the get_mods() function in pygame.key. the benefit is it represents the 
state of the modifiers when the KEYDOWN actually happened (not when you 
get around to processing the event) :]

the above code can be reduced to this...

	while 1:
		event = pygame.event.wait()
		if event.type == KEYDOWN:
			if event.mod & KMOD_LALT: print "meta"
			else: print "no meta"

wheee!
find this information (and more) available in the classy pygame docs,
http://www.pygame.org/docs/ref/pygame_event.html




____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org