'Pygame. How do I catch the pressing of <pgup> and <pgdn> keys? [duplicate]

For some reason, the code below doesn't seem to work for me

running = True
while running:
for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.K_PAGEDOWN:
            # do something
        if event.type == pygame.K_PAGEUP:
            # do something


Solution 1:[1]

You look at event.type to check the type of the event and then if it is a KEYDOWN event you look at event.key to check which key has been pressed.

So what you are looking for is something like that:

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:   
            running = False             
        elif event.type = pygame.KEYDOWN:
            if event.key == pygame.K_PAGEDOWN:
                # do something
            elif event.key == pygame.K_PAGEUP:
                # do something

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 qouify