Alex S
Alex S

Reputation: 95

SendInput to minimized window

Is it possible to utilize the sendInput function on windows that currently do not have focus, and maybe through the use of multithreading, sendinput to multiple minimized windows at the same time, or send input to one window while you're working on another window?

I'd like to do something like this in c#

thanks in advance.

Upvotes: 8

Views: 9489

Answers (2)

Skurmedel
Skurmedel

Reputation: 22159

You can only use SendInput to send input to the HWND with keyboard focus. Furthermore the window must be attached to the calling thread's message queue, so one cannot simply SetFocus either.

You'll need to get the window's thread id with GetProcessIdOfThread.

When you have the thread id you can use the AttachThreadInput function to attach your thread to the other threads input processing.

After all this you can probably use SetFocus and SendInput.

You'll probably want to detach your thread when you've sent your input.

To get access to these method you'll have to use P/Invoke for C# or C++/CLI. PInvoke.net is very handy as a reference. It will be a small chore importing all those functions, but when you are done you should be able to send input to whatever "window" you want.

Also as a side note, I'm not sure if you are aware of this, but in pure Win32 everything is regarded as a window, even a button. If you are unlucky you may have to send the input to the handle of the text control belonging to the notepad application.

Upvotes: 7

Mike Kwan
Mike Kwan

Reputation: 24447

That is not possible with SendInput. What you probably want to do is find the messages that were sent to the window by the OS when that particular event was performed then emulate them. You can use Spy++ to attach to the target window and perform your event. Then use SendMessage() and PostMessage() to reproduce the messages that were generated by your event. This will work fine for notepad.

If you do use this method, note that you need to send messages to notepad's child window which you can find with FindWindowEx() with a classname of "edit". For example to type text you could try WM_KEYDOWN. You should note that this method is not necessarily reliable: http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx

Upvotes: 2

Related Questions