Last Updated: Mar 20, 2025
Easy

Getting Keyboard input in Pygame

Table of contents
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We know that the gaming industry is one of the major developing industries in the current world using current developing technologies. On the parallel side, Python is also one of the famous programming languages that are stretching its hands in all fields due to its simplicity and a full-fledged pack of libraries. Gaming is really a fun way of learning things. Game programming also includes many concepts such as math, computer programming, logic development, and many more. In the current developing technology, AI is also showing some support for game programming. Python can be used for Game Programming by using the Pygame library. Pygame library is one of the Python programming libraries which has very efficient functionalities such as building custom events, taking input from the user through the keyboard, etc. You can learn about how to add custom events in pygame using this link. This article will try to discuss how to take or get keyboard input in our pygame application. Hope You will enjoy the article!

To get Keyboard input

All the gamers will like the games that are more simple and more interactive. Simple in the sense that the game must be available on all devices in an easy fashion, And Interactive in the sense that the game is such that the involvement of the user in the game should be more like pressing the keyboard, moving through the mouse, etc.

To know whether a key is pressed in a game, we will use a method called pygame.event(), as whenever a key is pressed or released, this method will queue all the methods such as pygame.KEYDOWN, pygame.KEYUP, etc., respectively. Another important function is pygame.event.get(), which returns all the events that are happening in the game at the current moment.

So, we will make use of these methods to keep track of whether a key is pressed or not as follows:

for event in pygame.event.get():

         if event.type == pygame.KEYDOWN:

               print(“A key is pressed down”)
You can also try this code with Online Python Compiler
Run Code

Here, we are comparing each event type as to whether it is a keyboard input like KEYDOWN, KEYUP, etc., or not. If it is, then we will do some action on the game logic.

Here KEYDOWN, KEYUP, K_a, K_backspace, etc., are some of the predefined key types. There are a lot of key types which are available in their official documentation, or you can get them through this link

Each key type can be identified as an event.key type.

For example:

for an event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()

            sys.exit()

        # checking if keydown event happened or not

        if event.type == pygame.KEYDOWN:

            # checking if key "A" was pressed

            if event.key == pygame.K_a:

                print("Key A has been pressed")

            # checking if key "J" was pressed

            if event.key == pygame.K_j:

                print("Key J has been pressed")

            # checking if key "P" was pressed

            if event.key == pygame.K_p:

                print("Key P has been pressed")
You can also try this code with Online Python Compiler
Run Code

Here, the logic flow is as follows:

For every event in the get() queue, we will check the event.type for keydown event, etc., and we will check the event.key for every keyboard input type such as K_a, K_j, etc. If any check satisfies, we will write their corresponding logic for them.

In this way, we will take care of how to get keyboard input in the pygame application.

Frequently Asked Questions

What is Pygame?

Pygame is a Python programming language library that allows developers to develop applications on gaming themes. It is a well-developed library that has many useful functions to implement several functionalities of our application.

How do I see what happened in Pygame?

Pygame supports several different functionalities by providing several built-in methods. In order to see the functionality of the event, we have Pygame.event.get() method to know which events have happened by calling this function.

How is the keyboard input checked in Pygame?

In Pygame, every keyboard key is represented as K_keyboard_key_name. And these keys are of type event.key type. Every key of the keyboard is checked using the ‘==’ sign of each input given from the user keyboard.

Can we control the timing of the key pressed?

Yes, we can control what to do when a key is pressed and also what to do when a key is pressed for a particular duration of time using the method called ‘pygame.time.delay(delayTime)’, which is to be added before an event. And another important notice is that the delayTime must be in milliseconds.

What does event.get() method will do?

In Pygame, we have a method called pygame.event.get() method which is used to get all the events that are happening in our current application. It will return an event queue that contains all the pygame.event.Event objects.

Conclusion

In this article, we have extensively discussed how to get a keyboard input in Pygame.