inputsimulator icon indicating copy to clipboard operation
inputsimulator copied to clipboard

Bug in Windows Forms Apps (.Net 6)

Open RobinBalean opened this issue 2 years ago • 1 comments
trafficstars

To demonstrate this error, I created a new Windows Forms App with a text box and a button. After pressing the button the text in the text box is sent using InputSimulatorEx after a 5 Second delay like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void btn_send_Click(object sender, EventArgs e)
    {
        Thread.Sleep(5000);
        var text = tb_text.Text;
        InputSimulator sim = new();
        sim.Keyboard.TextEntry(text);
    }
}

Start the app and open a notepad.exe Window to display the text. Press the button and make notepad the active window.

Single characters work fine, but if my text is longer , only the first few characters of the text appear. If I start typing in the notepad window the rest of the text shows up. (Sometimes if I wait long enough it appears too.)

The text appears to be stuck inside a buffer, which somehow needs to be nudged to start pushing out letters again. Is there a way to do this? I tried calling SendKeys.Flush() after the TextEntry, but it didn't help.

RobinBalean avatar Aug 30 '23 14:08 RobinBalean

Answering my own question... It seems to work if you put a small sleep in between characters. Like this:

private void btn_send_Click(object sender, EventArgs e)
{
    Thread.Sleep(5000);
    var text = tb_text.Text;
    InputSimulator sim = new();
    foreach (var c in text)
    {
        sim.Keyboard.TextEntry(c);
        sim.Keyboard.Sleep(1);
    }
}

RobinBalean avatar Aug 30 '23 14:08 RobinBalean