Mouse Click

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 42
Reputation: tondeuse34 is an unknown quantity at this point 
Solved Threads: 2
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

Mouse Click

 
0
  #1
May 9th, 2008
Hey guys, i have done C++ before but gave up on it . But i've came back to it and i'm wondering what is a good way to simulate a left and right click? i guess you would start with this:
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. int n = 1;
  9. int x;
  10. int y;
  11.  
  12. while (n !=NULL)
  13.  
  14. {
  15.  
  16. if(GetAsyncKeyState(VK_NUMPAD0))
  17. (
  18.  
  19. mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 100, 100));
  20. mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 200, 200));
  21.  
  22. }
  23.  
  24. if(GetAsyncKeyState(VK_NUMBPAD1))
  25. break;
  26.  
  27. }
  28.  
  29. return 0;
  30.  
  31.  
  32. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 87
Reputation: n1337 is on a distinguished road 
Solved Threads: 8
n1337 n1337 is offline Offline
Junior Poster in Training

Re: Mouse Click

 
0
  #2
May 10th, 2008
Ummm well, left and right click gets into events and event handling...that is, Win32 programming.

I suggest you start by doing some google searches on this topic As an added bonus, if you learn Win32, you can leave the boring old console and start making pretty GUIs...haha
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: tondeuse34 is an unknown quantity at this point 
Solved Threads: 2
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

Re: Mouse Click

 
0
  #3
May 10th, 2008
talk about some confusing stuff, python has spoiled me lol
Last edited by tondeuse34; May 10th, 2008 at 12:36 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Mouse Click

 
0
  #4
May 10th, 2008
Yes, writing GUI in c++ is pretty difficult stuff. Other languages such as python, vb, and C# are better suited for that.

I don't think you can process mouse events like that in console programs. The program needs a message pump in order to get and process windows event messages, and console programs don't have that capability.

Read this about windows console functions
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Mouse Click

 
0
  #5
May 10th, 2008
It looks to me like the code you wrote never releases the click. This is how you fully emulate a mouse click:

  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click
  2. mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); // Right click
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: elementz is an unknown quantity at this point 
Solved Threads: 0
elementz elementz is offline Offline
Newbie Poster

Re: Mouse Click

 
0
  #6
May 10th, 2008
Originally Posted by williamhemswort View Post
It looks to me like the code you wrote never releases the click. This is how you fully emulate a mouse click:

  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click
  2. mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); // Right click
does this work?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Mouse Click

 
0
  #7
May 10th, 2008
Originally Posted by elementz View Post
does this work?
Yes it does
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Mouse Click

 
0
  #8
May 10th, 2008
Originally Posted by williamhemswort View Post
Yes it does
Not in console programs.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Mouse Click

 
2
  #9
May 10th, 2008
It works with me

As long as you include the window header I think you can still use any of the functions there.
heres an example of what you can do with a console application.
  1. #include <windows.h>
  2. #include <cmath>
  3.  
  4. int main() {
  5. system("start c:\\windows\\system32\\mspaint.exe");
  6. HWND paint;
  7.  
  8. do {
  9. paint = FindWindow("MSPaintApp", "Untitled - Paint");
  10. Sleep(10);
  11. } while (paint == NULL);
  12.  
  13. Sleep(200);
  14. RECT r;
  15. GetWindowRect(paint, &r);
  16. RECT cr;
  17. GetClientRect(paint, &cr);
  18.  
  19. int x = r.left + (cr.right / 2);
  20. int y = r.top + (cr.bottom / 2);
  21.  
  22. SetCursorPos(x,y);
  23.  
  24. POINT p;
  25. GetCursorPos(&p);
  26. paint = WindowFromPoint(p);
  27.  
  28. GetWindowRect(paint, &r);
  29. GetClientRect(paint, &cr);
  30.  
  31. x = r.left + (cr.right / 2);
  32. y = r.top + (cr.bottom / 2);
  33.  
  34. SetCursorPos(x,y);
  35.  
  36. int radius = 0;
  37. double angle = 0;
  38.  
  39. int max = min(cr.right/2,cr.bottom/2);
  40.  
  41. mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  42.  
  43. for (; radius < max; radius++, angle += 0.1) {
  44. SetCursorPos(x+(cos(angle)*radius), y+(sin(angle)*radius));
  45. Sleep(20);
  46. }
  47.  
  48. mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
  49.  
  50. return 0;
  51. }
Last edited by William Hemsworth; May 10th, 2008 at 10:36 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Mouse Click

 
0
  #10
May 10th, 2008
You are right -- it does work, and even in console programs Change main() like this too:
  1. int main() {
  2. HWND paint = GetConsoleWindow();
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC