C++ 键盘事件C+的数据结构+;
作为我正在观看的教程的一部分,我正在研究一个输入系统。在谷歌搜索中,我发现只有一种方法可以通过应用程序循环中的switch语句在线处理输入 我找不到本教程真正采用shift、alt和ctrl等mod键的示例。我发现我可以适当地拉动当前的mods,但我找不到一个方便的方法来实现我想做的事情 我的计划是拥有一个作为应用程序类一部分的输入类。输入可以传递到窗口和游戏对象,这样我就有了一个用于整个程序的单一输入状态系统。我想根据需要在游戏或窗口中注册回调函数,我知道的唯一方法是通过C++ 键盘事件C+的数据结构+;,c++,class,containers,keyboard-events,C++,Class,Containers,Keyboard Events,作为我正在观看的教程的一部分,我正在研究一个输入系统。在谷歌搜索中,我发现只有一种方法可以通过应用程序循环中的switch语句在线处理输入 我找不到本教程真正采用shift、alt和ctrl等mod键的示例。我发现我可以适当地拉动当前的mods,但我找不到一个方便的方法来实现我想做的事情 我的计划是拥有一个作为应用程序类一部分的输入类。输入可以传递到窗口和游戏对象,这样我就有了一个用于整个程序的单一输入状态系统。我想根据需要在游戏或窗口中注册回调函数,我知道的唯一方法是通过。我想我可能会有一种s
。我想我可能会有一种std::map
的情况。但是,由于我需要考虑键的修改,是否有一种方法可以生成扫描码和修改码组合的唯一键值,例如a
、shift+a
和ctrl+a
我认为现在我的解决方案有点被破解了,因为我需要为每个修改器组合设置一个唯一的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;
}
}
}
无效寄存器(SDL_键盘事件键,std::func函数){
如果(!(key.keysym.sym==SDLK|LSHIFT | key.keysym.sym==SDLK|RSHIFT | key.keysym.sym==SDLK|LCTRL | key.keysym.sym==SDLK|RCTRL | key.key.keysym.sym==SDLK LALT | key.key.keysym{
如果((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 with
1> [
1> _Fty=无效(无效),
1> _Kty=键,
1> _Ty=std::function
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\map(190):或“std::function&std::map::operator[](常量键和)
1> 与
1> [
1> _Fty=无效(无效),
1> _Kty=键,
1> _Ty=std::function
1> ]
1> 尝试匹配参数列表时“(std::map,Input::key)”
1> 与
1> [
1> _Kty=键,
1> _Ty=std::function
1> ]
1> 正在生成代码。。。
1> 编译。。。
1> Game.cpp
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\utility(201):错误C2079:“std::pair::first”使用未定义的结构“key”
1> 与
1> [
1> _Ty1=常数键,
1> _Ty2=std::function
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(498):请参阅正在编译的类模板实例化“std::pair”的参考
1> 与
1> [
1> _Ty1=常数键,
1> _Ty2=std::function
1> ]
1> c:\program files(x86)\microsoft visual studio 11.0\vc\include\xtree(593):请参阅正在编译的类模板实例化“std::\u Tree\u node”的参考
1> 与
1> [
1> \u值\u类型=标准::对,
1> _Voidptr=无效*
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(592):编译类模板成员函数“std::_Tree\u node*&std:_Tree\u val::_Left(std:_Tree\u node*)”
1> 与
1> [
1> \u值\u类型=标准::对,
1> _Voidptr=void*,
1> _Val_types=std::_Tree_simple_类型
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(864):请参阅正在编译的函数模板实例化“std::_Tree\u node*&std:_Tree\u val:_Left(std:_Tree\u node*)”的参考
1> 与
1> [
1> \u值\u类型=标准::对,
1> _Voidptr=void*,
1> _Val_types=std::_Tree_simple_类型
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(766):请参阅正在编译的类模板实例化“std::\u Tree\u val”的参考
1> 与
1> [
1> _Val_types=std::_Tree_simple_类型
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(884):请参阅正在编译的类模板实例化“std::_Tree\u alloc”的参考
1> 与
1> [
1> _Al_has_storage=false,
1> _Alloc_types=std::_Tree_base_types
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(996):请参阅正在编译的类模板实例化“std::\u Tree\u buy”的参考
1> 与
1> [
1> _Ty=std::pair,
1> _Alloc=std::分配器
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xtree(1029):请参阅正在编译的类模板实例化“std::_Tree\u comp”的参考
1> 与
1> [
1> _Pr_has_storage=false,
1> _Traits=std:_Tmap_Traits
1> ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\map(67):请参阅正在编译的类模板实例化“std::\u Tree”的参考
1> 与
1> [
1> _Traits=std:_Tmap_Traits
1> ]
1> c:\users\frizzlefry\documents\visualstudio 2012\projects\3d game tut\3d game tut\input.h(21):请参阅正在编译的类模板实例化“std::map”的参考
1> 与
1> [
1> _Kty=键,
1> _Ty=std::function
1> ]
1> 正在生成代码。。。
1> 编译。。。
1> KApp.cpp
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\utility(201):错误C2079:'std::pair::fi
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;
}
1>------ Build started: Project: 3D Game Tut, Configuration: Debug Win32 ------
1>Build started 7/22/2015 3:31:55 PM.
1>InitializeBuildStatus:
1> Creating "Debug\3D Game Tut.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> Input.cpp
1>c:\users\frizzlefry\documents\visual studio 2012\projects\3d game tut\3d 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\projects\3d game tut\3d 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\projects\3d game tut\3d 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\projects\3d game tut\3d 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)>>;