Asked by:
SendMessage for keystroke is not working

Question
-
Hi all
could you please tell me why this code does not work? It should simulate the pression of TAB and then arrow DOWN, but nothing happens. I´m sure I have the right handle to the window, because I can click with the mouse on some of its buttons.
SetForegroundWindow(hwndWindow);
SetActiveWindow(hwndWindow);
SetFocus(hwndWindow);
SendMessage(hwndWindow, WM_SETFOCUS, VK_TAB, 0); //tried also without
SendMessage(hwndWindow, WM_SETFOCUS, VK_TAB, 0); //these 2 messages
Sleep(500);
SendMessage(hwndWindow, WM_KEYDOWN, VK_TAB, 0);
SendMessage(hwndWindow, WM_KEYUP, VK_TAB, 0);
Sleep(1000);
SendMessage(hwndWindow, WM_KEYDOWN, VK_DOWN, 0);
SendMessage(hwndWindow, WM_KEYUP, VK_DOWN, 0);
Many thanks in advance
Fede
Thursday, May 8, 2008 1:38 PM
All replies
-
No idea. SendInput() is another mouse trap.Friday, May 9, 2008 1:01 AM
-
Thanks for reply, nobugz.
I tried with SendInput. It works (not on Vista). But since my application runs as a service, SendInput requires that the service uses Local System Account. This causes conflict with other functionalities, so that´s why I need to use SendMessage or PostMessage.
Can someone else help, please?
Thanks in advance
Friday, May 9, 2008 2:37 PM -
Hello,
Please excuse me for being a little off topic:For two days I have been suffering, I cannot understand:
I'm trying to emulate a button click on the Window 10 calculator. Since there are no real buttons of the "Button class" in the calculator, then:
I look through spy ++ which messages are sent to the calculator window when I click, for example, on the number 8:000201C6 S WM_PARENTNOTIFY fwEvent:WM_LBUTTONDOWN xPos:191 yPos:493
I am sending the same message to the Calculator window:
HWND calculator_HWND = FindWindowW(NULL, L"Calculator"); //Get HWND calculator WPARAM my_wparam; wchar_t* wchar_wparam = (wchar_t*)&my_wparam; wchar_wparam[0] = WM_LBUTTONDOWN; // LOWORD-left click wchar_wparam[1] = 0; //HIWORD(wParam) is undefined. LPARAM my_lparam; //Coordinate click wchar_t* wchar_laparam = (wchar_t*)&my_lparam; wchar_laparam[0] = 191; //X wchar_laparam[1] = 493; //Y SendMessage(calculator_HWND, WM_PARENTNOTIFY, my_wparam, my_lparam);
After completing the SendMessage, I see a message in the calculator window in SPY ++:
000201C6 S WM_PARENTNOTIFY fwEvent:WM_LBUTTONDOWN xPos:191 yPos:493
But the number 8 is not pressed. The calculator does not respond to this message in any way. :(((
Please tell me what am I doing wrong?
Sunday, August 30, 2020 7:19 PM -
Maybe you've forgotten to send WM_LBUTTONUPSunday, August 30, 2020 11:13 PM