【问题标题】:SDL_GetKeyboardState not workingSDL_GetKeyboardState 不起作用
【发布时间】:2014-06-13 06:38:48
【问题描述】:

我正在尝试使用 SDL 2 为游戏制作控制器(不想在 gamedev 上询问,因为这不是直接的游戏问题)我使用 SDL_GetKeyboardEvent 来查看是否按下了导航箭头,但显然没有'不起作用,如果按下其中一个键,它应该打印值 1 或 -1,但即使我按住键几秒钟,它也不会只打印 0,就像它没有检测到键被按下。我在整个互联网上搜索,他们就是这样做的,但它对我不起作用。

#define SDL_MAIN_HANDLED
#include <stdio.h>
#include "SDL.h"

/* I'll add some code later
so something that isn't used
might be initialized
*/
int main (void)
{
    int a = 1;
    int x;
    int z;
    SDL_Event quit;
    const Uint8 *keys = SDL_GetKeyboardState(NULL);

    SDL_Init(SDL_INIT_VIDEO);

    while(a)
    {
        SDL_PollEvent(&quit);
        if(quit.type == SDL_QUIT)
            a = 0;

        if(keys[SDL_SCANCODE_UP])
            z = 1;
        else if(keys[SDL_SCANCODE_DOWN])
            z = -1;
        else
            z = 0;
        if(keys[SDL_SCANCODE_LEFT])
            x = -1;
        else if(keys[SDL_SCANCODE_RIGHT])
            x = 1;
        else
            x = 0;
        printf("%d, %d\n", x, z);
//This is supposed to print
//x, z values so if up arrow is pressed it will print 0, 1 and if
//down arrow is pressed it will print 0, -1: the same with horizontal ones.
//1 or -1, 1 or -1
    }

    SDL_Quit();
    return 0;
}

【问题讨论】:

  • 这不是重复的,这个问题使用了完全不同的方法和 SDL 1.x,而我使用的是 SDL 2,所以很多事情完全不同。

标签: c input keyboard sdl


【解决方案1】:

阅读文档:wiki

Note: This function gives you the current state after all events have been processed, so if a key or button has been pressed and released before you process events, then the pressed state will never show up in the SDL_GetKeyboardState() calls

什么意思?

您需要处理所有事件。如何?循环PollEvent,在循环之后(或者如果你想签入循环,请在最后检查),SDL_GetKeyboardState 是可用的。

所以,通过循环,检查键盘状态。在检查键之前不要忘记总是遍历循环

例如

while (game)
    {
        /*! updates the array of keystates */
        while ((SDL_PollEvent(&e)) != 0)
        {
            /*! request quit */
            if (e.type == SDL_QUIT) 
            { 
                game = false;
            }
        }

        if (keys[SDL_SCANCODE_RIGHT])
            std::cout << "Right key";
    }

【讨论】:

  • 这意味着我只需要在检查所有键之后而不是之前使用 SDL_PollEvent?
  • 你只需要“运行” PollEvent(循环),GetKeyboardState 就会完成
  • 嗯,它已经在循环里面了,我试着把它放在循环之前,它不起作用然后我把它放在循环之后,它不起作用。
  • 我尝试按照您的示例进行操作,但仍然无法正常工作。我正在使用 C BTW。可能是键盘编码有问题吗?
【解决方案2】:

这是我的 InputManager 更新函数,用于更新键盘和鼠标的用户输入。注意const_cast 需要能够更新类变量Uint8* keysArray;。这样,一次可以处理多个事件。

    void update() {
        SDL_PumpEvents();

        // update keyboard state
        keysArray = const_cast <Uint8*> (SDL_GetKeyboardState(NULL));

        if (keysArray[SDL_SCANCODE_RETURN])
            printf("MESSAGE: <RETURN> is pressed...\n");
        if (keysArray[SDL_SCANCODE_RIGHT] && keysArray[SDL_SCANCODE_UP])
            printf("MESSAGE: Right and Up arrows are pressed...\n");

        // update mouse location and button states
        SDL_GetMouseState(&x, &y);
        getMouseButtonStates();
}

【讨论】:

    【解决方案3】:

    您需要使用SDL_SetVideoMode 创建一个窗口来获取鼠标和键盘事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多