Search code examples
c#keyboardsendkeyssendinput

My app won't recognize keystrokes sent from SendKeys or SendInput, but does from the Keyboard


I've got a homegrown app (Master) that has a bunch of hotkeys defined. I need to automate the app with another external app (Control). I cannot rebuild the Master app, it runs, is already installed and can't be messed with.

Using hotkeys on Master works like a charm if I use a keyboard.

So, I figure I an use SendKeys.SendWait or SendKeys.Send to send the commands. While my code works fine with other apps (such as Notepad) it is not working with my custom app, Master. This could be for a number of reasons - Master is older, has .NET, C++ mixed... some low level video controls... who knows what's under the covers.

I need to get something higher level. I need to be able to send the keystrokes as if they are coming from the keyboard itself. I don't want the computer to be able to differentiate between the physical keyboard sending the keys and my app sending the keys.

What can I use and how will it be different?


Solution

  • The problem was not SendKeys.

    I tried SendKeys.Send, SendKeys.SendWait, SendInput and keybd_event.

    I later realized that the time between activating the application and the time sending the keystrokes was too short. I was waiting 200ms but simply by increasing this to 500ms I solved my problem.

    The application took a long time to draw because of the embedded video players. Giving it another 300ms allowed it to be ready for the keystrokes I was sending it.