简体   繁体   English

使用 WM_KEYDOWN 和 wParam

[英]Using WM_KEYDOWN and wParam

Noob question here, but I can't find any other threads that answer it.菜鸟问题在这里,但我找不到任何其他线程来回答它。 All I want to know is how to use the WM_KEYDOWN message, and then proceed to use its wParam parameter to check which key has been pressed.我只想知道如何使用 WM_KEYDOWN 消息,然后继续使用其 wParam 参数来检查按下了哪个键。 Like a lot of people, I'm using this to try and get windows to stop with the key-repeat delay.像很多人一样,我正在使用它来尝试让 Windows 停止按键重复延迟。

I can get my program to respond to WM_KEYDOWN with我可以让我的程序响应 WM_KEYDOWN

if (WM_KEYDOWN)
    //do something

But I can't work out how to access the wParam parameter.但我不知道如何访问 wParam 参数。 The code代码

if (WM_KEYDOWN)
    if (wParam == 'A')
        //do something

doesn't seem to work.似乎不起作用。

This kind of code is way out of my depth, but people are saying it's the easiest way to get around the key-repeat delay.这种代码超出了我的理解,但人们说这是解决关键重复延迟的最简单方法。 I don't really understand what a message is or how it can have a parameter.我真的不明白消息是什么或者它如何可以有一个参数。

As was pointed out in the comments you probably meant to write正如您可能打算写的评论中指出的那样

if( message == WM_KEYDOWN )

since因为

if( WM_KEYDOWN )

will always be true since it's a constant.将永远为真,因为它是一个常数。

As for the wparam check if I recall correctly you need to compare it to a lowercase character not an uppercase one.至于 wparam 检查,如果我没记错的话,您需要将它与小写字符而不是大写字符进行比较。

This is a very old question as there were some changes to MSDN, however I will try to cover the multiple scenarios.这是一个非常古老的问题,因为 MSDN 有一些变化,但是我将尝试涵盖多种情况。

Scenario A情景A

You're capturing a Message Callback through a Windows Procedure without using SetwindowsHookEx .您在不使用SetwindowsHookEx 的情况下通过 Windows 过程捕获消息回调。 Example: You're capturing messages inside WNDPROC .示例:您正在WNDPROC 中捕获消息。 First you need to detect the message type as in (msg == WM_KEYDOWN) .首先,您需要检测(msg == WM_KEYDOWN)的消息类型。 msg contains the event that triggered the call. msg包含触发调用的事件。 This way you can utilize wParam where it will be the Virtual Key code.通过这种方式,您可以使用wParam ,它将作为虚拟密钥代码。

Scenario B情景B

You're capturing a Message Callback through Low Level Keyboard Procedure ( WH_KEYBOARD_LL ).您正在通过低级键盘程序 ( WH_KEYBOARD_LL ) 捕获消息回调。 In this scenario wParam acts as msg from Scenario A , and lParam contains additional info.在这个场景中, wParam充当Scenario A msg ,而lParam包含附加信息。 You'll need to map lParam into a KBDLLHOOKSTRUCT and extract the Virtual Key code from it.您需要将lParam映射到KBDLLHOOKSTRUCT并从中提取虚拟密钥代码。 KBDLLHOOKSTRUCT kbStruct = *((KBDLLHOOKSTRUCT*)lParam); You can now access the Virtual Key code by using kbStruct.vkCode .您现在可以使用kbStruct.vkCode访问虚拟密钥代码。

Scenario C情景C

You're capturing a Message Callback through Keyboard Procedure ( WH_KEYBOARD ).您正在通过键盘过程 ( WH_KEYBOARD ) 捕获消息回调。 In this scenario wParam contains your Virtual Key code, and lParam contains the flags.在这种情况下, wParam包含您的虚拟密钥代码,lParam 包含标志。 Refer to MSDN for more info the flags.有关标志的更多信息,请参阅 MSDN。 In order to detect Key Down/Up and/or Holding the button, you'll have to rely on the flags on bits 31 and 30 ( lParam>>31 and lParam&0x40000000 ).为了检测按键按下/按下和/或按住按钮,您必须依赖第 31 位和第 30 位( lParam>>31lParam&0x40000000 )上的标志。

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

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