简体   繁体   English

SDL2键盘检测不起作用

[英]SDL2 keyboard detection doesn't work

I try to make a simple game using sdl2 for handling keyboard events. 我尝试使用sdl2制作一个简单的游戏来处理键盘事件。 But the main problem is that neither in Clion embedded console nor in external console my program can't detect any event, only SDL_AUDIODEVICEADDED is caught twice at start of the program. 但主要的问题是,无论是在Clion嵌入式控制台还是在外部控制台中,我的程序都无法检测到任何事件,只有SDL_AUDIODEVICEADDED在程序启动时被捕获两次。 Here's my code: 这是我的代码:

#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 returns 0. SDL_Init返回0。

You need to create a window. 您需要创建一个窗口。

Keyboard and mouse events depend on a display surface. 键盘和鼠标事件取决于显示表面。 Only mouse actions within a window and keyboard input while the window is in focus will be captured. 将捕获窗口处于焦点时窗口和键盘输入内的鼠标操作。 After all, you shouldn't be able to receive input possibly directed at other applications. 毕竟,您应该无法接收可能针对其他应用程序的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM