954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with Send Keys to background process

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. :)

tincho87
Newbie Poster
7 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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);
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

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).

tincho87
Newbie Poster
7 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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. :)

tincho87
Newbie Poster
7 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: