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. :)
-
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. :)
-
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. :)
I think the problem is, that you send an ALT key but not left or right ALT, what is a big difference for the OS. VK_ALT is only used in user application the simplify some things.
1. send VK_LEFTALT = 0xA4 or VK_RIGHTALT = 0xA5
or
2. use SendKeys.Send
or
3. Have a look at (low-level) windows key hooking (SetWindowsHookEx). This helps the understand how key strokes are queued in the windows messaging system.
Greetings Covean
-
hi, i have in mind to use the lPama for the modifier controls
PostMessage(hWnd, WM_KEYDOWN, VK_F, VK_ALT);
greetz
Thanks for both quick replies, but it doesn't seems to work :| Is there other way to do this? I can't use sendkeys because the application is minimized. Thanks
-
Thanks for both quick replies, but it doesn't seems to work :| Is there other way to do this? I can't use sendkeys because the application is minimized. Thanks
If you understand C/C++ here is an old example where I send Ctrl+f to some window (hWnd).
HWND hOldForegroundWindow = ::GetForegroundWindow(); ::SetForegroundWindow(hWnd); ::ShowWindow(hWnd, SW\_MAXIMIZE); ::SetFocus(hWnd); Sleep(100); INPUT inputs\[2\]; memset(&inputs\[0\], 0, sizeof(INPUT) \* 2); inputs\[0\].type = INPUT\_KEYBOARD; inputs\[0\].ki.wVk = VK\_CONTROL; inputs\[1\].type = INPUT\_KEYBOARD; inputs\[1\].ki.wVk = 70; // f ::SendInput(2, &inputs\[0\], sizeof(INPUT)); Sleep(100); inputs\[0\].ki.dwFlags = KEYEVENTF\_KEYUP; inputs\[1\].ki.dwFlags = KEYEVENTF\_KEYUP; ::SendInput(2, &inputs\[0\], sizeof(INPUT));
Maybe in your case you just have to wait some short period of time and send a KEYUP event.
Greetings Covean
-
If you understand C/C++ here is an old example where I send Ctrl+f to some window (hWnd).
HWND hOldForegroundWindow = ::GetForegroundWindow(); ::SetForegroundWindow(hWnd); ::ShowWindow(hWnd, SW\_MAXIMIZE); ::SetFocus(hWnd); Sleep(100); INPUT inputs\[2\]; memset(&inputs\[0\], 0, sizeof(INPUT) \* 2); inputs\[0\].type = INPUT\_KEYBOARD; inputs\[0\].ki.wVk = VK\_CONTROL; inputs\[1\].type = INPUT\_KEYBOARD; inputs\[1\].ki.wVk = 70; // f ::SendInput(2, &inputs\[0\], sizeof(INPUT)); Sleep(100); inputs\[0\].ki.dwFlags = KEYEVENTF\_KEYUP; inputs\[1\].ki.dwFlags = KEYEVENTF\_KEYUP; ::SendInput(2, &inputs\[0\], sizeof(INPUT));
Maybe in your case you just have to wait some short period of time and send a KEYUP event.
Greetings Covean
Hi, thanks for your reply, but the problem is that i need to do this without setting focus on the application :~ . I'm trying to do this with the 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".
-
Hi, thanks for your reply, but the problem is that i need to do this without setting focus on the application :~ . I'm trying to do this with the 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".
1. I think in this case you can't use the SendKeys function, because they send the input to the window that has the focus. Can't you just switch the focus for this very short period of time to your app and than back to the app that had the focus before? 2. Ever tried to send KEYDOWN at first (try with ALT modifier set/unset) and after you sent left ALT + some key just sent an KEYUP event? Something like this:
PostMessage(hWnd, KEYDOWN, VK_LEFTALT);
PostMessage(hWnd, KEYDOWN, VK_F);
Sleep(100);
PostMessage(hWnd, KEYUP, VK_F);
PostMessage(hWnd, KEYUP, VK_LEFTALT);Greetings Covean
-
1. I think in this case you can't use the SendKeys function, because they send the input to the window that has the focus. Can't you just switch the focus for this very short period of time to your app and than back to the app that had the focus before? 2. Ever tried to send KEYDOWN at first (try with ALT modifier set/unset) and after you sent left ALT + some key just sent an KEYUP event? Something like this:
PostMessage(hWnd, KEYDOWN, VK_LEFTALT);
PostMessage(hWnd, KEYDOWN, VK_F);
Sleep(100);
PostMessage(hWnd, KEYUP, VK_F);
PostMessage(hWnd, KEYUP, VK_LEFTALT);Greetings Covean
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 UpThanks to all for the replies. :)