Pygame keyboard layouts mixed up
Ok I had to do some acrobatics to get this working. So first I recommend you use the key scancode which you can get from event.scancode
. Each key has a unique code that refers to the physical key on the keyboard and this is the same scancode regardless of your keyboard layout dvorak or us. Then on the keydown event you will have an attribute called unicode which is the character pressed that respects the current keyboard layout in use. So pressing the d key on a us layout gets you unicode d, on dvorak that physical key would get you the e character and this gets reflected correctly in event.unicode
. Here's where it gets a little annoying. It seems the unicode attribute is only available on the keydown event and not the keyup event. So I simply created a dictionary called keymap that keeps track of this information as a mapping of scancode to unicode character. The example code below will print out the character you pressed taking into account the keyboard layout. You can try it out, even if you switch the keyboard layout during program execution it still picks up the right key. The output you see below is a session where I pressed the d key in us layout switched to dvorak pressed the same key and correctly got e. And hats off to you for using dvorak its way better then qwerty, I use it too :)
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]
output
keydown d pressed
keyup d pressed
keydown e pressed
keyup e pressed