Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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)? –  Anko Feb 28 '13 at 23:39
1  
I'm using 3.2 Python –  Vlad Mar 1 '13 at 16:51

2 Answers 2

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.

share|improve this answer
up vote 1 down vote accepted

Solved. Way to get correct non ASCII code :

event = pygame.event.poll()
if event.type == KEYDOWN:
   print(event.unicode)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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