C ++绑定键盘键:遍历所有虚拟键?

Posted

技术标签:

【中文标题】C ++绑定键盘键:遍历所有虚拟键?【英文标题】:C++ Binding keyboard keys: iterate over all virtual keys? 【发布时间】:2011-11-08 16:23:59 【问题描述】:

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

当我加载这个文件时,有没有办法遍历配置文件中列出的所有可能的虚拟键:

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

还是我需要手动检查每一个?

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

【参考方案2】:

在加载文件内容的例程中,您需要将数据存储在map 中,其中键码作为键,函数名称作为值。与其遍历所有可能的键码,不如遍历映射中的条目。

【讨论】:

是的,但问题是如何遍历所有可能的键,所有虚拟键都有一个十六进制值,并且将在此时创建映射,仅包含指定键的条目 由于您实际上在上面展示了一个 for 循环,我认为这显然不是真正的问题。 for 循环是为了说明问题...因此 有没有办法遍历所有可能的虚拟键,最后 或者我需要手动检查每一个? 我没有得到你。如果您将配置信息加载到适当的数据结构中,那么您就没有必须遍历所有可能的键;这不是更好吗? 当我想创建您所指的地图时,同样的问题仍然存在。我需要事先手动输入地图中的每个键码。或者我猜在阅读配置文件时,扫描当前地图一段时间,如果它不存在则插入

以上是关于C ++绑定键盘键:遍历所有虚拟键?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Emacs 中的次要模式键盘映射中删除键?

绑定数字键(数字小键盘)

e860. 列出组件绑定的键盘键

Java KeyListener 与键绑定

Rstudio 中的 %>% 键绑定/键盘快捷键

e858. 将键盘键和事件绑定