Search code examples
c#sendkeys

C# SendKeys.SendWait() doesn't always work


I am trying to make an application that sends keys to an external application, in this case aerofly FS. I have previously used the SendKeys.SendWait() method with success, but this time, it doesn't quite work the way I want it to.

I want to send a "G" keystroke to the application and testing it out with Notepad I do get G's. But in aerofly FS nothing is received at all. Pressing G on the keyboard does work though.

This is my code handling input data (from an Arduino) an sending the keystrokes,

private void handleData(string curData)
{
    if (curData == "1")
        SendKeys.SendWait("G");
    else
    { }
}

Solution

  • I too have run into external applications where SendKeys didn't work for me.

    As best I can tell, some applications, like applets inside a browser, expect to receive the key down, followed by a pause, followed by a key up, which I don't think can be done with SendKeys.

    I have been using a C# wrapper to the AutoIt Library, and have found it quite easy to use.

    Here's a link to quick guide I wrote for integrating AutoIt into a C# project.

    Once you have the wrapper and references, you can send "G" with the following:

    private void pressG()
    {
       AutoItX3Declarations.AU3_Send("{g}");
    }
    

    or with a pause,

    private void pressG()
    {
       AutoItX3Declarations.AU3_Send("{g down}", 0);
       AutoItX3Declarations.AU3_Sleep( 50 );          //wait 50 milliseconds
       AutoItX3Declarations.AU3_Send("{g up}", 0);
    }
    

    AutoIt also allows you programmatically control the mouse.