如何轻松地将字符串转换为键盘字符 [英] How can I easily convert a string to a keyboard character

查看:76
本文介绍了如何轻松地将字符串转换为键盘字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我正在进行热键解析,我可以通过一种简单的方法来完成从字符串到键盘字符的转换,而不是编写整个解析器
例如:"F6"-> VK_F6

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

推荐答案

使用查询表可以轻松完成此类转换.在C ++中,您可以为此目的使用 std :: map .还有许多其他可能性.

Such types of conversion can easily be done with lookup tables. In C++ you could use std::map for this purpose. There are many other possibilities.

请参见以下示例:

#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.

这篇关于如何轻松地将字符串转换为键盘字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