简体   繁体   中英

WM_KEYDOWN : how to use it?

I'm trying to send a key stroke to one application, through PostMessage. I am using too Spy++ to try to understand how to send the message, as I do not fully understand its inner workings.

替代文字

In this picture, the first item(selected item) was made with an actual key stroke made by myself. The one with a red elipse around it(below) was made with the following code:

WinApi.PostMessage(InsideLobbyHandle, WinApi.WM_KEYDOWN, (int)WinApi.VK_UP, 1);

I guess it must have something to do with the last PostMessage() parameter, but I can't figure out how it really works. I can see in the original key stroke the ScanCode = 48, and in mine its 0, and also fExtended is 1 and in mine is 0. How can I make it look the same?

In http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx I cannot understand the last parameter's working.

Simulate keyboard input using SendInput , not PostMessage.

You can't simulate keyboard input with PostMessage .

There are still some caveats with respect to keyboard state/async-state:

The SendInput function does not reset the keyboard's current state. Therefore, if the user has any keys pressed when you call this function, they might interfere with the events that this function generates. If you are concerned about possible interference, check the keyboard's state with the GetAsyncKeyState function and correct as necessary.


The lParam for the WM_KEYDOWN Notification is specified in terms of the bits of the field:

  • The first 16 bits are the repeat count
  • The next 8 bits are the scan code
  • The next bit is 1 for extended key, 0 otherwise
  • The next 4 bits are reserved and must be 0
  • The next bit is always 0 (for WM_KEYDOWN)
  • The next bit is the previous key state
  • The last bit is always 0 (for WM_KEYDOWN)

A warning: Any solution you build based around PostMessage is going to be very brittle.

看一下http://inputsimulator.codeplex.com ,它包装了Kevin提到的SendInput方法

In Spy++ if you right click on the highlighted (logged message) entry and look at its properties, You can see the exact value of the lParam. You can then use that as your lParam to ensure that the PostMessage leads to similar effects, as the manual action did.

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