Search code examples
pythoninputpygamekeyboardkey

Pygame keyboard input doesn't works - Python


Can someone help me ? When i press - A - on the keyboard , nothing happen

import pygame
pygame.init()

pygame.display.set_caption("PyGame")
pygame.display.set_mode((640,480))


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

Solution

  • That should work:

    #!/usr/bin/env python3
    
    import pygame
    import sys
    
    
    pygame.display.set_caption("PyGame")
    pygame.display.set_mode((640, 480))
    pygame.init()
    
    while (True):
        for event in pygame.event.get():
             if event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_a):
                    pygame.quit()
                    sys.exit(0)