1import pygame
2events = pygame.event.get()
3for event in events:
4 if event.type == pygame.KEYDOWN:
5 if event.key == pygame.K_LEFT:
6 location -= 1
7 if event.key == pygame.K_RIGHT:
8 location += 1
9
1# Outside loop
2pressed = False
3
4# Inside loop
5if event.type == pygame.KEYDOWN:
6 if event.key == pygame.K_a and not pressed: #K_a can be replaced by any key
7 # Do something
8 pressed = True
9 elif event.key != pygame.K_a:
10 pressed = False
1if pygame.mouse.get_pressed()[0]:
2 # do something when the left mouse button is pressed
3keys = pygame.key.get_pressed()
4if keys[pygame.K_a]:
5 # do something when the a key is pressed
1letters = {x: pygame.key.key_code(x) for x in "abcdefghijklmnopqrstuvwxyz"}
2
3touche = pygame.key.get-pressed()
4
5for (l, value) in letters.items() :
6 if touche[value] :
7 print(f"The letter {l} has been pressed ;)")