Mouse Move

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 6
Reputation: chrischavez is an unknown quantity at this point 
Solved Threads: 0
chrischavez chrischavez is offline Offline
Newbie Poster

Mouse Move

 
0
  #1
Dec 31st, 2008
I have an application which is supposed to move the mouse based on input like that from a keyboard. the code listens for the direction keys to see if they are pressed. the code works but only in 2 direction it can only move to the right and down. is there any way to move it up and left to.
Here is the code:
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. void MoveMouse(int x, int y);
  5. using namespace std;
  6.  
  7. int main(){
  8. bool boo = true;
  9. int x = 0;
  10. int y = 0;
  11. while(boo){
  12. if(GetKeyState(VK_LEFT)<0){
  13. x--;
  14. if(x < 0){
  15. x = 0;
  16. }
  17. MoveMouse(x,NULL);
  18. }
  19. if(GetKeyState(VK_RIGHT)<0){
  20. x++;
  21. if(x < 0){
  22. x = 0;
  23. }
  24. MoveMouse(x,NULL);
  25. }
  26. if(GetKeyState(VK_UP)<0){
  27. y--;
  28. if(y < 0){
  29. y = 0;
  30. }
  31. MoveMouse(NULL,y);
  32. }
  33. if(GetKeyState(VK_DOWN)<0){
  34. y++;
  35. if(y < 0){
  36. y = 0;
  37. }
  38. MoveMouse(NULL,y);
  39. }
  40. if(GetKeyState(VK_SPACE)<0){
  41. boo = false;
  42. }
  43. Sleep(30);
  44. system("cls");
  45. cout << x << endl;
  46. cout << y << endl;
  47.  
  48. }
  49.  
  50. system("cls");
  51. }
  52.  
  53. void MoveMouse(int x, int y){
  54. int buffer;
  55. for(buffer = 0; x >= buffer && y >= buffer; buffer++){
  56. mouse_event(0x0001,x,y,NULL,NULL);
  57. Sleep(1);
  58. }
  59. }

also if anyone can help is there any way to control the mouse and clicks with a serial input. if you need more info just ask and is there a way for me to be able to click outside of the console window but still control the mouse with the program
Last edited by chrischavez; Dec 31st, 2008 at 1:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Mouse Move

 
0
  #2
Dec 31st, 2008
You can use SetCursorPos() to control the position of the cursor. It returns true is it succeeds and false if not. It takes 2 arguments int x and int y. You can use GetCursorPos() to retrieve the position of cursor. it returns the same as Set...() it takes one paramater a pointer to a structure of type POINT, which contains to values long x and long y. Also if you want to send Mouse clicks then you will need to look into the function SendInput() its well documented on msdn but feel free to ask for more information.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Mouse Move

 
0
  #3
Dec 31st, 2008
Originally Posted by Freaky_Chris View Post
You can use SetCursorPos() to control the position of the cursor. It returns true is it succeeds and false if not. It takes 2 arguments int x and int y. You can use GetCursorPos() to retrieve the position of cursor. it returns the same as Set...() it takes one paramater a pointer to a structure of type POINT, which contains to values long x and long y. Also if you want to send Mouse clicks then you will need to look into the function SendInput() its well documented on msdn but feel free to ask for more information.

Chris
As chris said, i used that, and enhanced your program

hope it works:

  1. #include <windows.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. void MoveMouse(int x,int y);
  5. using namespace std;
  6.  
  7. int main(){
  8. bool boo = true;
  9. bool r = false;
  10. int x = 0;
  11. int y = 0;
  12. while(boo){
  13. if(GetKeyState(VK_LEFT)<0){
  14. x--;
  15. if(x < 0){
  16. x = 0;
  17. }
  18. r = true;
  19. MoveMouse(x,y);
  20. }
  21. if(GetKeyState(VK_RIGHT)<0){
  22. x++;
  23. if(x < 0){
  24. x = 0;
  25. }
  26. r = true;
  27. MoveMouse(x,y);
  28. }
  29. if(GetKeyState(VK_UP)<0){
  30. y--;
  31. if(y < 0){
  32. y = 0;
  33. }
  34. r = true;
  35. MoveMouse(x,y);
  36. }
  37. if(GetKeyState(VK_DOWN)<0){
  38. y++;
  39. if(y < 0){
  40. y = 0;
  41. }
  42. r = true;
  43. MoveMouse(x,y);
  44. }
  45. if(GetKeyState(VK_SPACE)<0){
  46. boo = false;
  47. }
  48. if(r)
  49. {
  50. system("cls");
  51. cout << x << endl;
  52. cout << y << endl;
  53. r = false;
  54. }
  55. }
  56. system("cls");
  57. }
  58.  
  59. void MoveMouse(int x, int y){
  60. int buffer;
  61. for(buffer = 0; x >= buffer && y >= buffer; buffer++){
  62. SetCursorPos(x,y);
  63. //Sleep(1);
  64. }
  65. }


Also Chris, can you give me an example of how to use
GetCursorPos(...);
?

lets say i wanted to set it to x and y and i want to get the current mouse pos..

int x = GetCursorPos(...);
int y = GetCursorPos(...);
Last edited by u8sand; Dec 31st, 2008 at 2:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Mouse Move

 
0
  #4
