简体   繁体   English

从WM_CHAR消息中获取虚拟密钥代码

[英]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 我从WM_CHAR和WM_KEYDOWN消息中获取文本输入和按键输入

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. 我想要做的是过滤掉WM_CHAR消息,它们具有与绑定的键相同的VK_代码,以启用您输入文本的控件。

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. EG:Game使用〜启用控制台,键绑定通过VK_OEM3和WM_KEYDOWN完成,但输入控制台的文本需要来自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. 当WM_KEYDOWN首先发生时,控制台被激活,然后WM_CHAR为〜被发送到我不想要的控制台缓冲区。

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. 我认为防止这种情况的最佳方法是将WM_CHAR中的VK_与控件的绑定键进行比较并将其过滤掉。

Is there a way to get the VK_ from a WM_CHAR message? 有没有办法从WM_CHAR消息中获取VK_?

I read that you can get the scancode out of Lparam at bits 16-23 我读到你可以从第16-23位的Lparam中获取扫描码

But am unsure how to: 但我不确定如何:

  1. Extract the value of the scancode from lparam 从lparam中提取扫描码的值
  2. Translate the scan code to a VK_ correctly 正确地将扫描代码转换为VK_

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): 此代码将lParam的地址作为unsigned char数组(长度为一个字节),然后使用指针算术来寻址第3个字节(位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 . 也许你大概使用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. 我不确定如何从lparam中提取scancode,因为文档没有说明 - 要么获得整个lparam并计算该函数知道要查看哪些位,要么使用bitfield struct并从中获取正确的位。 I think on of those methods should work - trying both shouldn't be difficult. 我认为这些方法应该起作用 - 尝试两者都不应该是困难的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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