Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

(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
I'm using 3.2 Python – Vlad Mar 1 '13 at 16:51

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.

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 2 down vote accepted

Solved. Way to get correct non ASCII code :

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

Your Answer


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.