Convert From ASCII W/O chr() Python

147 Views Asked by At

I am writing a program to get keyboard inputs. Here is the code:

key = 0
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    if event.type == pygame.KEYDOWN:
        for key in keys:
            if event.key == key:
                key = key
                if key != None:
                    print(key)
                    print(chr(key))
                    return chr(key)
                else:
                    return

The problem is that the key is too large in ASCII for chr() to handle. For example, when I press Caps Lock, it spits me an error. What I am trying to do is get the integer of the pygame.keydown and convert it to an ASCII character, then return that character. Is there any way, besides chr(), that won't give me the error?

ValueError: chr() arg not in range(0x110000)
1

There are 1 best solutions below

0
Rabbid76 On

A user friendly name of a key can be get by pygame.key.name():

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:

        print(pygame.key.name(event.key))