#πŸ”’ locked inputs when using this code

41 messages Β· Page 1 of 1 (latest)

outer coyote
#
def get_player_pressed_keys():
    keys = pygame.key.get_pressed()
    return{
        "BlueUp": keys[pygame.K_UP],
        "BlueDown": keys[pygame.K_DOWN],
        "RedUp": keys[pygame.K_w],  
        "RedDown": keys[pygame.K_s]
    }
  • when pressing W and S at the same time arrowkey Up is locked.
  • when doing the same with arrow up and arrow down W is locked
    this should not be happening does anyone know how to fix this ?
sonic daggerBOT
#

@outer coyote

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

warm parrot
#

what do you mean by "locked"? Your code above lets me press both "Up" and "W" and both will be set to True as expected.

#

wait, I misread your post

outer coyote
#

it are some weird scenario's

warm parrot
#

nope, I can press all keys in pretty much any combination

outer coyote
#

for example when holding both W and up it stops like this

outer coyote
warm parrot
#

how are you using your function? perhaps there is an error in your logic?

outer coyote
#

for now just like this i do nothing else with it yet

warm parrot
#

that wouldn't give you the output above

outer coyote
#

yeah i was testing that one out via the normal even.type to see where it starts blocking

#
import pygame
from enum import Enum



class Direction(Enum):
    UP = 1
    DOWN = 2

class Paddle(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.position = pygame.Vector2(0, 0)
        
    def move(self, direction, speed):
        match direction:
            case Direction.UP:
                self.y -= speed
            case Direction.DOWN:
                self.y += speed
        ...

def get_player_pressed_keys():
    keys = pygame.key.get_pressed()
    return{
        "BlueUp": keys[pygame.K_UP],
        "BlueDown": keys[pygame.K_DOWN],
        "RedUp": keys[pygame.K_w],  
        "RedDown": keys[pygame.K_s]
    }

window = pygame.display.set_mode((800, 600))



pygame.display.update()


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
    keys = get_player_pressed_keys()
    print(keys)

    ...

pygame.quit()
exit()
warm parrot
#

and it's not setting all to True, if you e.g. press up,down,w,s at the same time?

outer coyote
#

jup it either stops at 2 trues or 3 trues but i cant get it further

warm parrot
#

odd. maybe try adding a clock.tick to it, is it perhaps too fast, and you just don't see everything?

#

it's what I did:

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    clock = pygame.time.Clock()

    while True:
        clock.tick(5)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

        keys = get_player_pressed_keys()
        k = list(filter(None, [
            keys["BlueUp"] and "Up",
            keys["BlueDown"] and "Down",
            keys["RedUp"] and "W",
            keys["RedDown"] and "S",
        ]))

        if k:
            print(*k)


if __name__ == '__main__':
    main()
outer coyote
#

nope its still doing the same thing

#

its rather strange

#

i never had this happen before on my desktop

#

but now that i switched to laptop i have this problem

warm parrot
#

maybe a a laptop thing indeed then. perhaps a hardware limit? is it a different OS?

outer coyote
#

no its windows 11 as well

warm parrot
#

and you got arrow keys on your laptop? (I don't πŸ˜„ )

#

no wait, I do

#

I was thinking of numpad

#
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            if event.type == pygame.KEYDOWN:
                print(event)
outer coyote
warm parrot
#

you could also try printing your keydown events in the event loop. does it stop printing new events as well?

outer coyote
#

that is holding them both down and then trying to press the up arrow

warm parrot
#

and no more events appear?

outer coyote
#

when i hold but arrows and s and try to press on w as well

warm parrot
#

well, that's odd

outer coyote
warm parrot
#

just tried it on my laptop, and it works. fine. though I'm on linux here

outer coyote
#

imma let a friend try it out tomorrow since he is also on laptop and hes working with me on the github repo

#

its just such a weird bug

sonic daggerBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.