[Python] Getting pygame key constant ea K_UP from an event

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to pass the event to another function and I need the key constant as a string. Is there a way to get it?
    pygame.key.name(ev.key) just gives me Up instead of K_UP
    I did try pygame.key.key_code(pygame.key.name(ev.key)) but looks like sdl2 doesn't have that function.


    Here's a very valid use case for it:
    I already have this function that gets the pressed button's key and does something with it, and it accepts a string. Since It's already working, I wanted to keep it simple and use the same function.

    def input(self, key):
    for i in self.moves:
    if key in i.keys:
    i.change("_correct")
    else:
    i.change("_wrong")
    self.next()
    return


    And I didn't come on saying: There should be a function to do this or that, I just asked if there's a way like getting the value from some dictionary that already exists.

    Continue reading...

Compartilhe esta Página