Hi everybody. I'm developing an application that sends keystrokes to another background application. The problem is that i have so send a "Alt+F", but i can't get it to work.
I'm using PostMessage:

[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

const int VK_ALT = 0x12;
const int WM_KEYDOWN = 0x100;

PostMessage(hWnd, WM_KEYDOWN, VK_ALT, 0);
PostMessage(hWnd, WM_KEYDOWN, VK_F, 0);

The result is that the background application "see" the "F", but no the "Alt".
Anybody has any idea how to fix it or how to do this without using SendKeys? Because the background application used to be minimized.

Greetings and thanks in advance. :)

6 Years
Discussion Span
Last Post by tincho87

According to WM_KEYDOWN Message you need to set bit 30 of the LParam to indicate that the previous key is still down.
Try this.

PostMessage(hWnd, WM_KEYDOWN, VK_ALT, 0);
PostMessage(hWnd, WM_KEYDOWN, VK_F, 0x40000000);

Hi, thanks for your reply, but doesn't seems to work :S
I'm trying to do this with a minimized notepad (for test), sending CTRL+F to open the "Search" window, but when i run the application, the notepad shows a "F" in the edit area. I don't know why it's ignoring the "CTRL" (and the "ALT" too).


Hi! I found the solution! it is a mix between a sendkeys and postmessage, that works :)

First, set the "ControlKey" to "active" (i think it's like if we "press" the keyboard controlkey), then send the PostMessage to the hWnd (application), and then restore the "ControlKey" state.

keybd_event(VK_CONTROL, (byte)MapVirtualKey(VK_CONTROL, 0), 0, 0); // Control Down
PostMessage(hWnd, WM_KEYDOWN, (uint)VK_F, 0); // F
keybd_event(VK_CONTROL, (byte)MapVirtualKey(VK_CONTROL, 0), 2, 0); // Control Up

Thanks to all for the replies. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.