24

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);
    }
}
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
monkeylumps
  • 747
  • 4
  • 10
  • 23
  • `SendMessage` is what you're looking for. Maybe your question was already solved here: https://stackoverflow.com/questions/523405/how-to-send-text-to-notepad-in-c-win32 – arminb Dec 22 '11 at 14:24

3 Answers3

19
[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();
    }
}
Michael Hoffmann
  • 2,411
  • 2
  • 24
  • 42
Elastep
  • 3,272
  • 1
  • 14
  • 16
  • 7
    `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. – arminb Dec 22 '11 at 15:04
  • 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! – pookie Jul 25 '16 at 15:29
  • 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!!!! – user3790692 Jan 24 '18 at 04:29
  • I found this and it was helpful for me : https://stackoverflow.com/questions/3047375/simulating-key-press-c-sharp – Fredy Sep 01 '22 at 18:16
  • @pookie Your question doesn't make much sense. What do you mean with "over the internet"? Computers are no magic. :) How can you press real keys on the other machine over the internet? That remote tool you have to send your keys to... – The incredible Jan Nov 15 '22 at 08:03
  • @TheincredibleJan this question is nearly 12 years old... – pookie Nov 16 '22 at 17:13
11

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

abc123
  • 17,855
  • 7
  • 52
  • 82
  • What if I want to simulate ModifierKeys as well? – deathrace Aug 20 '20 at 06:37
  • @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. – abc123 Sep 09 '20 at 20:19
1

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

Tigran
  • 61,654
  • 8
  • 86
  • 123