Hey guys ive been working on my first c# program and im finding it rather difficult at the moment. I have got as far as the code below, But i am having trouble getting it to send key strokes to either a specified process or a non active window by window title or process name.

These past few days ive been searching and found that i need to re-write my code to use different API's, But i was hoping there may be a short code snippet i can put at the top of the code that will send to only a certain process name.

I would like the key strokes to be sent while the window is minimized / not active.

Example, :
if process name = notepad, sendkeys;
if process name is not notepad, do nothing


What my program does is.

#1 - Checks if notepad is open, If not, disabled button1
#2 - If notpad is open, Every 6 seconds it sends the keys that i input into the textbox

This is my current code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.timer1.Interval = 6000; 
            this.timer1.Enabled = false;
            timer2.Enabled = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private Int32 state = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            SendKeys.Send(this.textBox1.Text);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            {
                System.Diagnostics.Process[] myprocess = System.Diagnostics.Process.GetProcessesByName("notepad");
                if (myprocess.Length != 0)
                {
                    button1.Enabled = true;
                }
                else
                {
                    button1.Enabled = false;
                }
            }

        }

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

The window has to be active. Look at the documentation for the SendKeys class: SendKeys Class

[DllImport("User32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public static void SendKeysToNotePad()
        {
            try
            {
                // Start notepad...
                Process myProcess = Process.Start(@"notepad.exe");

                // Make the window active...
                SetForegroundWindow(myProcess.Handle);

                // Check to see it process is responding...
                if (myProcess.Responding)
                    // Send keys to the ACTIVE application...
                    SendKeys.SendWait("This text being sent to notepad.exe from " + Application.ProductName);
                
                // not responding... shut it down...
                else
                    myProcess.Kill();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

The window has to be active. Look at the documentation for the SendKeys class: SendKeys Class

umm i know...Hence why i asked here what i can do to make it send to a non-active window....

The window has to be active. Look at the documentation for the SendKeys class: SendKeys Class

umm i know...Hence why i asked here what i can do to make it send to a non-active window....

Sorry, wasn't sure whether you just wanted to get SendKeys to work with it so I was showing how to make it active. Don't know another way to do it, but maybe someone else does. Good luck.