ProGrammer
ProGrammer

Reputation: 55

Simulate keybord input in c#

It has been asked before, but these questions are really old (7-10 years ago) and answers very long, and I imagine it can be done in less than 500 lines.

Simple thing - console application that simulates button press. I launch it, and for example after 5 seconds button V is pressed. If I have for example Notepad active, I will see V appear in editor.

How I would see it in code, its obviously fake code that won't work:

Keyboard.Press("V");

Upvotes: 1

Views: 274

Answers (1)

Ayub
Ayub

Reputation: 2375

Install-Package WindowsInput (🔗) and then use this code:

await Task.Run(async () =>
{
    while (true)
    {
        await Keyboard.PressAsync("V");
        await Task.Delay(5000);
    }

});

public class Keyboard
{
    public static async Task PressAsync(string key)
    {
        await Simulate.Events().Click(key).Invoke();
    }
}

Upvotes: 1

Related Questions