开发者

PostMessage does not work in Windows 7 x64

开发者 https://www.devze.com 2023-03-19 07:14 出处:网络
I need to simulate a keypress in game window. I try to send key \"A\", but it don\'t work: [DllImport(\"user32.dll\", SetLastError = true)]

I need to simulate a keypress in game window. I try to send key "A", but it don't work:

[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 = Fi开发者_如何学PythonndWindow(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 ;-(
}


What error do you get? Is the game running as administrator? You could be blocked by UIPI.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号