在寻找将虚拟键转换为unicode字符的方法时,我找到了this answer。碰巧是针对c#,但是C ++中提供了相同的方法,所以我使用了它。这是我目前的代码:
BYTE keyboard_state[256];
GetKeyboardState(keyboard_state); // Fill the array with the status of all keys.
// vk_code comes from a low level keyboard hook.
WCHAR data[256];
const int res = ToUnicode(vk_code, 0, keyboard_state, data, 256, 0); // Convert to unicode.
char str[256];
wcstombs_s(0, str, data, 256); // Convert wchar to string.
std::cout << str << std::endl; // Logs lowercase regardless of shift/caps lock.
虽然GetKeyboardState()
获取了所有键的当前状态,但它没有考虑shift,altgr或大写锁定,但效果很好。但是,如果我使用GetKeyState()
手动选中了shift来查看是否按下了shift,然后通过将keyboard_state
索引设置为VK_SHIFT
来修改0xff
,就像在C#示例中一样,它有效。
为什么?