Hi guys!

I am in the process of making a game...similar to this one:

I am using the following code to detect key presses:

key = pygame.key.get_pressed()

if key[pygame.K_LEFT]:

How can I detect if a key is released? I have seen this done with using 'event' instead of 'key.get_pressed()', but I don't want to switch to event because I have had many previous problems with it.

Is there a way to detect if a key is released, or even just not pressed, using the 'key.get_pressed()' method of doing things? If not, then I will switch to event, but it would save me loads of time.

Thanks for your time

6 Years
Discussion Span
Last Post by tbone2sk

Simple example taken from pygame.org

for event in pygame.event.get() :
  if event.type == pygame.KEYDOWN :
    if event.key == pygame.K_SPACE :
      print "Space bar pressed down."
    elif event.key == pygame.K_ESCAPE :
      print "Escape key pressed down."
  elif event.type == pygame.KEYUP :
    if event.key == pygame.K_SPACE :
      print "Space bar released."
    elif event.key == pygame.K_ESCAPE :
      print "Escape key released."

I normally use this function to deal with all the keyboard inputs.

def keyPressed(inputKey):
    keysPressed = pygame.key.get_pressed()
    if keysPressed[inputKey]:
        return True
        return False

Okay. Thanks. I wanted to steer away from 'event' but it looks like I'll have too...


Wow. Well, what can I say?
I put in the code for the keypress, thinking that might not register if the key was held, and it works fine. What I wanted was for the ball in my game (see first post) to be passed left/right, but if you held the left/right arrow key, nothing happens.

Thanks very much!!!


New problem.
I am not using the sprite class for my images. I want to create my own collision function.

Here is the code for the ball colliding with each player

if ballx < player5x + 33 and ballx > player5x and bally < player5y + 25 and bally > player5y:
        playerWBall = 5

I use playerWBall as the variable for drawing a different set of images in my draw function. This code does not work. I tried just putting a print command in, but that didn't work. I think my bounding box is wrong.

This is the code for the ball going out. This works:

if ballx < 0 or ballx > 640:
        balloutbounds = 1
        ballx = 320

I got confuzzled about the y value being upside down (?) and the balls y position was going under the bounding box, I the box was in front of the player, not on them!


Making the ball a sprite class, it will make your life a lot easier. Remember that collision is based on the rect of an object, not the image. If you want to make a rect smaller than the image in a sprite that is easy to do.

Just a starting point.

class Ball(pygame.sprite.Sprite):

    def __init__(self):
        self.image, self.rect = load_image('ball.png',-1)
        """Set the number of Pixels to move each time"""
        self.x_dist = 5
        self.y_dist = 5 
    def move(self, pos):
	xMove = 0;
        yMove = 0;
        if (pos == "right"):
            xMove = self.x_dist
        elif (pos == "left"):
            xMove = -self.x_dist
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.