在键盘输入期间测试活动标志

时间:2015-07-22 04:29:50

标签: c++ keyboard-events sdl-2

我正在使用SDL2轮询事件并将键盘按下转发到游戏对象。我从SDL_KeyBoardEvent事件类型传入SDL_KEYDOWN变量。我似乎无法理解如何管理在输入期间按下修改键的情况。

我比较了MODK_Keys枚举和SDL_KeyBoardEvent的key.keysym.mod值,看起来它不应该进入顶部if循环,但确实如此。如果我只持有shift并按下a之类的任何键,则输出是第一个if语句中的意外输出。它显示

1 0
Ctrl + Alt + Shift + 97 

这个( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL ) )在输出期间等于零,但它是通过逻辑表达式吗?以下是if语句

void KeyDown(SDL_KeyboardEvent key) {

    if( !( key.keysym.sym == SDLK_LSHIFT || key.keysym.sym == SDLK_RSHIFT || key.keysym.sym == SDLK_LCTRL || key.keysym.sym == SDLK_RCTRL || key.keysym.sym == SDLK_LALT || key.keysym.sym == SDLK_RALT )) {

        if( ( key.keysym.mod & ( KMOD_LSHIFT | KMOD_RSHIFT ) > 0 ) && ( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL ) > 0 ) && ( key.keysym.mod & ( KMOD_LALT | KMOD_RALT )  > 0  )) {
            std::cout << ( key.keysym.mod & ( KMOD_LSHIFT | KMOD_RSHIFT ) ) << " " << ( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL ) ) << std::endl;
            std::cout << "Ctrl + Alt + Shift + " << key.keysym.sym  << std::endl;
        }
        else if ( ( key.keysym.mod & ( KMOD_LSHIFT |  KMOD_RSHIFT ) > 0 ) && ( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL ) > 0 )) {
            std::cout << "Shift + Ctrl + " << key.keysym.sym << std::endl;
        }
        else if ( ( key.keysym.mod & ( KMOD_LSHIFT |  KMOD_RSHIFT ) > 0 ) && ( key.keysym.mod & ( KMOD_LALT | KMOD_RALT )  > 0 )) {
            std::cout << "Shift + Alt + " << key.keysym.sym << std::endl;
        }
        else if ( ( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL ) > 0 ) && ( key.keysym.mod & ( KMOD_LALT | KMOD_RALT )  > 0 )) {
            std::cout << "Ctrl + Alt + " << key.keysym.sym << std::endl;
        }
        else if ( key.keysym.mod & ( KMOD_LSHIFT |  KMOD_RSHIFT ) > 0 ) {
            std::cout << "Shift + " << key.keysym.sym << std::endl;
        }
        else if ( key.keysym.mod & ( KMOD_LALT | KMOD_RALT ) > 0 ) {
            std::cout << "Alt + " << key.keysym.sym << std::endl;
        }
        else if ( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL ) > 0 ) {
            std::cout << "Ctrl + " << key.keysym.sym << std::endl;
        }
        else {
            std::cout << "Unmodded " << key.keysym.sym << " MODS: " << key.keysym.mod << std::endl;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

睡觉并尝试了一件不同的事情。我的操作顺序显然有误。

使以下开关工作(在所有表达式上更改)

( key.keysym.mod & ( KMOD_LSHIFT | KMOD_RSHIFT ) > 0 )

(( key.keysym.mod & ( KMOD_LSHIFT | KMOD_RSHIFT )) > 0 )

有效的功能是

void KeyDown(SDL_KeyboardEvent key) {

    if( !( key.keysym.sym == SDLK_LSHIFT || key.keysym.sym == SDLK_RSHIFT || key.keysym.sym == SDLK_LCTRL || key.keysym.sym == SDLK_RCTRL || key.keysym.sym == SDLK_LALT || key.keysym.sym == SDLK_RALT )) {

        if( (( key.keysym.mod & ( KMOD_LSHIFT | KMOD_RSHIFT )) > 0 ) && (( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL )) > 0 ) && (( key.keysym.mod & ( KMOD_LALT | KMOD_RALT ))  > 0  )) {
            std::cout << "Ctrl + Alt + Shift + " << key.keysym.sym  << std::endl;
        }
        else if ( (( key.keysym.mod & ( KMOD_LSHIFT |  KMOD_RSHIFT )) > 0 ) && (( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL )) > 0 )) {
            std::cout << "Shift + Ctrl + " << key.keysym.sym << std::endl;
        }
        else if ( (( key.keysym.mod & ( KMOD_LSHIFT |  KMOD_RSHIFT )) > 0 ) && (( key.keysym.mod & ( KMOD_LALT | KMOD_RALT ))  > 0 )) {
            std::cout << "Shift + Alt + " << key.keysym.sym << std::endl;
        }
        else if ( (( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL )) > 0 ) && (( key.keysym.mod & ( KMOD_LALT | KMOD_RALT ))  > 0 )) {
            std::cout << "Ctrl + Alt + " << key.keysym.sym << std::endl;
        }
        else if (( key.keysym.mod & ( KMOD_LSHIFT |  KMOD_RSHIFT )) > 0 ) {
            std::cout << "Shift + " << key.keysym.sym << std::endl;
        }
        else if (( key.keysym.mod & ( KMOD_LALT | KMOD_RALT )) > 0 ) {
            std::cout << "Alt + " << key.keysym.sym << std::endl;
        }
        else if (( key.keysym.mod & ( KMOD_LCTRL | KMOD_RCTRL )) > 0 ) {
            std::cout << "Ctrl + " << key.keysym.sym << std::endl;
        }
        else {
            std::cout << "Unmodded " << key.keysym.sym << " MODS: " << key.keysym.mod << std::endl;
        }

    }
}