locked
How do I input to another application? RRS feed

Answers

  • .NET offers us an easy way to input to another application using the SendKeys class.   However, there are some limitations:

    1)    The target application must be currently active.  Therefore, we must activate the target application before we perform SendKeys.  Windows API FindWindow can help us to retrieve the window handle of the target application, and the Windows API SetForegroundWindow function can help us to activate the specific application window.

    2)   SendKeys does not work for a disconnected session. 

    3)   SendKeys could yield unpredictable results  when the application is intended for international use and utilizes a variety of keyboards

    For more information on how to send different keys using SendKeys, please refer to the MSDN documentation:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

    Here is a code snippet to find the window of Notepad and input some text:

     

    [DllImport("user32.dll", EntryPoint = "FindWindow")]
            private static extern IntPtr FindWindow(string lp1, string lp2);
    
            [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
    
            private void button1_Click(object sender, EventArgs e)
            {
                // find window handle of Notepad
                IntPtr handle = FindWindow("Notepad", "Untitled - Notepad");
                if (!handle.Equals(IntPtr.Zero))
                {
                    // activate Notepad window
                    if (SetForegroundWindow(handle))
                    {
                        // send "Hello World!"
                        SendKeys.Send("Hello World!");
                        // send key "Tab"
                        SendKeys.Send("{TAB}");
                        // send key "Enter"
                        SendKeys.Send("{ENTER}");
                    }            
                }
            }
    
    

    Related thread:

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/2d38d219-b6ea-42d8-92fe-5af344ea820e

     

     

    We can also use Windows API SendMessage or PostMessage to send a WM_SETTEXT message to the corresponding window. 

    The fundamental difference between SendMessage and PostMessage is that SendMessage sends a message to another window by calling that window’s procedure and waiting for it to return, whereas PostMessage posts a message in the message queue associated with the thread that created the specified window and returns immediately without waiting. 

    For details on these two Windows APIs, we can refer to http://msdn.microsoft.com/en-us/magazine/cc301431.aspx.  

    Here is a code snippet using the SendMessage function to input text on Notepad.

    private const int WM_SETTEXT = 0x000C;
    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(
        string lpClassName,
        string lpWindowName);
    
    [DllImport("User32.dll")]
    private static extern IntPtr FindWindowEx(
        IntPtr hwndParent,
        IntPtr hwndChildAfter,
        string lpszClass,
    string lpszWindows);
    [DllImport("User32.dll")]
    private static extern Int32 SendMessage(
        IntPtr hWnd,               
        int Msg,                
        IntPtr wParam,             
    StringBuilder lParam);
    private void button1_Click(object sender, EventArgs e)
    {
        // retrieve Notepad main window handle
        IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad");
        if (!hWnd.Equals(IntPtr.Zero))
        {
            // retrieve Edit window handle of Notepad
            IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
            if (!edithWnd.Equals(IntPtr.Zero))
                // send WM_SETTEXT message with "Hello World!"
                SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("Hello World!"));
        }
    }
    
    

    Note:

    We can use SPY++ shipped with Visual Studio to track Windows messages and then mimic them with SendMessage or PostMessage.  
    For more information about how to use SPY++, please refer to the MSDN documentation:
    http://msdn.microsoft.com/en-us/library/aa264396(VS.60).aspx

    You can also refer to the MSDN magazine article by Benjamin Wulfe:
    http://msdn.microsoft.com/en-us/magazine/cc163617.aspx


    For more FAQ about Visual C# General, please see
    Visual C# General FAQ



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 8, 2009 7:02 AM