简体   繁体   English

如何轻松将字符串转换为键盘字符

[英]How can I easily convert a string to a keyboard character

Yeah, i'm doing hotkey parsing and I could do with an easy way to convert from string to keyboard character rather than writing a whole parser是的,我正在做热键解析,我可以用一种简单的方法从字符串转换为键盘字符,而不是编写整个解析器
For example: "F6" -> VK_F6例如:“F6”-> VK_F6

Such types of conversion can easily be done with lookup tables.这种类型的转换可以通过查找表轻松完成。 In C++ you could use std::map for this purpose.在 C++ 中,您可以为此目的使用std::map There are many other possibilities.还有很多其他的可能性。

Please see example below:请看下面的例子:

#include <iostream>
#include <map>
#include <string>

constexpr unsigned int VK_F1 = 0x70;
constexpr unsigned int VK_F2 = 0x71;
constexpr unsigned int VK_F3 = 0x72;
constexpr unsigned int VK_F4 = 0x73;
constexpr unsigned int VK_F5 = 0x74;
constexpr unsigned int VK_F6 = 0x75;

std::map<std::string, unsigned int> lookup{
    {"F1", VK_F1},
    {"F2", VK_F2},
    {"F3", VK_F3},
    {"F4", VK_F4},
    {"F5", VK_F5},
    {"F6", VK_F6},
};

int main() {

    unsigned int virtualKeyCode{ 0 };
    std::string keyString{};

    keyString = "F6";
    virtualKeyCode = lookup[keyString];

    std::cout << std::hex << virtualKeyCode << "\n";

    return 0;
}

Of course you need to extend for further keys.当然,您需要扩展更多键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM