C++绑定键盘按键:迭代所有虚拟键?

发布于 2024-12-14 10:42:13 字数 308 浏览 3 评论 0原文

我希望能够为我的应用程序加载自定义按键绑定。配置文件通常包含键列表和关联的函数名称。

当我加载此文件时,是否有一种方法可以迭代配置文件中以以下形式列出的所有可能的虚拟键:

for(int i = key0; i < keymax; i = i+key){
    char key = itoa(i);
    if(cfgfile.getfunction(key,function)){
        _keyBinds[key] = function;
    }
}

或者我是否需要手动检查其中的每一个?

I'd like to be able to load custom key binds for my application. A configuration file would typically contain a list of keys and associated function names.

When I load this file, is there a way to iterate over all the possible virtual keys that would be listed inside the configuration file in the form:

for(int i = key0; i < keymax; i = i+key){
    char key = itoa(i);
    if(cfgfile.getfunction(key,function)){
        _keyBinds[key] = function;
    }
}

Or do I need to manually check for every one of them ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-21 10:42:13

这就是我最终创建键盘按键映射的方法。它涵盖了几乎所有可能性:不可映射键存储为十六进制字符串,不将自身表示为单个字符的可映射键需要手动添加。也许这对将来的某人有用。

CreateKeyboardMap()
{
    string keystring;
    char keybuffer;
    for(int i = 0; i < 256; ++i){
        if(keybuffer = MapVirtualKey(UINT(i),2)){
            keystring += keybuffer;
        }
        else{
            keystring = int_to_hex(i);
        }
        _keyboardMap.insert(_keyboardMap.end(),pair<UINT,string>(i,keystring));
        keystring = "";
    }

    _keyboardMap[0x1B] = "ESCAPE";
    _keyboardMap[0x70] = "F1";
    _keyboardMap[0x71] = "F2";
    _keyboardMap[0x72] = "F3";
    _keyboardMap[0x73] = "F4";
    _keyboardMap[0x74] = "F5";
    _keyboardMap[0x75] = "F6";
    _keyboardMap[0x76] = "F7";
    _keyboardMap[0x77] = "F8";
    _keyboardMap[0x78] = "F9";
    _keyboardMap[0x79] = "F10";
    _keyboardMap[0x7A] = "F11";
    _keyboardMap[0x7B] = "F12";
    _keyboardMap[0x2C] = "PRINT SCREEN";
    _keyboardMap[0x91] = "SCROLL LOCK";
    _keyboardMap[0x08] = "BACKSPACE";
    _keyboardMap[0x20] = "SPACE";
    _keyboardMap[0x2D] = "INSERT";
    _keyboardMap[0x24] = "HOME";
    _keyboardMap[0x22] = "PAGE DOWN";
    _keyboardMap[0x21] = "PAGE UP";
    _keyboardMap[0x2E] = "DELETE";
    _keyboardMap[0x90] = "NUMLOCK";
    _keyboardMap[0x6F] = "NUMPAD /";
    _keyboardMap[0x6A] = "NUMPAD *";
    _keyboardMap[0x6D] = "NUMPAD -";
    _keyboardMap[0x6B] = "NUMPAD +";
    _keyboardMap[0x6E] = "NUMPAD .";
    _keyboardMap[0x60] = "NUMPAD 0";
    _keyboardMap[0x61] = "NUMPAD 1";
    _keyboardMap[0x62] = "NUMPAD 2";
    _keyboardMap[0x63] = "NUMPAD 3";
    _keyboardMap[0x64] = "NUMPAD 4";
    _keyboardMap[0x65] = "NUMPAD 5";
    _keyboardMap[0x66] = "NUMPAD 6";
    _keyboardMap[0x67] = "NUMPAD 7";
    _keyboardMap[0x68] = "NUMPAD 8";
    _keyboardMap[0x68] = "NUMPAD 9";

    _keyboardMap[0x26] = "ARROW UP";
    _keyboardMap[0x28] = "ARROW DOWN";
    _keyboardMap[0x25] = "ARROW LEFT";
    _keyboardMap[0x27] = "ARROW RIGHT";

    _keyboardMap[0x0D] = "ENTER";
    _keyboardMap[0xA0] = "LSHIFT";
    _keyboardMap[0xA1] = "RSHIFT";
    _keyboardMap[0x09] = "TAB";
    _keyboardMap[0x14] = "CAPS LOCK";
    _keyboardMap[0xA2] = "LCONTROL";
    _keyboardMap[0xA3] = "RCONTROL";
    _keyboardMap[0xA4] = "LALT";
    _keyboardMap[0xA5] = "RALT";
    _keyboardMap[0x5B] = "LWIN";
    _keyboardMap[0x5C] = "RWIN";
}

