Pygame 键盘布局混淆 [英] Pygame keyboard layouts mixed up

查看:22
本文介绍了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 的逗号键上,依此类推.如果我更改代码以查找 aeo 键,事情会像预期.

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?

推荐答案

好的,我不得不做一些杂技才能使这个工作正常进行.所以首先我建议你使用 key scancode,你可以从 event.scancode.每个键都有一个唯一的代码,指的是键盘上的物理键,无论您的键盘布局是 dvorak 还是我们,这都是相同的扫描码.然后在 keydown 事件中,您将拥有一个名为 unicode 的属性,该属性是按下的字符,符合当前使用的键盘布局.因此,在 us 布局上按 d 键会得到 unicode d,在 dvorak 上,物理键会得到 e 字符,这在 event.unicode 中得到正确反映.这就是它有点烦人的地方.似乎 unicode 属性仅适用于 keydown 事件,而不适用于 keyup 事件.所以我简单地创建了一个名为 keymap 的字典,它跟踪这些信息作为扫描码到 unicode 字符的映射.下面的示例代码将根据键盘布局打印出您按下的字符.您可以尝试一下,即使您在程序执行期间切换键盘布局,它仍然会选择正确的键.你在下面看到的输出是一个会话,我在我们的布局中按下 d 键切换到 dvorak 按下相同的键并正确得到 e.向你致敬,因为它比 qwerty 更好地使用 dvorak,我也使用它:)

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天全站免登陆