1

我需要在游戏窗口中模拟按键。我尝试发送密钥“A”,但它不起作用:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

private void button1_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindow(null, "Game Name");  // it's work!

    if (hWnd == IntPtr.Zero)
    {
        MessageBox.Show("Game is not running");
        return;
    }

    SetForegroundWindow(hWnd);  // it's work too and now I have active window of game
    System.Threading.Thread.Sleep(3000);

    const int WM_KEYDOWN = 0x0100;
    PostMessage(hWnd, WM_KEYDOWN, (IntPtr)Keys.A, IntPtr.Zero);     // don't work ;-(
}
4

1 回答 1

4

你得到什么错误?游戏是否以管理员身份运行?您可能会被UIPI阻止。

于 2011-07-09T22:37:09.777 回答