c - 如果已经按下了向下+向右或向上+向左,则没有空格键的 SDL 事件

标签 c sdl

我正在 SDL 的 2D 游戏中试验 Action ,效果很好。但是,当我通过按 SPACE 添加角色射击的可能性时,我遇到了一个奇怪的错误:如果已经按下了向下和向右键或向上和向左键,则按下 时不会创建任何事件>空间。在其他情况下,事件会按原样创建。

这是我用来获取一些事件的循环:

while (on == 1)
{
    SDL_PollEvent(&event);
    switch (event.type)
    {
        case SDL_KEYDOWN:
            switch (event.key.keysym.sym)
            {
                case SDLK_ESCAPE:
                    on = 0;
                    break;
                case SDLK_UP:
                    shooter1.dir[0] = 1; // shooter1 is an instance of a structure that
                                         // contains a position and an array representing
                                         // 4 directions (down, up, left, right)
                    break;
                case SDLK_DOWN:
                    shooter1.dir[1] = 1;
                    break;
                case SDLK_LEFT:
                    shooter1.dir[2] = 1;
                    break;
                case SDLK_RIGHT:
                    shooter1.dir[3] = 1;
                    break;
                case SDLK_SPACE:         // Eventually, the program does not go in this
                                         // case, even though the SPACE key is pressed
                    fprintf(stderr, "SPACE PRESSED\n");
                    break;
                default:
                    break;
            }
            break;
        case SDL_KEYUP:
            switch (event.key.keysym.sym)
            {
                case SDLK_UP:
                    shooter1.dir[0] = 0;
                    break;
                case SDLK_DOWN:
                    shooter1.dir[1] = 0;
                    break;
                case SDLK_LEFT:
                    shooter1.dir[2] = 0;
                    break;
                case SDLK_RIGHT:
                    shooter1.dir[3] = 0;
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    event.type = 0;
    get_new_positions(&shooter1);
    SDL_BlitSurface(background, NULL, screen, &background_pos);
    blit_shooter(screen, &shooter1);
    SDL_Flip(screen);
}

知道这种行为的原因吗?

最佳答案

这几乎可以肯定是硬件限制。引自 the relevant Wikipedia page , 强调:

Certain high-end keyboards have "n-key rollover". This means that each key is scanned completely independently by the keyboard hardware, so that each keypress is correctly detected regardless of how many other keys are being pressed or held down at the time. [...]

However, to reduce cost and design complexity, most computer keyboards do not isolate all keys in this way. Instead, they use a matrix of key switches, without any isolation diodes, that assumes that only a limited number of keys will be held down at any given time. With these keyboards, pressing as few as three keys can cause ghosting effects, although care is taken when laying out the matrix arrangement that this does not happen for common modifier key combinations.

关于c - 如果已经按下了向下+向右或向上+向左,则没有空格键的 SDL 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331316/

相关文章:

c - 使用函数在 C 中打印字符串

c - 如何将 char * 放入数组中,以便我可以在 qsort 中使用它,然后转到下一行

c++ - SDL_image 无法使用 IMG_LoadTexture() 加载 .png 文件

opengl - 渲染 TTF SDL2.0 opengl 3.1

input - 使用SDL从键盘读取输入的最佳方法是什么?

c - 了解 C 中的堆栈溢出处理

c - ANSI C 和 sqlite3_get_table : How to pass results array back to calling function?

c++ - signal.h 是捕获空指针的可靠方法吗?

sdl - 如何使用 SDL 创建子窗口

c - 使用 SDL 时未定义对 WinMain@16 的引用