Sending an application keystrokes with “SendMessage” (vb.net)

时光毁灭记忆、已成空白 提交于 2019-12-17 18:29:55

问题


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!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5641869/sending-an-application-keystrokes-with-sendmessage-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!