PostMessage does not work in Windows 7 x64
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.
精彩评论