0

今天我在我的应用程序中为所有不同的键设置了输入。除了虚拟键(例如插入符号或 & 符号)之外,这可以正常工作。通常需要换档才能拿到的钥匙。使用 SDL,这些虚拟键不起作用。因为他们没有注册事件。

if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
            case SDLK_CARET:
                Keys[KeyCodes::Caret] = KeyState::Down;
                break;
            case SDLK_UP:
                Keys[KeyCodes::Up] = KeyState::Down;
                break;
            default:
                break;
        }

我绝对确定我的系统可以使用 Up 等物理键。该程序查询一个键状态,如下所示:

if (Keys[KeyCode] == KeyState::Down) {
    lua_pushboolean(L, true);
} else {
    lua_pushboolean(L, false);
}

KeyCode 作为参数传入。

那么,为什么使用 SDL 的 KeyDown 事件类型时虚拟键或需要转换才能正常工作的键?是否需要更多代码才能找到它们?还是我傻?

4

2 回答 2

1

SDL only reports real key events.

The good news is you can enable Unicode translation to get symbols like '^' or '@'.

First put this in your initialization code:

SDL_EnableUNICODE(1);

Now SDL_KEYDOWN events will have the accompanying character in the unicode member of SDL_keysym. This factors in shift, caps lock, etc., when translating the key press into a character. Keys like SDLK_UP will have unicode == 0.

This actually makes using keysym.unicode ideal for text input, especially when used with SDL_EnableKeyRepeat.

Here's an example: on my keyboard, I hold shift-6 to generate ^. The program recieves an SDL_KEYDOWN event with keysym.sym == SDLK_6, and keysym.unicode == '^'.

The one caveat is that only key press events will be translated, not release events. But this should not be a big problem, since you shouldn't use text characters for game controls anyway, only real keys. And if you're doing text input with key repeating, it only matters when keys are pressed, not released.

You might have to mix-and-match using keysym.sym and keysym.unicode to fit your exact needs.

于 2012-10-10T16:38:56.273 回答
0

好的,我为有点沮丧而道歉,但我终于得到了一些代码来判断某人何时按下了插入键。我确实希望其他人能找到这些有用的信息。

case SDLK_6:
    if (event.key.keysym.mod == KMOD_LSHIFT || event.key.keysym.mod == KMOD_RSHIFT) {
        Keys[KeyCodes::Caret] = KeyState::Down;
    } else {
        Keys[KeyCodes::n6] = KeyState::Down;
    }
    break;

基本上,在检查具有 shift click 特殊键的普通键时,然后检查键修饰符。我现在了解 unicode 值,但这个想法现在似乎更简单。

再次感谢所有的帮助!

于 2012-10-10T21:24:15.617 回答