Why isn't VK_CODE corresponding to my keyboard input?

57 views Asked by At

I need to detect the keys pressed on my keyboard (further more also the mouse clicks and position) and for that I used Windows Hooks, more specifically MapVirtualKeyExA. However, the corresponding keys to the code documented at "Virtual key codes" does not refer to my own keyboard keys.

To exemplify, that's a code for showing-me the VK_CODE for the key I'm pressing:


#include <iostream>
#include <windows.h>

//Declarando o Hook:
LRESULT CALLBACK KBDHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    KBDLLHOOKSTRUCT *s = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
    switch (wParam){
        case WM_KEYDOWN:        
            std::cout << MapVirtualKey(s->vkCode, MAPVK_VK_TO_VSC) << '\n';         
            break;      
    }   
    //Libera o teclado para ser utilizado pelo sistema operacional novamente
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int main(){ 
    //Criando o hook: (WH_KWYBOARD_LL é o tipo de hook; ll significa Low Level)
    HHOOK kbd = SetWindowsHookEx(WH_KEYBOARD_LL, &KBDHook, 0, 0);   
    MSG message;
    while (GetMessage(&message, NULL, NULL, NULL) > 0){
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
    UnhookWindowsHookEx(kbd);
return 0;
}

With this code if I press Tab, I'll get the "15" decimal answer; What makes it impossible for me to detect the key with if(MapVirtualKey(s->vkCode, MAPVK_VK_TO_VSC) == VK_TAB){/*TAB Pressed*/} because "VK_TAB" corresponds to 9 decimal.

How could I properly detect my keyboard keys?

1

There are 1 answers

0
iumonyty On

I could check it by using only s->vkCode as parameter.