C# WPF Simulate KeyPress (User32 SendInput Wrapper)

Simulating keypress in Windows forms was very easy due to the availability of the Sendkeys.send method, but this is absent in WPF, even if you import the Windows.Forms namespace it still won't work and will recommend SendWait. I only just noticed recently when I needed to implement something similar. I finally got it working and this was how.

I had previously used user32.dll for moving mouse cursor programmatically but recently needed to also simulate keypresses and since SendKey was unavailable for WPF I had to resort to using the user32.dll's SendInput method, midway into that I stumbled on this awesome library, Windows Input Simulator which is a wrapper for user32.dll's SenInput and implements very convenient methods to simulate/trigger key-presses even text input.

You can download from here, unzip and import the inputsimulator.dll, and use the WindowsInput namespace and you're good to go. The readme from the author very easy to follow.

Give it a try, it's awesome. It saved me a lot of time instead of working all over again with SendInput, it was very helpful and time saving!!


Post a Comment