Two simple questions.

This is a discussion on Two simple questions. within the Windows Programming forums, part of the Platform Specific Boards category; How can I send a message that makes the computer think the user pressed the left mouse button? Also, how ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Two simple questions.

    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.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Code:
    SendMessage(hwnd,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(x,y));
    For some reason I don't think this is what you meant, but posted it in case it was.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    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

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I see. But this is not working. Why?

    Code:
    HWND hwndOnTop = GetFocus();
    SendMessage(hwndOnTop, WM_LBUTTONDOWN, 0, 0);

  5. #5
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    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

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Use the SendInput or mouse_event functions.

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    This should work as long as you get your desired window open in 3 seconds:
    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;
    }
    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.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    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.

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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?

  11. #11
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  3. Few simple questions...
    By Shane in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2005, 02:40 AM
  4. A few simple batch questions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-02-2003, 01:35 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21