7

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.

Alex S
  • 85
  • 1
  • 3
  • Are those windows' thread different from your program's? – Skurmedel May 29 '11 at 01:48
  • they're in different process spaces, different programs.. different threads.. I have a program that wants to write to a minimized notepad. I just want to know if its possible to SendInput to a minimized inactive window from a separate program – Alex S May 29 '11 at 01:51
  • 2
    Why is this question tagged with 'C' and 'C++'? – johnsyweb May 29 '11 at 02:05
  • A minimised notepad window is pretty much equivalent to a text file. Also notepad is so basic you may as well put the text in a form in you own app with a multi kind edit box. Trying to poke text into another app is dirty. – David Heffernan May 29 '11 at 07:47

2 Answers2

8

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.

Skurmedel
  • 21,515
  • 5
  • 53
  • 66
2

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

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • 1
    Just a note, but SendMessage won't necessarily be asynchronous. You can use PostMessage to ensure faster execution. –  May 29 '11 at 05:50
  • Note that [you can't simulate keyboard input with PostMessage](https://blogs.msdn.microsoft.com/oldnewthing/20050530-11/?p=35513) (or SendMessage), because you can't control the actual keyboard state (shift keys, etc). I have tried exactly what you suggest, using Spy++ to see exactly the messages that are generated, and replaying those messages. In the end I could only send all lowercase letters because I could not actually set the shift key's state (sending WM_KEYDOWN/WM_KEYUP for VK_SHIFT doesn't work). – user1594322 Mar 31 '16 at 02:49