Pygame keyboard layout mixed up

I am on Mac OS X 10.6 and I have Dvorak, US Extended, and Norwegian in my keyboard input selector menu, and US Extended is the one I use.

When I run Pygame programs using keyboard input, pygame seems to be using dvorak no matter what is actually selected.

This is part of the code that enters keyboard input:

# Check for events for event in pygame.event.get(): if event.type == KEYDOWN: # Change the keyboard variables if event.key == K_LEFT or event.key == ord('a'): moveRight = False moveLeft = True if event.key == K_RIGHT or event.key == ord('d'): moveLeft = False moveRight = True if event.key == K_UP or event.key == ord('w'): moveDown = False moveUp = True if event.key == K_DOWN or event.key == ord('s'): moveUp = False moveDown = True if event.type == KEYUP: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_LEFT or event.key == ord('a'): moveLeft = False if event.key == K_RIGHT or event.key == ord('d'): moveRight = False if event.key == K_UP or event.key == ord('w'): moveUp = False if event.key == K_DOWN or event.key == ord('s'): moveDown = False if event.key == ord('x'): player.top = random.randint(0, WINDOWHEIGHT - player.height) player.left = random.randint(0, WINDOWWIDTH - player.width) 

The arrow keys work as they should, but the WASD keys are distributed on the keyboard according to Dvorak. So, β€œA” is in the same place on both layouts, β€œW” is on the comma QWERTY key and so on. If I changed the code to search for the keys a , e and o , everything will work as expected.

How to get Pygame to use the correct layout?

+6
source share
1 answer

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 
+1
source

All Articles