c++winapikeyboard

Obtaining the virtual key code from a WM_CHAR message


I am getting text input and keypress input from the WM_CHAR and WM_KEYDOWN messages

What I want to do is filter out WM_CHAR messages the have the same VK_ code as the key that is bound to enable the control that you enter text in.

EG: Game uses ~ to enable console, key binding is done via VK_OEM3 and WM_KEYDOWN, but text input into the console needs text from WM_CHAR.

As the WM_KEYDOWN happens first, the console is activated, then a WM_CHAR of ~ is sent to the console buffer which I don't want.

I figured the best way to prevent this is to compare the VK_ from the WM_CHAR to the bound key for the control and filter it out.

Is there a way to get the VK_ from a WM_CHAR message?

I read that you can get the scancode out of Lparam at bits 16-23

But am unsure how to:

  1. Extract the value of the scancode from lparam
  2. Translate the scan code to a VK_ correctly

Solution

  • After some messing around I managed to extract the virtual key with the following code:

    This code gets the address of lParam as a unsigned char array (one byte of length), then uses pointer arithmatic to address the 3rd byte (bits 16-23):

      unsigned char scancode = ((unsigned char*)&lParam)[2];
    

    This code translates from the scancode to the virtual key:

      unsigned int virtualKey = MapVirtualKey(scancode,MAPVK_VSC_TO_VK);