tags:

views:

684

answers:

3

I have a window which is hidden and I would like to send a keypress to it. An example of what I'm trying to achieve is an app that will send the key F5 to a web browser which wasn't the active window. The web browser would know to refresh the current page when the F5 keystroke is received.

I would also like to send a combination of keys to an application, e.g. Ctrl+S. One example of this usage could be a timed auto-save feature to use with applications which don't have autosave. This would spare me having to remember to save every 5 mins.

C# is my technology, does this sound realistic?

A: 

You can use WinApi for this purpose. SendMessage or PostMessage method to send desired message to your application. Here's C# definition of SendMessage

  [DllImport("user32.dll")]
            public static extern int SendMessage(
                  int hWnd,      // handle to destination window
                  uint Msg,       // message
                  long wParam,  // first message parameter
                  long lParam   // second message parameter
                  );

and define the message that you want to send like:

    public const int %WM_MESSAGE_HERE% = %value%;

Check Pinvoke - great resource containing WinApi method definitions for .NET

Sorantis
How would I, for instance, send Ctrl+F5 using your example (ie both keys pressed together)?
James
+3  A: 

This CodeProject article shows how to send keystrokes to an external application (with C# source code).

Eric J.
A: 

This should be possible, look at www.pinvoke.net and search for sendinput, that page should tell you everything you need to know, also how to find the window (look for findwindow)

H4mm3rHead