Here is an example that implements all that we learned in this chapter. It will check for two different types of keystrokes: the
KEYUP(a key is let go of) and the
KEYDOWN(a key is pressed). However, because
pygame.KEYDOWNis blocked, the application won't be able to detect a key being pressed. It will only print
"Up up up!"when the key is let go of.
import pygame pygame.init() flag = pygame.locals.RESIZABLE window = pygame.display.set_mode((500, 400), flag) pygame.event.set_blocked(pygame.KEYDOWN) run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: # if event is QUIT run = False if event.type == pygame.KEYUP: # if event is KEYUP print(“Up up up!”) If event.type == pygame.KEYDOWN: # this will never happen because KEYDOWN is blocked print(“Down down down!”) pygame.quit()
View code on Github