c# Sending keyboard commands to another window / process

73,812

Solution 1

[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

public void Start()
{
    IntPtr zero = IntPtr.Zero;
    for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
    {
        Thread.Sleep(500);
        zero = FindWindow(null, "YourWindowName");
    }
    if (zero != IntPtr.Zero)
    {
        SetForegroundWindow(zero);
        SendKeys.SendWait("{TAB}");
        SendKeys.SendWait("{TAB}");
        SendKeys.SendWait("{ENTER}");
        SendKeys.Flush();
    }
}

Try smth like this.

Solution 2

I've written a couple of programs that send keystrokes to background windows, I generally implemented PostMessage/SendMessage. I documented all my findings here!

But you will basically be using a low level c call to put messages into the windows message queue to allow the application to pick up the key presses.

PostMessage

SendMessage

Please let me know if you have any questions, my library is written in C# and i'd be happy to share it. This method also allows for mouse use in a background window :)

All code was checked into GitHub: https://github.com/EasyAsABC123/Keyboard

Solution 3

Considering that you know when and what keyboard command you gonna send to Outlook process, you need to use SendMessage Windows API function.

Just a sample

Share:
73,812
monkeylumps
Author by

monkeylumps

Updated on July 09, 2022

Comments

  • monkeylumps
    monkeylumps almost 2 years

    I am trying to write a program that will take a line of data and pass it into another window / process.

    This is the code I have so far, but I have not been able to work out how I would send the keyboard command to the OUTLOOK process.

    I would like to be able to use the Tab command / key and the Enter command / key.

    This is what I have tried so far

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Threading;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace Config
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                System.Threading.Thread.Sleep(30);//300000
                TextReader tr = new StreamReader("config.txt");
                Clipboard.SetText(tr.ReadLine());
                tr.Close();
    
                var proc = Process.GetProcessesByName("OUTLOOK").FirstOrDefault();
                if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
                {
                    SetForegroundWindow(proc.MainWindowHandle);
                    //SendKeys.Send("{ENTER}");
                    //   Clipboard.GetText();
                }
            }
    
            [DllImport("user32")]
            private static extern bool SetForegroundWindow(IntPtr hwnd);
        }
    }
    
  • arminb
    arminb over 12 years
    SendKeys doesn't work solid. Pretend an other window is being brought to the foreground. In this case you would send your keys to the wrong application.
  • pookie
    pookie almost 8 years
    Sorry for the resurrection, but any idea how I can do this --over the internet? I need to do essentially the same thing, but the target application/process is running on another machine!
  • user3790692
    user3790692 over 6 years
    Could anyone help, please? I did it and its working but i got a big problem. I can't use the form again. I created a form and put start and stop button. But i can't select the form to click stop... Any tip, please? Ty!!!!
  • deathrace
    deathrace almost 4 years
    What if I want to simulate ModifierKeys as well?
  • abc123
    abc123 almost 4 years
    @deathrace great question! this doesn't seem to always work since they are global modifiers from what i found anyways. So what i did was use global key down methods and then would press the background key and it would accept the modifier as pressed. Then i'd un press them foreground keystroke modifier. Normally keystrokes are so short that it didn't mess anything up...However, it always bothered me that it was as close as i could come without just injecting into the program a dll that listened for an api call for keystrokes.