InputSimulator Keyboard.TextEntry is not working

42 Views Asked by At

I'm trying to open a notepad instance and write a virtual text entry to it. The notepad opens securely but nothing is written on it. What would be the problem and why?

using System;
using System.Diagnostics;
using WindowsInput;
using WindowsInput.Native;

class Program
{
    static void Main()
    {
        try
        {
            Process notepadProcess = new Process();

            notepadProcess.StartInfo.FileName = "notepad.exe";

            notepadProcess.Start();

            InputSimulator simulator = new InputSimulator();

            simulator.Keyboard.TextEntry("This is a message that will be written to Notepad");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error opening Notepad: " + ex.Message);
        }
    }
}

I tried to pause the execution thread to allow time for the notepad to start, but I only managed to get it typed if I moved the mouse.

1

There are 1 best solutions below

0
Micke On

If you cannot tell InputSimulator which process the text should be sent to, the focus must be set to the recipient notepadProcess (before sending). It should be noted that Notepad consists of a control for the menu and a control for the edit area. The focus of your text must be on the latter. You set this with your mouse click.