views:

1543

answers:

4

Hello guys,

I know how to simulate mouse and keyboard events, but they act as if the user did them, so they will affect the window that is active. What I need is to simulate one of those inputs, but in a Window that is not active.

I'm not saying that it is minimized, imagine for example, you have msPaint, and notepad. Notepad is in front of paint. And you want to simulate mouse clicks in certain coordinates of the paint window, but without setting it active, making it possible for the user to keep using notepad which is in fron of paint.

Is this possible at all? Thanks!

I've tried this:

            const int WM_KEYDOWN = 0x100;
        Thread.Sleep(5000);
        Process x = Process.Start(@"C:\WINDOWS\NOTEPAD.EXE");
        PInvokes.PlatformInvokeUSER32.SendMessage(x.MainWindowHandle, WM_KEYDOWN, ((int)Keys.W), 0);

but it doesn't work =( Doesn't do anything :(

A: 

You can try the UI automation API. It supports also legacy applications.

David Suarez
A: 

Uh I would like to avoid that. I know that the api call sendMessage should work. I guess I'm doing something wrong here =/

Joao Oliveira
A: 

Maybe try the PostMessage function instead:

[DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern int PostMessage(int hWnd, int msg, int wParam, IntPtr lParam);

David Suarez
A: 

Depending on the target app, there could be lots of issues. Some apps trigger on WM_KEYDOWN or WM_KEYUP instead of WM_CHAR. Also, the key might already be down and is being ignored. For targets like webbrowsers, you need to get the right window handle. Winspector can get that for you.

Eyal