5
\$\begingroup\$

When I press a keyboard button on a keyboard with a Russian layout, my application pygame game crashes with this message:

UnicodeEncodeError: 'ascii' codec can't encode character '\u0444' in position 40: ordinal not in range(128)

This is my code:

 event = pygame.event.poll()
 if event.type == KEYDOWN:
    print(event)

(I pressed the latin 'a' key.)

Pressing keys in the English layout works fine.

I tried changing the event.type == KEYDOWN to event.type == KEYUP: Everything works, but then I cannot access the event.unicode field to get the pressed key.

How do I get KEYDOWN events to work with any keyboard layout?

Python version 3.2

\$\endgroup\$
  • \$\begingroup\$ This general Python 2.7 resource for working with Unicode might have something useful. Have you tried casting the problematic value to the unicode string type with unicode(variable_goes_here)? \$\endgroup\$ – Anko Feb 28 '13 at 23:39
  • 1
    \$\begingroup\$ I'm using 3.2 Python \$\endgroup\$ – Vlad Mar 1 '13 at 16:51
4
\$\begingroup\$

The program is trying to convert a unicode wide character format into a standard ascii format. The code you are trying to convert is out of the available ASCII format range.

http://www.fileformat.info/info/unicode/char/444/index.htm

This is the code you are trying to convert, and ASCII only supports 128 different values, with extended ASCII supporting 256.

edit you need to use functions that support unicode characters, instead of standard or wide ASCII characters.

\$\endgroup\$
2
\$\begingroup\$

Solved. Way to get correct non ASCII code :

event = pygame.event.poll()
if event.type == KEYDOWN:
   print(event.unicode)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.