7

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!

Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • You cannot send keystrokes with SendMessage(), you can't control the state of the modifier keys. Hold the Ctrl key down for example. SendInput is required. – Hans Passant Apr 12 '11 at 23:58

2 Answers2

9

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
GregoryComer
  • 740
  • 8
  • 18
1

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.

Rob P.
  • 14,921
  • 14
  • 73
  • 109
  • This all has to run in the background so setting the app as active will defeat the purpose of my program. The client should be allowed to do things while this is going. Also, for the particular app, it, for some reason, doesn't receive input from sendkeys. – Freesnöw Apr 12 '11 at 22:45
  • Your link doesn't really help me. I'm not sure what to use as input for the function I have supplied. Could you possibly give me an example of it sending a common character? (Such as 'A')? – Freesnöw Apr 12 '11 at 22:46