KeyBoardEvents C++ 的数据结构

Data Structure for KeyBoardEvents C++

我正在研究一个输入系统,作为我正在观看的教程的一部分,并且在我的 google 搜索中,我发现只有一种在线方法可以通过应用程序循环中的 switch 语句处理输入。

我找不到本教程真正采用 mod 键(如 shift、alt 和 ctrl)的示例。我发现我可以正确地提取当前 mod 的内容,但我找不到方便的方法来实现我想做的事情。

我的计划是让输入 class 成为应用程序 class 的一部分。输入可以传递给 window 和游戏对象,这样我就有了一个用于整个程序的输入状态系统。我想根据需要从游戏或 window 中注册回调函数,而我知道的唯一方法是通过 <functional>。我以为我会遇到 std::map<key scancode,std::function<void()>> 那种情况。但是,由于我需要考虑密钥的 modding,有没有一种方法可以生成由扫描码和 mods 组合而成的唯一密钥值,例如 ashift + actrl+a?

现在我认为我的解决方案有点被黑了,因为我需要为每个 modifier 组合设置唯一的 std::map

当前注册(减去将 key/func 对插入地图的实现),但我基本上需要为下面的每个 if 块创建一个单独的地图。有没有更好的方法来管理它?

void Register(SDL_KeyboardEvent key, std::function<void()> func ) {

    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;
        }

    }
}

目前:

std::map<key,std::function<void()>> Callbacks;

struct key {
    SDL_Scancode code;
    Uint16       kmod;

    bool operator<( const key & okey) const {
        return std::tie( code,kmod ) < std::tie( okey.code,okey.kmod );
    }
};

void Input::RegCallBack(SDL_Scancode k, Uint16 kmod, std::function<void()> func) {
key tkey; 
tkey.code = k;
tkey.kmod = kmod;

Callbacks[tkey] = func;  //Does not like this.

return;
}

智能感知错误是:

Error: no instance of overloaded function "std::map<_Kty,_Ty,_Pr,_Alloc>::insert [with _Kty=key,_Ty=std::function,_Pr=std::less,_Alloc=std::allocator>>]" matches the argument list argument types are:( std::pair>)

object type is: std::map, std::less, std::allocator>>>

下面是编译器的输出

