0

I am attempting to simulate keyboard input to programmatically play a game in VisualBoy Advance. There is no response from VisualBoy Advance when SendKeys.SendWait() is used.

private const string VBA_PROCESS_NAME = "VBA-rr-svn480";

public void Up()
{
    PressButton("{UP}");
}

public void Down()
{
    PressButton("{DOWN}");
}

public void Left()
{
    PressButton("{LEFT}");
}

public void Right()
{
    PressButton("{RIGHT}");
}

public void A()
{
    PressButton("z");
}

public void B()
{
    PressButton("x");
}

public void LShoulder()
{
    PressButton("a");
}

public void RShoulder()
{
    PressButton("s");
}

public void Start()
{
    PressButton("~");
}

public void Select()
{
    PressButton("+");
}

private void PressButton(string Button)
{
    var VBAProcess = GetVBAProcess();

    // Verify that VBA is a running process.
    if (VBAProcess == null)
        throw new Exception("Visual Boy Advance could not be found.");

    IntPtr VBAHandle = VBAProcess.MainWindowHandle;

    // Make sure that VBA is running and that we have a valid handle.
    if (VBAHandle == IntPtr.Zero)
        throw new Exception("Visual Boy Advance is not running.");

    // Make VBA the foreground application and send it the button press.
    SetForegroundWindow(VBAHandle);
    SendKeys.SendWait(Button);
}

// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

private Process GetVBAProcess()
{
    return Process.GetProcessesByName(VBA_PROCESS_NAME).FirstOrDefault();
}

If I swap out the process name for a different process (such as notepad++) the key presses work perfectly. This leads me to believe that I must have the wrong process or window for VisualBoy Advance, but I haven't found one that looks correct when I grab all processes and look through them.

Schizoreindeer
  • 229
  • 1
  • 3
  • 9
  • 2
    VisualBoy Advance, and other games programs, generally don't use keyboard events from window-messages, instead they're likely using a HID input API like DirectInput, XInput, SDL's input API - this is why using `SendKeys` won't work. – Dai Jul 23 '18 at 00:31
  • 1
    Check this question: https://stackoverflow.com/questions/409886/send-key-strokes-to-games-using-direct-input – Lucca Ferri Jul 23 '18 at 03:59

0 Answers0