简体   繁体   中英

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. Like a lot of people, I'm using this to try and get windows to stop with the key-repeat delay.

I can get my program to respond to WM_KEYDOWN with

if (WM_KEYDOWN)
    //do something

But I can't work out how to access the wParam parameter. 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.

This is a very old question as there were some changes to MSDN, however I will try to cover the multiple scenarios.

Scenario A

You're capturing a Message Callback through a Windows Procedure without using SetwindowsHookEx . Example: You're capturing messages inside WNDPROC . First you need to detect the message type as in (msg == WM_KEYDOWN) . msg contains the event that triggered the call. This way you can utilize wParam where it will be the Virtual Key code.

Scenario B

You're capturing a Message Callback through Low Level Keyboard Procedure ( WH_KEYBOARD_LL ). In this scenario wParam acts as msg from Scenario A , and lParam contains additional info. You'll need to map lParam into a KBDLLHOOKSTRUCT and extract the Virtual Key code from it. KBDLLHOOKSTRUCT kbStruct = *((KBDLLHOOKSTRUCT*)lParam); You can now access the Virtual Key code by using kbStruct.vkCode .

Scenario C

You're capturing a Message Callback through Keyboard Procedure ( WH_KEYBOARD ). In this scenario wParam contains your Virtual Key code, and lParam contains the flags. Refer to MSDN for more info the flags. 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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