Pygame's pygame.event.get()'s key value is a ridiculous number as of today, why?

Viewed 345

My program used to run the following bit of code and it worked perfectly when i pressed left shift. But as of today it suddenly stopped working, no code changed, no reinstalls, no updates since it was working and stopped working.

#running on windows 10
while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            print(event)
            if event.key == 304:#left shift
                print("worked")

the output of print(event) used to be :

<Event(768-KeyDown {'unicode': '', 'key': 304, 'mod': 4096, 'scancode': 79, 'window': None})> now it is : <Event(768-KeyDown {'unicode': '', 'key': 1073742049, 'mod': 4096, 'scancode': 79, 'window': None})>

I know how to fix it, or do it in a different way, but does someone know the reason the 'key' is suddenly a ridiculous number? Is there something wrong with my computer that the key values it's giving is messed up?

2 Answers

It would help if you didn`t write the key codes on your own. It will be better if you use the predefined pygame key, for example:

while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            print(event)
            if event.key == pygame.K_LSHIFT:
                print("worked")

Here is a list of pygame`s predefined keys.

pygame documentation - pygame.key

Use pygame.K_LEFT if you want to detect if LEFT is pressed.

Let me refer to the documentation of the pygame.key module:

Portability note: The integers for key constants differ between pygame 1 and 2. Always use key constants (K_a) rather than integers directly (97) so that your key handling code works well on both pygame 1 and pygame 2.

A user friendly name of a key can be get by pygame.key.name():

Get the descriptive name of the button from a keyboard button id constant.

The keyboard events KEYDOWN and KEYUP (see pygame.event module) create a pygame.event.Event object with additional attributes. The key that was pressed can be obtained from the key attribute. The unicode attribute provides the Unicode representation of the keyboard input.

Related