PostMessage не работает в Windows 7 x64 - PullRequest
1 голос
/ 10 июля 2011

Мне нужно смоделировать нажатие клавиши в окне игры.Я пытаюсь отправить ключ "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 ;-(
}

1 Ответ

4 голосов
/ 10 июля 2011

Какую ошибку вы получаете? Игра работает от имени администратора? Вы можете быть заблокированы UIPI .

...