Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Sending keyboard commands to another window / process

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);
    }
}
like image 490
monkeylumps Avatar asked Dec 22 '11 14:12

monkeylumps


3 Answers

[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.

like image 57
Elastep Avatar answered Nov 06 '22 19:11

Elastep


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

like image 11
abc123 Avatar answered Nov 06 '22 20:11

abc123


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

like image 1
Tigran Avatar answered Nov 06 '22 20:11

Tigran