pythonsohard pythonsohard - 4 months ago 14
Python Question

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

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.

Answer

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.