Read specific key from another application textbox
<p>I need to know when the enter key is pressed on a specific textbox in another application. I'm able to find the textbox and write/read text from it using the user32.dll. But how do I get one specific key?</p>

<p>The application is just a chat. The goal is to when I type on this application textbox something like '/time' and hit enter I want my application to read this command and output the current time, for exemple.</p>

<p>Here is the code I have to retrieve the handle of the application and its textfield child and writing/reading it.</p>

<pre><code>    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, string lParam);

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);

    [DllImport("user32.dll")]
    private static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
    [DllImport("user32.Dll")]
    public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);

    [DllImport("user32.dll")]
    static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);

    private IntPtr handle = IntPtr.Zero;
    private string childClassName = "TComboEdit";

    public bool findChildHandle(IntPtr hwnd, IntPtr lParam)
    {
        StringBuilder className = new StringBuilder();
        GetClassName(hwnd, className, 120);
        if (className.ToString() == childClassName)
        {
            handle = hwnd;
            return false;
        }
        return true;
    }

    private void findControl(string className, string title)
    {
        IntPtr application = IntPtr.Zero;
        application = FindWindow(className, title);
        if (application == IntPtr.Zero)
            MessageBox.Show("Aplicativo não encontrado");
        else
            EnumChildWindows(application, findChildHandle, IntPtr.Zero);
    }

    private void setTextToHandle(string msg)
    {
        SendMessage(handle, 0x000c, IntPtr.Zero, msg); //set text
        PostMessage(handle, 0x0100, new IntPtr(0x0D), IntPtr.Zero); // key down (enter)
    }

    private void getTextFromHandle()
    {
        StringBuilder t = new StringBuilder();
        SendMessage(handle, 0x0D, 100, t); //get text
        MessageBox.Show(t.ToString());
    }
</code></pre>
