开发者

Obtaining the virtual key code from a WM_CHAR message

开发者 https://www.devze.com 2023-04-11 03:16 出处:网络
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 messag开发者_JAVA百科es the have the same VK_ code as the key that is bound

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 messag开发者_JAVA百科es 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


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);


Perhaps you migh use MapVirtualKey.

I am not sure how to extract scancode from lparam as documentation does not state that - either get entire lparam and count that this function knows which bits to look at, or use bitfield struct and just get right bits out of it. I think on of those methods should work - trying both shouldn't be difficult.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号