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\$
2
  • \$\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
    Commented Feb 28, 2013 at 23:39
  • 1
    \$\begingroup\$ I'm using 3.2 Python \$\endgroup\$
    – Vlad
    Commented Mar 1, 2013 at 16:51

2 Answers 2

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\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .