Pygame的键盘布局混杂在一起 [英] Pygame keyboard layouts mixed up

查看:133
本文介绍了Pygame的键盘布局混杂在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Mac OS X 10.6,我的键盘输入选择器菜单中有Dvorak,US Extended和Norwegian,而US Extended是我所使用的.

I'm 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.

当我使用键盘输入来运行Pygame程序时,pygame似乎认为我正在使用dvorak,无论实际选择了什么.

When I run Pygame programs with keyboard input, pygame seems to think I'm using dvorak regardless of what is actually selected.

这是接受键盘输入的代码部分:

This is the part of the code that takes the 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)

箭头键可以正常工作,但是WASD键以与Dvorak一致的方式分布在键盘上.因此,"A"在两种布局中都位于同一位置,"W"在QWERTY的逗号键中,依此类推.如果我更改代码以查找ae,o键,则一切正常.

The arrow keys work as they should, but the WASD keys are spread over the keyboard in a way consistent with Dvorak. So, "A" is in the same place on both layouts, "W" is on QWERTY's comma key, and so on. If I change the code to look for the a, e, , and o keys instead, things work as expected.

如何使Pygame使用正确的布局?

How can I make Pygame use the correct layout?

推荐答案

好,我必须做一些杂技才能使它正常工作.因此,首先我建议您使用密钥扫描代码,该代码可从event.scancode获取.每个键都有一个唯一的代码,该代码表示​​键盘上的物理键,无论您使用的是键盘布局还是我们,这都是相同的扫描代码.然后在keydown事件中,您将拥有一个名为unicode的属性,该属性是按下的字符,它尊重当前使用的键盘布局.因此,在us布局上按d键可以使您获得unicode d,在dvorak上按该键可以使您获得e字符,并且可以正确地反映在event.unicode中.这是有点烦人的地方.看来unicode属性仅在keydown事件上可用,而在keyup事件上不可用.因此,我简单地创建了一个称为keymap的字典,该字典将这些信息作为scancode到unicode字符的映射进行跟踪.考虑到键盘布局,下面的示例代码将打印出您按下的字符.您可以尝试一下,即使在程序执行过程中切换键盘布局,它仍然会选择正确的键.您在下面看到的输出是一个会话,在该会话中,我按了我们布局中的d键,切换为dvorak按了相同的键并正确得到e.还要感谢您使用dvorak比qwerty更好的方法,我也使用它:)

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 :)

代码

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]

输出

keydown d pressed
keyup d pressed
keydown e pressed
keyup e pressed

这篇关于Pygame的键盘布局混杂在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