Pygame Key events only detects a limited amount of keys being held down
Question: Hi I have used pygame (the modules for python) for a while. Now I have written
a RPG game that has multiple keys been held down at once. It seem that only 2
or 3 keys are detected whiles been held down. If anyone knows how to fix this
problem it would be great. Try out my code below for python 2.7 and see if you
have the same problem. Thanks

    
    
    import pygame
    
    def main():
        # Initialise screen
        pygame.init()
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((150, 50))
        pygame.display.set_caption('Basic Pygame program')
    
        # Fill background
        background = pygame.Surface(screen.get_size())
        background = background.convert()
        background.fill((250, 250, 250))
    
        # Display some text
        font = pygame.font.Font(None, 36)
        text = font.render("Hello There", 1, (10, 10, 10))
        textpos = text.get_rect()
        textpos.centerx = background.get_rect().centerx
        background.blit(text, textpos)
    
        # Blit everything to the screen
        screen.blit(background, (0, 0))
        pygame.display.flip()
        q=0
        w=0
        e=0
        r=0
    
        #Event loop
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q  : 
                    q = 1
                if event.key == pygame.K_w  : 
                    w = 1
                if event.key == pygame.K_e  : 
                    e = 1
                if event.key == pygame.K_r  : 
                    r = 1
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_q  : 
                    q = 0
                if event.key == pygame.K_w  : 
                    w = 0
                if event.key == pygame.K_e  : 
                    e = 0
                if event.key == pygame.K_r  : 
                    r = 0
            count = q+w+e+r
            print("Total: "+str(count)+"  q: "+str(q) + "   w: "+str(w)+ "   e: "+str(e)+ "   r: "+str(r))
            clock.tick(30)
            screen.blit(background, (0, 0))
            pygame.display.flip()
    if __name__ == '__main__': main()
    

Here I have tryed with pygame.key.get_pressed() but it still does not seem to
work with more than 3 keys being held down. )-:

    
    
    from pygame.locals import * 
    import pygame
    def main():
        # Initialise screen
        pygame.init()
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((150, 50))
        pygame.display.set_caption('Basic Pygame program')
    
        # Fill background
        background = pygame.Surface(screen.get_size())
        background = background.convert()
        background.fill((250, 250, 250))
    
        # Display some text
        font = pygame.font.Font(None, 36)
        text = font.render("Hello There", 1, (10, 10, 10))
        textpos = text.get_rect()
        textpos.centerx = background.get_rect().centerx
        background.blit(text, textpos)
    
        # Blit everything to the screen
        screen.blit(background, (0, 0))
        pygame.display.flip()
    
    
        #Event loop
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    return
            q=0
            w=0
            e=0
            r=0
            keys=pygame.key.get_pressed()
            if keys[K_q]  : 
                q = 1
            if keys[K_w]  : 
                w = 1
            if keys[K_e]  : 
                e = 1
            if keys[K_r]  : 
                r = 1
    
            count = q+w+e+r
            print("Total: "+str(count)+"  q: "+str(q) + "   w: "+str(w)+ "   e: "+str(e)+ "   r: "+str(r))
            clock.tick(30)
            screen.blit(background, (0, 0))
            pygame.display.flip()
    
    if __name__ == '__main__': main(
    

)


Answer: You can used 'get_pressed' which will give you a bool value for every key on
the keyboard.

<http://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed>

    
    
    keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            go_left()
    

