1

How to print/detect the scancode of a pressed key in pygame?

If I do this:

for event in pygame.event.get():
    print event

It prints out:

<Event(2-KeyDown {'scancode': 1, 'key': 115, 'unicode': u's', 'mod': 0})>
<Event(3-KeyUp {'scancode': 1, 'key': 115, 'mod': 0})>

but how can I get it to print out the scancode part? e.g. prints out 1 when I press d.

I tried this:

for event in pygame.event.get():
    print event["scancode"]

which throws a error message because is not really a dictionary data structure.

martineau
  • 119,623
  • 25
  • 170
  • 301
pythonsohard
  • 127
  • 9
  • 3
    Did you look up the documentation for the `Event` object? It probably has a getter, or the field is public. – Carcigenicate Aug 07 '16 at 00:39
  • 2
    Have you tried: `event.scancode`? Based on: http://stackoverflow.com/a/16044380/6647217 – Munchhausen Aug 07 '16 at 00:49
  • It's a little hard to find, but here's the documentation for `pygame.KEYDOWN` and `pygame.KEYUP` events: http://www.pygame.org/docs/ref/key.html – martineau Aug 07 '16 at 02:53

1 Answers1

1

Have you tried event.scancode? The documentation indicates that key press events have the following attributes:

  • key is the integer ID
  • unicode is the UNICODE string for the single character
  • scancode is the platform specific key code

I'm guessing that mod is a bit-mask of OR-d bits.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113