C# WM_KEYDOWN :如何使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1169732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WM_KEYDOWN : how to use it?
提问by devoured elysium
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.
我正在尝试通过 PostMessage 向一个应用程序发送击键。我也在使用 Spy++ 来尝试了解如何发送消息,因为我不完全了解其内部工作原理。
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?
我想它一定与最后一个 PostMessage() 参数有关,但我无法弄清楚它是如何工作的。我可以在原始击键中看到 ScanCode = 48,在我的中是 0,fExtended 是 1,在我的中是 0。我怎样才能让它看起来一样?
In http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspxI cannot understand the last parameter's working.
在http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx 中,我无法理解最后一个参数的工作原理。
采纳答案by Kevin Montrose
Simulate keyboard input using SendInput, not PostMessage.
使用SendInput模拟键盘输入,而不是 PostMessage。
You can't simulate keyboard input with PostMessage.
There are still some caveatswith 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.
SendInput 函数不会重置键盘的当前状态。因此,如果用户在调用此函数时按下了任何键,则它们可能会干扰此函数生成的事件。如果您担心可能的干扰,请使用 GetAsyncKeyState 函数检查键盘的状态并根据需要进行更正。
The lParam for the WM_KEYDOWN Notificationis specified in terms of the bits of the field:
WM_KEYDOWN 通知的 lParam是根据字段的位指定的:
- 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)
- 前 16 位是重复计数
- 接下来的8位是扫描码
- 对于扩展密钥,下一位为 1,否则为 0
- 接下来的 4 位是保留的,必须为 0
- 下一位始终为 0(对于 WM_KEYDOWN)
- 下一位是之前的关键状态
- 最后一位始终为 0(对于 WM_KEYDOWN)
A warning: Any solution you build based around PostMessage is going to be very brittle.
警告:您基于 PostMessage 构建的任何解决方案都将非常脆弱。
回答by pdhoolia
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.
在 Spy++ 中,如果您右键单击突出显示的(记录的消息)条目并查看其属性,您可以看到 lParam 的确切值。然后,您可以将其用作 lParam 以确保 PostMessage 产生类似的效果,就像手动操作一样。
回答by Ohad Schneider
Take a look at http://inputsimulator.codeplex.com, it wraps the SendInput
method mentioned by Kevin
看一下http://inputsimulator.codeplex.com,它封装了SendInput
Kevin 提到的方法