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

后端 未结 2 403
孤独总比滥情好
孤独总比滥情好 2020-12-09 06:46

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(         


        
相关标签:
2条回答
  • 2020-12-09 07:01

    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
    
    0 讨论(0)
  • 2020-12-09 07:23

    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.

    0 讨论(0)
提交回复
热议问题