1>------ Build started: Project: 3D Game Tut, Configuration: Debug Win32 ------
1>Build started 7/22/2015 3:31:55 PM.
1>InitializeBuildStatus:
1>  Creating "DebugD Game Tut.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  Input.cpp
1>c:\users\frizzlefry\documents\visual studio 2012\projectsd game tutd game tut\input.cpp(65): error C2679: binary '[' : no operator found which takes a right-hand operand of type 'Input::key' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(173): could be 'std::function<_Fty> &std::map<_Kty,_Ty>::operator [](key &&)'
1>          with
1>          [
1>              _Fty=void (void),
1>              _Kty=key,
1>              _Ty=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(190): or       'std::function<_Fty> &std::map<_Kty,_Ty>::operator [](const key &)'
1>          with
1>          [
1>              _Fty=void (void),
1>              _Kty=key,
1>              _Ty=std::function<void (void)>
1>          ]
1>          while trying to match the argument list '(std::map<_Kty,_Ty>, Input::key)'
1>          with
1>          [
1>              _Kty=key,
1>              _Ty=std::function<void (void)>
1>          ]
1>  Generating Code...
1>  Compiling...
1>  Game.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(201): error C2079: 'std::pair<_Ty1,_Ty2>::first' uses undefined struct 'key'
1>          with
1>          [
1>              _Ty1=const key,
1>              _Ty2=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(498) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const key,
1>              _Ty2=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(593) : see reference to class template instantiation 'std::_Tree_node<_Value_type,_Voidptr>' being compiled
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(592) : while compiling class template member function 'std::_Tree_node<_Value_type,_Voidptr> *&std::_Tree_val<_Val_types>::_Left(std::_Tree_node<_Value_type,_Voidptr> *)'
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *,
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(864) : see reference to function template instantiation 'std::_Tree_node<_Value_type,_Voidptr> *&std::_Tree_val<_Val_types>::_Left(std::_Tree_node<_Value_type,_Voidptr> *)' being compiled
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *,
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(766) : see reference to class template instantiation 'std::_Tree_val<_Val_types>' being compiled
1>          with
1>          [
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(884) : see reference to class template instantiation 'std::_Tree_alloc<_Al_has_storage,_Alloc_types>' being compiled
1>          with
1>          [
1>              _Al_has_storage=false,
1>              _Alloc_types=std::_Tree_base_types<std::pair<const key,std::function<void (void)>>,std::allocator<std::pair<const key,std::function<void (void)>>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(996) : see reference to class template instantiation 'std::_Tree_buy<_Ty,_Alloc>' being compiled
1>          with
1>          [
1>              _Ty=std::pair<const key,std::function<void (void)>>,
1>              _Alloc=std::allocator<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(1029) : see reference to class template instantiation 'std::_Tree_comp<_Pr_has_storage,_Traits>' being compiled
1>          with
1>          [
1>              _Pr_has_storage=false,
1>              _Traits=std::_Tmap_traits<key,std::function<void (void)>,std::less<key>,std::allocator<std::pair<const key,std::function<void (void)>>>,false>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(67) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<key,std::function<void (void)>,std::less<key>,std::allocator<std::pair<const key,std::function<void (void)>>>,false>
1>          ]
1>          c:\users\frizzlefry\documents\visual studio 2012\projectsd game tutd game tut\input.h(21) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=key,
1>              _Ty=std::function<void (void)>
1>          ]
1>  Generating Code...
1>  Compiling...
1>  KApp.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(201): error C2079: 'std::pair<_Ty1,_Ty2>::first' uses undefined struct 'key'
1>          with
1>          [
1>              _Ty1=const key,
1>              _Ty2=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(498) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const key,
1>              _Ty2=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(593) : see reference to class template instantiation 'std::_Tree_node<_Value_type,_Voidptr>' being compiled
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(592) : while compiling class template member function 'std::_Tree_node<_Value_type,_Voidptr> *&std::_Tree_val<_Val_types>::_Left(std::_Tree_node<_Value_type,_Voidptr> *)'
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *,
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(864) : see reference to function template instantiation 'std::_Tree_node<_Value_type,_Voidptr> *&std::_Tree_val<_Val_types>::_Left(std::_Tree_node<_Value_type,_Voidptr> *)' being compiled
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *,
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(766) : see reference to class template instantiation 'std::_Tree_val<_Val_types>' being compiled
1>          with
1>          [
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(884) : see reference to class template instantiation 'std::_Tree_alloc<_Al_has_storage,_Alloc_types>' being compiled
1>          with
1>          [
1>              _Al_has_storage=false,
1>              _Alloc_types=std::_Tree_base_types<std::pair<const key,std::function<void (void)>>,std::allocator<std::pair<const key,std::function<void (void)>>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(996) : see reference to class template instantiation 'std::_Tree_buy<_Ty,_Alloc>' being compiled
1>          with
1>          [
1>              _Ty=std::pair<const key,std::function<void (void)>>,
1>              _Alloc=std::allocator<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(1029) : see reference to class template instantiation 'std::_Tree_comp<_Pr_has_storage,_Traits>' being compiled
1>          with
1>          [
1>              _Pr_has_storage=false,
1>              _Traits=std::_Tmap_traits<key,std::function<void (void)>,std::less<key>,std::allocator<std::pair<const key,std::function<void (void)>>>,false>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(67) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<key,std::function<void (void)>,std::less<key>,std::allocator<std::pair<const key,std::function<void (void)>>>,false>
1>          ]
1>          c:\users\frizzlefry\documents\visual studio 2012\projectsd game tutd game tut\input.h(21) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=key,
1>              _Ty=std::function<void (void)>
1>          ]
1>  Generating Code...
1>  Compiling...
1>  Source.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(201): error C2079: 'std::pair<_Ty1,_Ty2>::first' uses undefined struct 'key'
1>          with
1>          [
1>              _Ty1=const key,
1>              _Ty2=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(498) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const key,
1>              _Ty2=std::function<void (void)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(593) : see reference to class template instantiation 'std::_Tree_node<_Value_type,_Voidptr>' being compiled
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(592) : while compiling class template member function 'std::_Tree_node<_Value_type,_Voidptr> *&std::_Tree_val<_Val_types>::_Left(std::_Tree_node<_Value_type,_Voidptr> *)'
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *,
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(864) : see reference to function template instantiation 'std::_Tree_node<_Value_type,_Voidptr> *&std::_Tree_val<_Val_types>::_Left(std::_Tree_node<_Value_type,_Voidptr> *)' being compiled
1>          with
1>          [
1>              _Value_type=std::pair<const key,std::function<void (void)>>,
1>              _Voidptr=void *,
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(766) : see reference to class template instantiation 'std::_Tree_val<_Val_types>' being compiled
1>          with
1>          [
1>              _Val_types=std::_Tree_simple_types<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(884) : see reference to class template instantiation 'std::_Tree_alloc<_Al_has_storage,_Alloc_types>' being compiled
1>          with
1>          [
1>              _Al_has_storage=false,
1>              _Alloc_types=std::_Tree_base_types<std::pair<const key,std::function<void (void)>>,std::allocator<std::pair<const key,std::function<void (void)>>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(996) : see reference to class template instantiation 'std::_Tree_buy<_Ty,_Alloc>' being compiled
1>          with
1>          [
1>              _Ty=std::pair<const key,std::function<void (void)>>,
1>              _Alloc=std::allocator<std::pair<const key,std::function<void (void)>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(1029) : see reference to class template instantiation 'std::_Tree_comp<_Pr_has_storage,_Traits>' being compiled
1>          with
1>          [
1>              _Pr_has_storage=false,
1>              _Traits=std::_Tmap_traits<key,std::function<void (void)>,std::less<key>,std::allocator<std::pair<const key,std::function<void (void)>>>,false>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(67) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<key,std::function<void (void)>,std::less<key>,std::allocator<std::pair<const key,std::function<void (void)>>>,false>
1>          ]
1>          c:\users\frizzlefry\documents\visual studio 2012\projectsd game tutd game tut\input.h(21) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=key,
1>              _Ty=std::function<void (void)>
1>          ]
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.70
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

将元键添加到您的地图键类型:

struct Shortcut {
 bool shift;
 bool alt;
 // ...
 KeySym sym;
 struct Hasher {
  // some hash algorithm
 };
 // also add equality operator
};

using ShortcutsMap = unordered_map<Shortcut, function<void(void)>, Shortcut::Hash>;

或者,将一组活动/按下的键映射到功能;

using ShortcutsMap = unordered_map<unordered_set<Key>, function<void(void)>>;

然后你需要一些 Key class,它能够容纳每个按下的键(包括元键)。