pygame event key pressed code example
Example 1: check key pressed pygame
import pygame
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
location -= 1
if event.key == pygame.K_RIGHT:
location += 1
Example 2: pygame key pressed once
pressed = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a and not pressed:
pressed = True
elif event.key != pygame.K_a:
pressed = False
Example 3: key pressed pygame
letters = {x: pygame.key.key_code(x) for x in "abcdefghijklmnopqrstuvwxyz"}
touche = pygame.key.get-pressed()
for (l, value) in letters.items() :
if touche[value] :
print(f"The letter {l} has been pressed ;)")