使用“SendMessage”(vb.net)发送应用程序击键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5641869/
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
Sending an application keystrokes with "SendMessage" (vb.net)
提问by Freesn?w
So far, I have all the handle capturing and gui set up. I'm stumped as to how to perform the actual step.
到目前为止,我已经完成了所有的句柄捕获和 gui 设置。我很难过如何执行实际步骤。
I have this code:
我有这个代码:
SendMessage(New IntPtr(CurrentHandle), WHAT,GOES,HERE?)
I've been looking at: http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspxand http://msdn.microsoft.com/en-us/library/ms644927(v=VS.85).aspx#system_defined
我一直在看:http: //msdn.microsoft.com/en-us/library/ms644950( VS.85).aspx和 http://msdn.microsoft.com/en-us/library/ms644927( v=VS.85).aspx#system_defined
However, none of these are giving much of the "code example" method that I need to learn how to do it. I just need to send key events such as pressing "/" or "w", etc. No, I can't use sendkeys for this.
然而,这些都没有给出我需要学习如何去做的大部分“代码示例”方法。我只需要发送按键事件,例如按“/”或“w”等。不,我不能为此使用 sendkeys。
Thanks if you can help!
如果你能帮忙,谢谢!
回答by GregoryComer
To simulate a keypress, you would need to simulate a keydown and keyup event, which would be what you specify in the Msg field. (Use 256 for keydown and 257 for keyup). wParam and lParam are message-specific, so for keyup and keydown, wParam would be the key code (See this pagefor the hexadecimal codes) and lParam contains other miscellaneous information (see this page). In vb.net you can use an int32 for lParam. For example, you can use 0 for keydown and 65539 for keyup.
要模拟按键,您需要模拟 keydown 和 keyup 事件,这将是您在 Msg 字段中指定的内容。(keydown 使用 256,keyup 使用 257)。wParam 和 lParam 是特定于消息的,因此对于 keyup 和 keydown,wParam 将是关键代码(有关十六进制代码,请参阅此页面)并且 lParam 包含其他杂项信息(请参阅此页面)。在 vb.net 中,您可以为 lParam 使用 int32。例如,您可以将 0 用于 keydown,将 65539 用于 keyup。
Ex:
前任:
SendMessage(New IntPtr(CurrentHandle), 256, KEYCODE, 0) - Keydown
SendMessage(New IntPtr(CurrentHandle), 257, KEYCODE, 65539) - Keyup
回答by Rob P.
http://msdn.microsoft.com/en-us/library/ms644950(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms644950(v=vs.85).aspx
LRESULT WINAPI SendMessage(
__in HWND hWnd,
__in UINT Msg,
__in WPARAM wParam,
__in LPARAM lParam
);
hWnd - is the handle of the window to send the message. Msg - is the message type to send. WParam and lParam are essentially 'information'. The exact use will depend on the message you are sending.
hWnd - 是发送消息的窗口句柄。Msg - 是要发送的消息类型。WParam 和 lParam 本质上是“信息”。具体用途取决于您发送的消息。
What situation are you in that you need to use SendMessage instead of SendKeys to emulate keypresses? I've used SendMessage before, but it's always been for mouse movements. .SendKeys() should send whatever keystroke you tell it to the active window.
你在什么情况下需要使用 SendMessage 而不是 SendKeys 来模拟按键?我以前使用过 SendMessage,但它一直用于鼠标移动。.SendKeys() 应该将您告诉它的任何击键发送到活动窗口。
Public Shared Sub ActivateWin()
Dim Win As Process = Process.GetProcessesByName("myWindow").First
AppActivate(Win.Id)
End Sub
I've used the above immediately before SendKeys() and it's always worked.
我在 SendKeys() 之前立即使用了上述内容,并且一直有效。
If that doesn't work, or you want to use SendMessage for the sake of using SendMessage; the documentation for WM_KEYDOWN message is what you need. http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx
如果这不起作用,或者您想使用 SendMessage 以使用 SendMessage;WM_KEYDOWN 消息的文档正是您所需要的。 http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx
You'll be manipulating bits to create the correct lParam value.
您将操作位以创建正确的 lParam 值。