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

Re: [pygame] Odd key code behaviour



On Fri, 25 Jan 2002, Magnus Lie Hetland wrote:

> I just had a lot of trouble with K_UP... K_TAB worked just fine, but
> not K_UP. Then I started to suspect that the trouble was that I was
> using "event.key is K_UP" and not "event.key == K_UP". The latter is
> safer, of course, but I assumed that K_UP had been used in generating
> the event... After all, "event.key is K_TAB" worked just fine.


As a guess, it might have to do with the actual numbers involved.  I took
a quick glance at the SDL headers:

###
[dyoo@tesuque include]$ grep K_UP *
SDL_keysym.h:	SDLK_UP			= 273,
[dyoo@tesuque include]$ grep K_TAB *
SDL_keysym.h:	SDLK_TAB		= 9,
###


Python automatically precaches all integers from -1 to 100, but anything
beyond that isn't cached.  This may be why 'is' may not work for K_UP,
because that value's out of range of the integer cache.

Here's an example that shows a little of this behavior:

###
>>> 100 + 100 is 200
0
>>> 40 + 40 is 80
1
###

The best approach is probably to do an '==' comparsion.  Hope this helps!

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