I found this answer when looking for a way to convert a virtual key to a unicode character. It happened to be for c#, but the same method was available in C++, so I used it. This is my current code:
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.
This worked great, except it did not take shift, altgr, or caps lock into account, even though GetKeyboardState()
gets the current status of all keys. However, if I manually checked shift with GetKeyState()
to see if shift is pressed, and then modified keyboard_state
by setting the VK_SHIFT
index to 0xff
, like in the C# example, it works.
Why is that?