muyustan
muyustan

Reputation: 1585

pygame strange behavior on key pressings

I am kind of new to pygame module.

I am keeping track of the key pressings from the terminal via PyCharm IDE.

To clearness, I am adding screenshot of the workspace of mine below.

enter image description here

Now, the problem is, I found out that, If I am pressing and holding DOWN and UP keys at the same time, the system can detect RIGHT key pressings, however ignorant to LEFT key pressings.

Same thing is valid for holding W and S keys together and sensing T key but not sensing E or Q keys.

RIGHT & LEFT being hold, senses DOWN does not sense UP

I am adding a fully ready to run code(except you need pygame module installed) for ones who might want to try on their computer.

import pygame
#import time

pygame.init()

display_width = 800
display_height = 600

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

#carImg = pygame.image.load('raceCar.png')


# def drawCar(x, y):
#    gameDisplay.blit(carImg, (x, y))


x = display_width * 0.45
y = display_height * 0.6
dx = 0
dy = 0

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("MY GAME")

clock = pygame.time.Clock()

crashed = False
Quit = False

i = 0

while not crashed and not Quit:



    for event in pygame.event.get():

        # if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        #     Quit = True
        #
        # elif event.type == pygame.KEYDOWN:
        #
        #     if event.key == pygame.K_DOWN:
        #         dy += 5
        #     elif event.key == pygame.K_UP:
        #         dy += -5
        #     elif event.key == pygame.K_RIGHT:
        #         dx += 5
        #     elif event.key == pygame.K_LEFT:
        #         dx += -5
        #
        # elif event.type == pygame.KEYUP:
        #
        #     if event.key == pygame.K_DOWN:
        #         dy += -5
        #     elif event.key == pygame.K_UP:
        #         dy += 5
        #     elif event.key == pygame.K_RIGHT:
        #         dx += -5
        #     elif event.key == pygame.K_LEFT:
        #         dx += 5
        #
        # elif event.type == pygame.QUIT:
        #     Quit = True

        i += 1
        print(i, event)

    # x = (x + 1) % display_width
    # y = (y - 1) % display_height
    x += dx
    y += dy
    gameDisplay.fill(white)
    x = x if x <= display_width else -163
    x = x if x+163 >= 0 else display_width
    y = y if y <= display_height else -244
    y = y if y + 244 >= 0 else display_height
    # drawCar(x, y)
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

Note that, I have commented out unnecessary parts in order to make it ready to run for anyone easily.

Upvotes: 1

Views: 166

Answers (3)

Mark_Parker
Mark_Parker

Reputation: 1

I am not sure if this can solve your problem but its worth a shot. I was having a similar issue where some of my keys would not be detected if they were pressed at the same time. I changed all the elif and else statements in my code to if statements which fixed the problem. Logically, it makes sense since you are telling the computer that you only want to check if the key is pressed ONLY IF (which is what elif means) the if statement above is not true.

Your code will look something like this:

for event in pygame.event.get():

     if event.type == pygame.KEYDOWN and event.key == 
     pygame.K_ESCAPE:
         Quit = True
    
     if event.type == pygame.KEYDOWN:

         if event.key == pygame.K_DOWN:
             dy += 5
         if event.key == pygame.K_UP:
             dy += -5
         if event.key == pygame.K_RIGHT:
             dx += 5
         if event.key == pygame.K_LEFT:
             dx += -5
    
     if event.type == pygame.KEYUP:
    
         if event.key == pygame.K_DOWN:
             dy += -5
         if event.key == pygame.K_UP:
             dy += 5
         if event.key == pygame.K_RIGHT:
             dx += -5
         if event.key == pygame.K_LEFT:
             dx += 5

         if event.type == pygame.QUIT:
            Quit = True  
   

However, it may need some other adjustments. For instance, you might need to change the "if" in the string that checks for pygame.KEYUP to an "elif." Hope this helps.

edit: just realized this post is from a while ago, oh well.....

Upvotes: 0

Kingsley
Kingsley

Reputation: 14906

I suspect it's the way your code is handling key-presses. It's better to use the pygame.key.get_pressed(), which returns a dictionary of the current state of all keys pressed at that instant. But the presented code (the commented out section) does not seem to have any issues that would cause this.

Below is some example code that demonstrates handling multiple key-presses. You can also use it to ensure your keyboard handles multiple presses correctly. I would expect every keyboard can handle a minimum of 3 simultaneous key-presses, since otherwise Ctrl-Alt-Del would be impossible.

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   =  50
WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
DARK_BLUE     = (   3,   5,  54)
WHIPPED_CREAM = ( 251, 252, 214 )

### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Multi Keys Test")

# We need to write some stuff
default_font = pygame.font.SysFont(None, 40)

### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Make a string of which arrow-keys are pressed
    # EDIT: Removing the loop, as it seems to be causing some minor confusion  
    currently_pressed = ""
    keys = pygame.key.get_pressed()
    if ( keys[ pygame.K_UP ] ):
        currently_pressed += "up "
    if ( keys[ pygame.K_DOWN ] ):
        currently_pressed += "down "
    if ( keys[ pygame.K_LEFT ] ):
        currently_pressed += "left "
    if ( keys[ pygame.K_RIGHT ] ):
        currently_pressed += "right "
    keys_text = default_font.render( currently_pressed, True, WHIPPED_CREAM )

    # Update the window, but not more than 60fps
    window.fill( DARK_BLUE )
    window.blit( keys_text, ( 10, 10 ) )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()

For what it's worth, I can get it to show "left right up down" all together.

Upvotes: 1

This is most likely the result of hardware limitations. It's called keyboard ghosting and happens to most cheap commercial keyboards

Upvotes: 0

Related Questions