This is the way I finally proceeded to create a map of the keyboard keys. It covers almost all possibilities: non-mappable keys are stored as a hex string, mappable keys which do not represent themselves as a single caracter need to be added manually. Maybe this can be usefull for someone in the future.

CreateKeyboardMap()
{
    string keystring;
    char keybuffer;
    for(int i = 0; i < 256; ++i){
        if(keybuffer = MapVirtualKey(UINT(i),2)){
            keystring += keybuffer;
        }
        else{
            keystring = int_to_hex(i);
        }
        _keyboardMap.insert(_keyboardMap.end(),pair<UINT,string>(i,keystring));
        keystring = "";
    }

    _keyboardMap[0x1B] = "ESCAPE";
    _keyboardMap[0x70] = "F1";
    _keyboardMap[0x71] = "F2";
    _keyboardMap[0x72] = "F3";
    _keyboardMap[0x73] = "F4";
    _keyboardMap[0x74] = "F5";
    _keyboardMap[0x75] = "F6";
    _keyboardMap[0x76] = "F7";
    _keyboardMap[0x77] = "F8";
    _keyboardMap[0x78] = "F9";
    _keyboardMap[0x79] = "F10";
    _keyboardMap[0x7A] = "F11";
    _keyboardMap[0x7B] = "F12";
    _keyboardMap[0x2C] = "PRINT SCREEN";
    _keyboardMap[0x91] = "SCROLL LOCK";
    _keyboardMap[0x08] = "BACKSPACE";
    _keyboardMap[0x20] = "SPACE";
    _keyboardMap[0x2D] = "INSERT";
    _keyboardMap[0x24] = "HOME";
    _keyboardMap[0x22] = "PAGE DOWN";
    _keyboardMap[0x21] = "PAGE UP";
    _keyboardMap[0x2E] = "DELETE";
    _keyboardMap[0x90] = "NUMLOCK";
    _keyboardMap[0x6F] = "NUMPAD /";
    _keyboardMap[0x6A] = "NUMPAD *";
    _keyboardMap[0x6D] = "NUMPAD -";
    _keyboardMap[0x6B] = "NUMPAD +";
    _keyboardMap[0x6E] = "NUMPAD .";
    _keyboardMap[0x60] = "NUMPAD 0";
    _keyboardMap[0x61] = "NUMPAD 1";
    _keyboardMap[0x62] = "NUMPAD 2";
    _keyboardMap[0x63] = "NUMPAD 3";
    _keyboardMap[0x64] = "NUMPAD 4";
    _keyboardMap[0x65] = "NUMPAD 5";
    _keyboardMap[0x66] = "NUMPAD 6";
    _keyboardMap[0x67] = "NUMPAD 7";
    _keyboardMap[0x68] = "NUMPAD 8";
    _keyboardMap[0x68] = "NUMPAD 9";

    _keyboardMap[0x26] = "ARROW UP";
    _keyboardMap[0x28] = "ARROW DOWN";
    _keyboardMap[0x25] = "ARROW LEFT";
    _keyboardMap[0x27] = "ARROW RIGHT";

    _keyboardMap[0x0D] = "ENTER";
    _keyboardMap[0xA0] = "LSHIFT";
    _keyboardMap[0xA1] = "RSHIFT";
    _keyboardMap[0x09] = "TAB";
    _keyboardMap[0x14] = "CAPS LOCK";
    _keyboardMap[0xA2] = "LCONTROL";
    _keyboardMap[0xA3] = "RCONTROL";
    _keyboardMap[0xA4] = "LALT";
    _keyboardMap[0xA5] = "RALT";
    _keyboardMap[0x5B] = "LWIN";
    _keyboardMap[0x5C] = "RWIN";
}
木格 2024-12-21 10:42:13

在加载文件内容的例程中,您需要将数据存储在 < code>map 以键码作为键,以函数名称作为值。那么,只需迭代映射中的条目,而不是迭代所有可能的键码。

In your routine that loads the contents of the file, you'll want to store the data in a map with the keycodes as the keys and function names as the values. Instead of iterating over all possible keycodes, then, just iterate over the entries in the map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文