How can I send a message that makes the computer think the user pressed the left mouse button?
Also, how can I send a message that makes the computer think the user pressed the 'A' key on the keyboard?
Thanks, August.
How can I send a message that makes the computer think the user pressed the left mouse button?
Also, how can I send a message that makes the computer think the user pressed the 'A' key on the keyboard?
Thanks, August.
For some reason I don't think this is what you meant, but posted it in case it was.Code:SendMessage(hwnd,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(x,y));
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
To send click:
SendMessage(handle_of_window, WM_LBUTTONDOWN, 0, 0);
Sleep(50);
SendMessage(handle_of_window, WM_LBUTTONUP, 0, 0);
send a character, I think it's:
SendMessage(handle_of_window, WM_CHAR, (WPARAM)'A', 0);
I may be wrong on the last one, but anyway check msdn for SendMessage and WM_CHAR or WM_KEYDOWN/UP
I see. But this is not working. Why?
Code:HWND hwndOnTop = GetFocus(); SendMessage(hwndOnTop, WM_LBUTTONDOWN, 0, 0);
you can't just send a button down message. A click is a button down and button up. You also usually need to sleep for a few milliseconds between them for it to register correctly
I tried this:
Sleep(3000);
Beep(2000,100);
HWND hwndOnTop = GetFocus();
SendMessage(hwndOnTop, WM_LBUTTONDOWN, 0, 0);
Sleep(100);
SendMessage(hwndOnTop, WM_LBUTTONUP, 0, 0);
It still does not work.
This should work as long as you get your desired window open in 3 seconds:
The problem was GetFocus() wasn't returning a handle. I tried GetFocus and it returned NULL so I used GetForegroundWindow(). Hope that helps. Oh not ALL programs need a time lapse to respond to those two messages, but I don't doubt that some do. So if no luck with the above code, try a little time lapse between messages.Code:#include <windows.h> int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) { HWND hwnd; Sleep(3000); hwnd=GetForegroundWindow(); while(hwnd==NULL) { hwnd=GetForegroundWindow(); } SendMessage(hwnd,WM_LBUTTONDOWN,(WPARAM)MK_LBUTTON,0); SendMessage(hwnd,WM_LBUTTONUP,0,0); return 0; }
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
also, just sending an lbutton down/up to a window isn't going to do anything. The GetForegroundWindow function may return the top most window, but it won't return a handle to a button on the window. If you want a specific button, use FindWindow() then GetWindow with GW_CHILD and GW_HWNDNEXT until you find that button. Also Spy++ is your friend here.
Still, even if I can capture a child window's handle, how am I suppose to know if that is the one that is focused?
Well if it's not the focused window then make it the focused window, with SetFocus(HWND);.
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0