Sending an application keystrokes with "SendMessage" (vb.net)
Asked Answered
D

3

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!

Darton answered 12/4, 2011 at 21:31 Comment(1)
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.Matador
M
10

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
Materi answered 12/4, 2011 at 22:46 Comment(2)
Look at this site: linkMateri
Wow, thank you so much. (Looks at reputation) Welcome to Stackoverflow! I hope you enjoy the site!Qatar
V
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.

Vegetation answered 12/4, 2011 at 22:32 Comment(2)
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.Qatar
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')?Qatar
S
0

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.

Stambul answered 3/5 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.