[SOLVED] Why every key on keyboard in event.type in pygame has same id as the rest of keyboard

Issue

This Content is from Stack Overflow. Question asked by NaQuCode

I’m trying to check if keyUp is pressed by event.loop in pygame, but when i click any other button on keyboard i get the same output with print(event.type) that every key has same id as pygame.KEYUP
Here is the code:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYUP: print("key up")



Solution

KEYUP down does not address the "up" key. It is the event that occurs when a key is released. You must check that the event type is KEYDOWN (key pressed) and the key is K_UP:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
            print("key up")

Also see How to get keyboard input in pygame?.


This Question was asked in StackOverflow by NaQu__ and Answered by Rabbid76 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?