Question

I'm using the following code to get an unicode string of a keyboard state:

std::wstring App::DecodeMessage(KBDLLHOOKSTRUCT* kbHook) {
    // Clean up the keyboard state
    for(int i=0; i<256; ++i) keyboardMap[i] = 0;

    // Get the state of all the virtual keys
    GetKeyboardState(keyboardMap);

    // Then we get the current layout setting
    HKL kbdLayout = GetKeyboardLayout(0);

    // We create the buffer to receive the unicode chars
    std::vector<wchar_t> buffer;
    buffer.resize(257);
    buffer.assign(257, L'\0');

    // And finally we translate all this to an unicode char
    int numberOfChars = ToUnicode(kbHook->vkCode, kbHook->scanCode, keyboardMap, &buffer[0], 256, 0);

    if(numberOfChars >= -1 && numberOfChars <= 0) return std::wstring(L"");

    return std::wstring(&buffer[0], numberOfChars);
}

My keyboard layout is US-INTL, and without the app running, when i press "'" (simple quote), and on a second keystroke i press "a", i get á. However, with this function, when i press "'" (simple quote again) i actually get ANOTHER single quote in whatever app i'm focused. Also, it doesn't seem to get the encoding right, as it won't record á. I'm clueless, can anyone help?

Was it helpful?

Solution

So i finally sorted it out to not using ToUnicode. I ended up using the solution created by Marc-André Moreau, found available at http://keymagic.googlecode.com/svn-history/r112/trunk/KeyMagicDll/kbdext.cpp, if anyone is interested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top