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.

4
  • Are those windows' thread different from your program's?
    – Skurmedel
    May 29, 2011 at 1: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, 2011 at 1:51
  • 2
    Why is this question tagged with 'C' and 'C++'?
    – johnsyweb
    May 29, 2011 at 2: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. May 29, 2011 at 7:47

2 Answers 2

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.

1
  • Awesome, exactly what I was looking for
    – KeatsPeeks
    Sep 16, 2011 at 18:24
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

2
  • 1
    Just a note, but SendMessage won't necessarily be asynchronous. You can use PostMessage to ensure faster execution.
    – user774895
    May 29, 2011 at 5:50
  • Note that you can't simulate keyboard input with PostMessage (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). Mar 31, 2016 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.