Dec 31st, 2008
Sure.
  1. POINT mousePos;
  2. GetCursorPos(&mousePos);
  3. std::cout<< "X: " << mousePos.x << std::endl;
  4. std::cout<< "Y: " << mousePos.y << std::endl;
hope that makes sense.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,427
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: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Mouse Move

 
0
  #5
Dec 31st, 2008
I made the same program once, but much more complex. In the end I used a similar technique to what Freaky_Chris mentioned, though I went the extra mile and added velocity & friction to make it look a little smoother.
On top of that, it allowed the user the left click using only the keyboard. I originally made this for a friend who had a problem and wasn't able to use his mouse, so I realize it might be slightly buggy in some areas, but overall it works pretty well.

I've attached the project & executable, it might help you improve yours. Move the cursor with the arrow keys, and use the left ctrl button to emulate a left click. You can change the options by right clicking on the notification icon (picture of a cursor) in your task bar.

Hope this helps
Last edited by William Hemsworth; Dec 31st, 2008 at 5:29 pm.
Attached Files
File Type: zip Cursor Mover.zip (46.7 KB, 16 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: chrischavez is an unknown quantity at this point 
Solved Threads: 0
chrischavez chrischavez is offline Offline
Newbie Poster

Re: Mouse Move

 
0
  #6
Jan 1st, 2009
Thanks williamhemsworth but sorry for bothering you but could you explain the code you posted. i know some c++ code but not as much as you
Last edited by chrischavez; Jan 1st, 2009 at 5:47 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,427
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: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Mouse Move

 
1
  #7
Jan 1st, 2009
To begin with, I created a windows hook for the keyboard. This enables you to catch messages related to the keyboard and handle them before windows does.
  1. bool ld = 0, // Left button down
  2. rd = 0, // Right button down
  3. ud = 0, // Up button down
  4. dd = 0, // Down button down
  5. lcd = 0; // Left Control key down
These are flags to show if any of the arrow keys or the left control key is being pressed, they are kept up to date in the keyboard hook.

I also set up a timer in the main windows message loop with an interval of 5ms. So every 5ms it checks if any of the arrow keys are down, if any of them are, then it will change the velocity the cursor is moving in the appropriate direction.
  1. case WM_TIMER:
  2. {
  3.  
  4. // Check if keys are down
  5. if (ld) vx -= cursorSpeed; // Left key
  6. if (rd) vx += cursorSpeed; // Right key
  7. if (ud) vy -= cursorSpeed; // Up key
  8. if (dd) vy += cursorSpeed; // Down key
  9.  
  10. GetCursorPos(&mousePos);
  11.  
  12. // Add velocity to current coordinates
  13. x += vx;
  14. y += vy;
  15.  
  16. // Slows the velocity of the cursor down
  17. vx *= cursorFriction;
  18. vy *= cursorFriction;
  19.  
  20. // Calculate the average speed
  21. avSpeed = sqrt( vx * vx + vy * vy );
  22.  
  23. if (avSpeed > 0.2) {
  24. // If cursor isn't almost stopped
  25. SetCursorPos(x, y);
  26.  
  27. } else {
  28. // Allow user to move cursor with the mouse instead
  29. x = mousePos.x;
  30. y = mousePos.y;
  31. }
  32.  
  33. }
  34. break;

As for allowing the user to left click using only the left ctrl key, I put this code in the keyboard hook process.
  1. if (p->vkCode == VK_LCONTROL) {
  2.  
  3. if (LOWORD(wParam) == WM_KEYDOWN && !lcd) {
  4. // Ctrl key pressed = left mouse button down
  5. mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  6. lcd = true; // Set flag
  7.  
  8. } else if (LOWORD(wParam) == WM_KEYUP && lcd) {
  9. // Ctrl key released = left mouse button up
  10. mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  11. lcd = false; // Set flag
  12. }
  13.  
  14. // We are handling the ctrl key, so don't let windows process it too
  15. eat = true;
  16.  
  17. }
I commented the code to help, but overall, it uses the same basic idea to move the cursor as what Freaky_Chris said.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Mouse Move

 
0
  #8
Jan 1st, 2009
Not too bad William, the only one thing that annoys me about that is the fact that when pressing the ctrl key to simulate a left mouse click it is a big grrr. For example, you should either A) Block input on the ctrl key being sent and just send mouse input or B) Send a left ctrl key up event so that it is not stuck on!

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,427
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: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Mouse Move

 
0
  #9
Jan 1st, 2009
I will take your advice, and improve it. But cut me some slack ...
I made this when I was 14 I think, didn't seem too bad at the time. XD

As for the control key always being left on, I could never quite figure out why that was happening, the keyboard hook stopped windows from processing it. I will try Block Input though, hopefully this is still helping chrischavez solve his problem (as well as mine) .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Mouse Move

 
1
  #10
Jan 1st, 2009
Originally Posted by williamhemsworth View Post
I will take your advice, and improve it. But cut me some slack ...
I made this when I was 14 I think, didn't seem too bad at the time. XD

As for the control key always being left on, I could never quite figure out why that was happening, the keyboard hook stopped windows from processing it. I will try Block Input though, hopefully this is still helping chrischavez solve his problem (as well as mine) .
You need to pass the ctrl key press with the KEYEVENTF_KEYUP flag. That way it wont *stick* so to speak.

I guess blocking input might be a bad idea since then you have no way of using the ctrl lol....there might be a way to sort it out. have a play with it i guess.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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