简体   繁体   中英

Detect keyboard input after windows recognition of the key

I am building a program that would print out the pressed buttons' values on the screen. I need it to work also when the program's window is not active. I tried using GetAsyncKeyState(int), which works fine. However, I can attach certain strings to keys, but it doesn't work for all types of keyboards (for example, on english keyboards shift+2 is @, while on mine its not). How can I detect post-processing of the key input that is done by Windows (the character that is printed on the screen when writing in Notepad)?

Try the OemKeyScan function. With yours you should have the scancode (eg the "2" key has a given scancode that is always the same depending on position on the keyboard, eg 17 for the "2" key):

    int whichscancode= 17;
    if (key_with_shift)
        whichscancode|= (1<<16);  // shifted values with 1<<16

    for (i=0; i<256; i++) {
            int scan= ::OemKeyScan(i);  // try all characters for a matching scancode
            if (scan==whichscancode) {
                // i here is the ascii code of the key on that position
                printf("on that scancode is character %c\n", (char)i);
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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