C++ Learning Community Forum
August 01, 2010, 03:03:40 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Simulate Left mouse button click  (Read 1063 times)
unholii
N00b!!1
*
Posts: 10


View Profile
« on: July 11, 2009, 07:50:40 PM »

Hey everyone Smiley

I'm new in here and i want to know how to simulate left mouse click when right mouse button is pressed Smiley

I have tried google to find and there i found

void SimulateMouseClick(int x, int y, MouseButton button);

But it's not working Shocked

Well my code is atm
Code:
#include <iostream>
#include <windows.h>
#include <time.h>

using namespace std;

int main()
{
    char cMan;
    switch (cMan)
    {
           case 'VM_RBUTTONDOWN':
                
                    do
                    {
                        void SimulateMouseClick(int x, int y, MouseButton button);
                        sleep(250);
                    }while(WM_RBUTTONDOWN == true)
                
    }
    
    return EXIT_SUCCESS;
}              

And my goal is to make a program what will autoclick every 250 millisecond until i release right mouse button.
« Last Edit: July 11, 2009, 07:52:25 PM by unholii » Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #1 on: July 11, 2009, 11:38:17 PM »

I'd use the keybd_event function http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx . Where did you get the SimulateMouseClick function from? Your syntax is also wrong where you're trying to use it.
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
unholii
N00b!!1
*
Posts: 10


View Profile
« Reply #2 on: July 12, 2009, 10:06:38 AM »

I'd use the keybd_event function http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx . Where did you get the SimulateMouseClick function from? Your syntax is also wrong where you're trying to use it.

Thanks for the site, and i found the SimulateMouseClick from google. I don't remember what site.
Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #3 on: July 12, 2009, 06:30:43 PM »

I simulate mouse clicks by using windows messages. WM_CLICK, BM_CLICK
i think right mouse is WM_RCLICK
but i'll have to look that up.
Logged


Imagine the impossible, then make it happen.
unholii
N00b!!1
*
Posts: 10


View Profile
« Reply #4 on: July 14, 2009, 05:45:50 AM »

Could someone paste me whole code Shocked Because i'm not sure anymore how it could be done Shocked
Logged
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #5 on: July 14, 2009, 04:07:46 PM »

well read up on it and stuff. we are here to help not give answers
Logged
unholii
N00b!!1
*
Posts: 10


View Profile
« Reply #6 on: July 15, 2009, 04:52:15 AM »

well read up on it and stuff. we are here to help not give answers

Heh ok Cheesy I've been very busy lately so i didn't have enough time to try to fix it and now i am sick. I will start trying now those links and things. But i hope i will succeed some day  Roll Eyes.
Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #7 on: July 18, 2009, 02:16:43 AM »

the messages approach is quite simple you just use SendMessage or PostMessage to the window you want to receive the click. (note some windows use mouse messages rather than mouse clicks. if you need the mouse to be at a specific spot when the click is received you can use SetCursorPos or SetPhysicalCursorPos to move the cursor to that location.

Sorry, i lost the example program i used to test this method in a HD crash a while back. but trust me i have used it and it does work, you should be able to look these functions up on MSDN. As for the messages you can just look them up in just about any example program that accepts mouse clicks of the type you are trying to emulate.
Logged


Imagine the impossible, then make it happen.
unholii
N00b!!1
*
Posts: 10


View Profile
« Reply #8 on: August 09, 2009, 06:37:14 PM »

HAHAA. Thanks anyways BUT I GOT IT WORKING !!! But its still not working as i wanted ...

Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
   
    using namespace std;
   
    int main()
    {
        system("TITLE ChickenFriend autoclicker");
        cout << "Welcome!" << endl;
        cout << endl << "Press F9 to start autoclicking and press F9 again to stop it" << endl;
        cout << endl << "Have fun :) " << endl;
    for(;;){
    if(GetKeyState(VK_F9) == 1){
                                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                                 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                                 Sleep(250);
    }
    }
    system("PAUSE");
    return 0;
    }

And i don't know how to make it autoclick only when F9 is down. and stop when its up. ^^
If you can help with that, i would be glad Smiley
« Last Edit: August 09, 2009, 07:47:00 PM by unholii » Logged
*brammie*
my rank:
C++ Freak
***
Posts: 309



View Profile WWW
« Reply #9 on: August 09, 2009, 08:37:51 PM »

if(GetKeyState(VK_F9) == 1)

replace that with:

if(GetKeyState(VK_F9))
Logged
unholii
N00b!!1
*
Posts: 10


View Profile
« Reply #10 on: August 21, 2009, 06:01:37 PM »

if(GetKeyState(VK_F9) == 1)

replace that with:

if(GetKeyState(VK_F9))

Didnt work :s It stilll autoclicks until i press F9 again :<
Does anybody have any idea how to make it to autoclick when F9 is down and its stops when its up Shocked If you understand Wink

EDIT____________
Ok i got some help from mIRC. Its perfect now Wink
Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
   
    using namespace std;
   
    int main()
    {
        int a = 0;
        system("TITLE ChickenFriend autoclicker");
    for(;;){
    if(GetAsyncKeyState(VK_F9)){
                                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 5, 5);
                                 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 5, 5);
                                 a++;
                                 Sleep(250);
    }else{
                      cout << "Welcome!" << endl;
        cout << endl << "Press F9 to start autoclicking and press F9 again to stop it" << endl;
        cout << endl << "Have fun :) " << endl;
        cout << endl << endl;
              cout << "Autoclicked " << a << " times total." << endl;
              Sleep(1000);
              system("CLS");
              }
              Sleep(25);

    }
    system("PAUSE");
    return 0;
    }
« Last Edit: August 21, 2009, 07:08:29 PM by unholii » Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #11 on: August 22, 2009, 03:44:58 AM »

There are many ways to do this one is to have your autoclicks inside a separate function that you call whenever a variable is set in your main loop. then you set/clear this variable based on the state of VK_F9.
Logged


Imagine the impossible, then make it happen.
unholii
N00b!!1
*
Posts: 10


View Profile
« Reply #12 on: August 23, 2009, 01:21:37 PM »

There are many ways to do this one is to have your autoclicks inside a separate function that you call whenever a variable is set in your main loop. then you set/clear this variable based on the state of VK_F9.

I didn't understand anything but there is one advice for anyone who's trying to make autoclicker, Remember to always put a delay for VK_F9 checking because otherwice it will raise your cpu because its trying to check the VK_F9 as fast as possible, so around 5-10 milliseconds Wink
Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #13 on: August 23, 2009, 06:28:59 PM »

Please more my post above. I have successful created auto-clickers using the messages approach. And most auto-clickers I have seen have used hotkeys to manage the auto-clicking on and off.
Logged


Imagine the impossible, then make it happen.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!