1

As an example, when I try to input (through py.event.get()) the ^ (above the 6), the event reads the shift key, but not the six. The same is true for capital letters and @, #, $, %, *, (, and ). The code below is in a while loop. It works for all the keys that don't require a shift.

for event in py.event.get():
    if event.type == py.KEYDOWN:
        keys = py.key.get_pressed()
        if keys[py.K_CARET]:
            print("^")
        if event.type == py.K_CARET:
            print("^")

1 Answers1

0

See How to get keyboard input in pygame?

pygame.key.get_pressed() returns a list with the state of all keyboard buttons. This is not intended to get the key of a keyboard event. The key that was pressed can be obtained from the key attribute (not type) of the pygame.event.Event object. This also works for uppercase keys:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        print(event.key, event.unicode)

If this doesn't work for you, this is not a general problem, it is related to your system.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Wow! That does work. I'm not sure what I'm getting with event.key, but the event.unicode does seem to return what I want: Shift-a gives: 97 A a gives: 97 a Shift-6 gives: 54 ^. Not sure what the 97 and 54 represent. Also, I get 1073742049 each time. That is a mystery. But the event.unicode gives the keystroke I want. Thank you very much, you really know your stuff. – HENRY KOETHER Jan 31 '22 at 18:32
  • I should add, the long integer only prints when I use shift. If I type a lowercase letter, I don't get it. The number is a little different if I hit Alt or Ctrl. So I suspect it is a code for those keys. – HENRY KOETHER Jan 31 '22 at 18:38