Reputation: 213
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 :(
Upvotes: 3
Views: 3024
Reputation: 1140
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);
Upvotes: 2
Reputation: 812
Your code wont work because SendMessage requires that the window you're sending to be active. Try PostMessage.
Upvotes: 0
Reputation: 5848
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.
Upvotes: 1
Reputation: 1140
You can try the UI automation API. It supports also legacy applications.
Upvotes: 2