Well, I needed to do acrobatics to get this to work. Therefore, first I recommend using the scancode key , which you can get from event.scancode . Each key has a unique code that refers to a physical key on the keyboard, and this is the same scancode, regardless of the layout of the dvorak keyboard or us. Then in the keydown event, you will have an attribute called unicode, which is a pressed character that matches the keyboard layout used. Thus, pressing the d key on the user's layout will lead you to the unicode d, on dvorak this physical key will give you the e character, and this will be correctly reflected in event.unicode . Here, where it gets a little annoying. It seems that the unicode attribute is only available in the keydown event, not in the keyup event. So I just created a dictionary called keymap that tracks this information like a scancode display for a Unicode character. In the code example below, the character you pressed will be printed based on the keyboard layout. You can try, even if you switch the keyboard layout during program execution, it still raises the right key. The output you see below is a session in which I pressed the d key in our layout switched to dvorak, pressed the same key and got e correctly. And hats from you to use dvorak its way is better than qwerty, I use it too :)
the code
import pygame, os from pygame.locals import * pygame.init() screen = pygame.display.set_mode((640, 480)) keymap = {} while True: event = pygame.event.wait() if event.type == KEYDOWN: keymap[event.scancode] = event.unicode print 'keydown %s pressed' % event.unicode if (event.key == K_ESCAPE): os._exit(0) if event.type == KEYUP: print 'keyup %s pressed' % keymap[event.scancode]
Exit
keydown d pressed keyup d pressed keydown e pressed keyup e pressed
source share