C# SendKeys不使用VisualBoy Advance

C# SendKeys不使用VisualBoy Advance,c#,sendkeys,C#,Sendkeys,我试图模拟键盘输入,以编程方式在VisualBoy Advance中玩游戏。使用SendKeys.SendWait()时,VisualBoy Advance没有响应 如果我将进程名称换成另一个进程(如notepad++),则按键操作将非常有效。这让我相信VisualBoy Advance的进程或窗口一定是错误的,但在抓取所有进程并查看它们时,我没有找到一个看起来正确的进程或窗口。VisualBoy Advance和其他游戏程序通常不使用窗口消息中的键盘事件,相反,他们可能会使用HID输入API,

我试图模拟键盘输入,以编程方式在VisualBoy Advance中玩游戏。使用SendKeys.SendWait()时,VisualBoy Advance没有响应


如果我将进程名称换成另一个进程(如notepad++),则按键操作将非常有效。这让我相信VisualBoy Advance的进程或窗口一定是错误的,但在抓取所有进程并查看它们时,我没有找到一个看起来正确的进程或窗口。

VisualBoy Advance和其他游戏程序通常不使用窗口消息中的键盘事件,相反,他们可能会使用HID输入API,如DirectInput、XInput、SDL的输入API-这就是为什么使用
SendKeys
不起作用的原因。检查此问题:
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();
}