gpt4 book ai didi

c++ - KeyBoardEvents C++ 的数据结构

转载 作者:行者123 更新时间:2023-11-28 02:23:12 24 4
gpt4 key购买 nike

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

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

我的计划是让输入类成为应用程序类的一部分。输入可以传递给窗口和游戏对象,这样我就可以为整个程序提供一个输入状态系统。我想根据需要从游戏或窗口中注册回调函数,我知道的唯一方法是通过 <functional> .我以为我可以有一个 std::map<key scancode,std::function<void()>>种情况。但是,由于我需要考虑 key 的修改,有没有一种方法可以生成结合扫描码和修改的唯一键值,例如 a , shift + actrl+a

现在我认为我的解决方案有点被黑了,因为我需要有一个独特的 std::map为每个修饰符组合设置。

当前注册(减去将键/函数对插入映射的实现),但我基本上需要每个 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 "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 ==========

最佳答案

将元键添加到您的 map 键类型:

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 类,它能够保存每个按下的键(包括元键)。

关于c++ - KeyBoardEvents C++ 的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com