Skip to main content
Engineering LibreTexts

8.22: Waiting for a Key Press Event with the checkForKeyPress() Function

  • Page ID
  • def checkForKeyPress():
        # Go through event queue looking for a KEYUP event.
        # Grab KEYDOWN events to remove them from the event queue.
        for event in pygame.event.get([KEYDOWN, KEYUP]):
            if event.type == KEYDOWN:
            return event.key
        return None

    The checkForKeyPress() function works almost the same as it did in the Wormy game. First it calls checkForQuit() to handle any QUIT events (or KEYUP events specifically for the Esc key) and terminates the program if there are any. Then it pulls out all the KEYUP and KEYDOWN events from the event queue. It ignores any KEYDOWN events (KEYDOWN was specified to pygame.event.get() only to clear those events out of the event queue).

    If there were no KEYUP events in the event queue, then the function returns None.

    • Was this article helpful?