class - Data Structure for KeyBoardEvents C++ -


i working on input system part of tutorial watching, , google searching, find 1 method online handling input via switch statement in application loop.

i can't find examples tutorial takes in mod keys shift, alt , ctrl. found enough can pull current mods can not find convenient way achieve do.

my plan have input class part of application class. input can passed window , game objects way have single input state system entire program. wanted register callback functions within game or window necessary, , way know how via <functional>. thought have std::map<key scancode,std::function<void()>> sort of situation. however, since need account modding of key, there way generate unique key value combined of scancode , mods, example a, shift + a , ctrl+a?

right solution think bit hacked need have unique std::map set each modifier combination.

current registration (minus implementation of inserting key/func pair map), need individual map each if block below. there better way manage this?

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

currently:

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 this.  return; } 

intellisense error is:

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 argument list argument types are:( std::pair>)

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

and following compiler output

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" 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 takes right-hand operand of type 'input::key' (or there no acceptable conversion) 1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(173): 'std::function<_fty> &std::map<_kty,_ty>::operator [](key &&)' 1>          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>          1>          [ 1>              _fty=void (void), 1>              _kty=key, 1>              _ty=std::function<void (void)> 1>          ] 1>          while trying match argument list '(std::map<_kty,_ty>, input::key)' 1>          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>          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 class template instantiation 'std::pair<_ty1,_ty2>' being compiled 1>          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 class template instantiation 'std::_tree_node<_value_type,_voidptr>' being compiled 1>          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>          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 function template instantiation 'std::_tree_node<_value_type,_voidptr> *&std::_tree_val<_val_types>::_left(std::_tree_node<_value_type,_voidptr> *)' being compiled 1>          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 class template instantiation 'std::_tree_val<_val_types>' being compiled 1>          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 class template instantiation 'std::_tree_alloc<_al_has_storage,_alloc_types>' being compiled 1>          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 class template instantiation 'std::_tree_buy<_ty,_alloc>' being compiled 1>          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 class template instantiation 'std::_tree_comp<_pr_has_storage,_traits>' being compiled 1>          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 class template instantiation 'std::_tree<_traits>' being compiled 1>          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 class template instantiation 'std::map<_kty,_ty>' being compiled 1>          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>          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 class template instantiation 'std::pair<_ty1,_ty2>' being compiled 1>          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 class template instantiation 'std::_tree_node<_value_type,_voidptr>' being compiled 1>          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>          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 function template instantiation 'std::_tree_node<_value_type,_voidptr> *&std::_tree_val<_val_types>::_left(std::_tree_node<_value_type,_voidptr> *)' being compiled 1>          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 class template instantiation 'std::_tree_val<_val_types>' being compiled 1>          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 class template instantiation 'std::_tree_alloc<_al_has_storage,_alloc_types>' being compiled 1>          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 class template instantiation 'std::_tree_buy<_ty,_alloc>' being compiled 1>          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 class template instantiation 'std::_tree_comp<_pr_has_storage,_traits>' being compiled 1>          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 class template instantiation 'std::_tree<_traits>' being compiled 1>          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 class template instantiation 'std::map<_kty,_ty>' being compiled 1>          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>          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 class template instantiation 'std::pair<_ty1,_ty2>' being compiled 1>          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 class template instantiation 'std::_tree_node<_value_type,_voidptr>' being compiled 1>          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>          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 function template instantiation 'std::_tree_node<_value_type,_voidptr> *&std::_tree_val<_val_types>::_left(std::_tree_node<_value_type,_voidptr> *)' being compiled 1>          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 class template instantiation 'std::_tree_val<_val_types>' being compiled 1>          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 class template instantiation 'std::_tree_alloc<_al_has_storage,_alloc_types>' being compiled 1>          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 class template instantiation 'std::_tree_buy<_ty,_alloc>' being compiled 1>          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 class template instantiation 'std::_tree_comp<_pr_has_storage,_traits>' being compiled 1>          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 class template instantiation 'std::_tree<_traits>' being compiled 1>          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 class template instantiation 'std::map<_kty,_ty>' being compiled 1>          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 ========== 

add meta keys maps key type:

struct shortcut {  bool shift;  bool alt;  // ...  keysym sym;  struct hasher {   // hash algorithm  };  // add equality operator };  using shortcutsmap = unordered_map<shortcut, function<void(void)>, shortcut::hash>; 

or, map set of active / pressed keys functions;

using shortcutsmap = unordered_map<unordered_set<key>, function<void(void)>>; 

you need key class then, capable of holding every pressed key (including meta keys).


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -