c# - SendKeys 不适用于 VisualBoy Advance
问题描述
我正在尝试模拟键盘输入以编程方式在 VisualBoy Advance 中玩游戏。使用 SendKeys.SendWait() 时 VisualBoy Advance 没有响应。
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();
}
如果我将进程名称换成不同的进程(例如记事本++),则按键工作完美。这让我相信我一定有错误的 VisualBoy Advance 进程或窗口,但是当我抓取所有进程并查看它们时,我还没有找到一个看起来正确的进程或窗口。