SDL2键盘检测不起作用

时间:2017-06-27 10:25:39

标签: c++ keyboard sdl sdl-2

我尝试使用sdl2制作一个简单的游戏来处理键盘事件。但主要的问题是,无论是在Clion嵌入式控制台还是在外部控制台中,我的程序都无法检测到任何事件,只有SDL_AUDIODEVICEADDED在程序启动时被捕获两次。这是我的代码:

#include <iostream>
#include "include/SDL2/SDL_keyboard.h"
#include "include/SDL2/SDL_keycode.h"
#include "include/SDL2/SDL_scancode.h"

#include "include/SDL2/SDL.h"

using namespace std;

int main(int argc, char* argv []) {
    SDL_Event event;
    bool running = true;
    SDL_Init(SDL_INIT_EVERYTHING);
    int x, y;
    while(running){

        if(SDL_PollEvent(&event)){
            switch(event.type){
                case SDL_QUIT: running = false; break; 
                case SDL_KEYDOWN: 
                    switch(event.key.keysym.sym){ 
                        case SDLK_q: 
                            running = false;
                            break; 
                    }
                    break;

                case SDL_MOUSEBUTTONDOWN:
                    SDL_GetMouseState(&x, &y); 
                    cout << "[SDL_MOUSEBUTTONDOWN] " << "x = " << x << " y = " << y << "\n";
                    break;
                case SDL_MOUSEMOTION:
                    x = event.motion.x;
                    y = event.motion.y;
                    cout << "[SDL_MOUSEMOTION] " << "x = " << x << " y = " << y << "\n";
                    break;
            }
        }
    }
}

SDL_Init返回0.

1 个答案:

答案 0 :(得分:3)

您需要创建一个窗口。

键盘和鼠标事件取决于显示器表面。将捕获窗口处于焦点时窗口和键盘输入内的鼠标操作。毕竟,您应该无法接收可能针对其他应用程序的输入。