使用“SendMessage”发送应用程序击键(vb.net)

发布于 2024-11-01 01:49:31 字数 600 浏览 1 评论 0原文

到目前为止,我已经设置了所有句柄捕获和 GUI。我对如何执行实际步骤感到困惑。

我有这段代码:

SendMessage(New IntPtr(CurrentHandle), WHAT,GOES,HERE?)

我一直在看: http://msdn.microsoft.com/en-us/库/ms644950(VS.85).aspxhttp://msdn.microsoft.com/ en-us/library/ms644927(v=VS.85).aspx#system_define

但是,这些都没有提供我需要学习如何执行此操作的大量“代码示例”方法。我只需要发送按键事件,例如按“/”或“w”等。不,我不能为此使用sendkeys。

如果您能提供帮助,谢谢!

So far, I have all the handle capturing and gui set up. I'm stumped as to how to perform the actual step.

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).aspx
and
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.

Thanks if you can help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无敌元气妹 2024-11-08 01:49:31

要模拟按键,您需要模拟 keydown 和 keyup 事件,这将是您在 Msg 字段中指定的事件。 (使用 256 表示 keydown,使用 257 表示 keyup)。 wParam 和 lParam 是特定于消息的,因此对于 keyup 和 keydown,wParam 将是关键代码 (请参阅此页了解十六进制代码),lParam 包含其他杂项信息 (查看此页面)。在 vb.net 中,您可以使用 int32 作为 lParam。例如,您可以使用 0 表示 keydown,使用 65539 ​​表示 keyup。

前任:

SendMessage(New IntPtr(CurrentHandle), 256, KEYCODE, 0) - Keydown
SendMessage(New IntPtr(CurrentHandle), 257, KEYCODE, 65539) - Keyup

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 page for 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.

Ex:

SendMessage(New IntPtr(CurrentHandle), 256, KEYCODE, 0) - Keydown
SendMessage(New IntPtr(CurrentHandle), 257, KEYCODE, 65539) - Keyup
煮茶煮酒煮时光 2024-11-08 01:49:31

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 - 是发送消息的窗口句柄。
Msg - 是要发送的消息类型。
WParam 和 lParam 本质上是“信息”。确切的用途取决于您发送的消息。

您在什么情况下需要使用 SendMessage 而不是 SendKeys 来模拟按键?我以前用过SendMessage,但它总是用于鼠标移动。 .SendKeys() 应该将您告诉它的任何击键发送到活动窗口。

Public Shared Sub ActivateWin()
    Dim Win As Process = Process.GetProcessesByName("myWindow").First
    AppActivate(Win.Id)
End Sub

我在 SendKeys() 之前立即使用了上面的内容,并且它总是有效。

如果这不起作用,或者您想为了使用 SendMessage 而使用 SendMessage;您需要 WM_KEYDOWN 消息的文档。 http://msdn.microsoft.com/en- us/library/ms646280(v=vs.85).aspx

您将操作位来创建正确的 lParam 值。

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.

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.

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.

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

You'll be manipulating bits to create the correct lParam value.

动听の歌 2024-11-08 01:49:31

这是发送常见字符(例如“A”)的示例。

res:= WinApi.SendMessageW (console, WinApi.WM_CHAR, ORD('A'), 0);

发送 Ctrl-A 则是另一回事了。您可能需要 WinApi.SendInput 函数。

Here is an example of sending a common character (Such as 'A')

res:= WinApi.SendMessageW (console, WinApi.WM_CHAR, ORD('A'), 0);

To send Ctrl-A is another story. You Would probably need WinApi.SendInput function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文