2

I'm trying to create an application that will send a keystroke to an application (by process or window) in the background at a set interval(ms). I've found a few different answers on this topic but I need an ELI5 (explain like I'm 5) answer. As a test I used the following code I found to send keys to notepad.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TextSendKeys
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        static void Main(string[] args)
        {
            Process notepad = new Process();
            notepad.StartInfo.FileName = @"C:\Windows\Notepad.exe";
            notepad.Start();

            // Need to wait for notepad to start
            notepad.WaitForInputIdle();

            IntPtr p = notepad.MainWindowHandle;
            ShowWindow(p, 1);
            SendKeys.SendWait("ABC");
        }
    }
}

This code allows me to send the keystrokes to notepad. It opens notepad, but can't do it in the background.

Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace TextSendKeys
{
    class Program
    {

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

    static void Main(string[] args)
    {
        Process[] processes = Process.GetProcessesByName("process name here");
        Process game1 = processes[0];

        IntPtr p = game1.MainWindowHandle;

        SetForegroundWindow(p);
        SendKeys.SendWait("{1}");
        Thread.Sleep(1000);
        SendKeys.SendWait("{1}");

        }
    }
}
  • That fixed the error, thank you. I'm still now trying to send the keys to the program in the background - I've edited my original question. – Advancin Mar 12 '14 at 16:43
  • Which answers did you find, and what didn't you understand about them? This prevents any of the new answers here from reiterating the other answers you did find ... – Steven Jeuris Mar 12 '14 at 16:48
  • I'm a bit uncertain what you refer to when mentioning 'background'. Could you clarify? – Steven Jeuris Mar 12 '14 at 16:48
  • Background = not a focused window. – Advancin Mar 12 '14 at 16:55
  • I've found my answer here and updated my original question with the correct answer. stackoverflow.com/questions/8953399/… – Advancin Mar 12 '14 at 17:23
2

Found the answer at C# // SendKeys.SendWait works only when process'es window is minimzed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace TextSendKeys
{
    class Program
    {

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

    static void Main(string[] args)
    {
        Process[] processes = Process.GetProcessesByName("process name here");
        Process game1 = processes[0];

        IntPtr p = game1.MainWindowHandle;

        SetForegroundWindow(p);
        SendKeys.SendWait("{1}");
        Thread.Sleep(1000);
        SendKeys.SendWait("{1}");

        }
    }
}
| improve this answer | |
  • here need focus. but work. thx. – BREI May 18 '18 at 2:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.