在没有窗口焦点的情况下处理SDL_KeyboardEvent

时间:2018-07-09 12:37:28

标签: events input keyboard sdl-2

我正在尝试使用SDL2.0获得键盘事件。当我具有窗口焦点时,以下示例对我有用:

#include <SDL2/SDL.h>

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0) /* Prefer only events SDL_INIT_EVENTS */
    {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        return 1;
    }

    SDL_Window* gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 320, SDL_WINDOW_SHOWN ); /* I don't want this window! */

    /* While no quit */
    bool quit = false;

    //Event handler
    SDL_Event e;

    //While application is running
    while( !quit )
    {
        //Handle events on queue
        while( SDL_PollEvent( &e ) != 0 )
        {
            //User requests quit
            if( e.type == SDL_QUIT )
            {
                quit = true;
            }
            //User presses a key
            else if( e.type == SDL_KEYDOWN )
            {
                printf( "Key type: %d\n", e.key.keysym.sym );
                //Select surfaces based on key press
                switch( e.key.keysym.sym )
                {
                    case SDLK_UP:
                    printf( "UP!\n" );
                    break;

                    case SDLK_DOWN:
                    printf( "DOWN!\n" );
                    break;

                    case SDLK_LEFT:
                    printf( "LEFT!\n" );
                    break;

                    case SDLK_RIGHT:
                    printf( "RIGHT!\n" );
                    break;

                    default:
                    break;
                }
            }
        }
    }

    SDL_Quit();
    return 0;
}

我想避免使用SDL_Init(SDL_INIT_VIDEO)而不创建仅使用SDL_Init(SDL_INIT_EVENTS)的SDL_Window。实际上,我的问题是,只有创建窗口并对其进行聚焦后,我才能接收事件。没有焦点就可以在全球范围内举办活动吗?

我的策略是尝试使用SDL库来隔离OS事件行为,并专注于记录/播放用户提供的小事件。其他建议也欢迎。

0 个答案:

没有答案