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);
    }
}
1

3 Answers 3

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();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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!
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!!!!
I found this and it was helpful for me : stackoverflow.com/questions/3047375/…
@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...
|
12

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

2 Comments

What if I want to simulate ModifierKeys as well?
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.