Over a million developers have joined DZone.

Automatically Send Keys To Other Application

·

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ConsoleApplicationTest
{
    class Program
    {
        [System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        static void Main(string[] args)
        {
            Process calc = Process.Start("calc.exe");
            calc.WaitForInputIdle();

            IntPtr calculatorHandle = FindWindow(null, "Calculator");
            if (calculatorHandle == IntPtr.Zero)
            {
                Console.WriteLine("Calculator is not running.");
            }
            else
            {
                SetForegroundWindow(calculatorHandle);
                SendKeys.SendWait("13");
                SendKeys.SendWait("*");
                SendKeys.SendWait("13");
                SendKeys.SendWait("=");
            }
        }
    }
}
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}