943,547 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2346
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 31st, 2008
0

Mouse Move

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrischavez is offline Offline
6 posts
since Dec 2008
Dec 31st, 2008
0

Re: Mouse Move

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 31st, 2008
0

Re: Mouse Move

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Dec 31st, 2008
0

Re: Mouse Move

Sure.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 31st, 2008
0

Re: Mouse Move

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
Attached Files
File Type: zip Cursor Mover.zip (46.7 KB, 76 views)
Last edited by William Hemsworth; Dec 31st, 2008 at 5:29 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jan 1st, 2009
0

Re: Mouse Move

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrischavez is offline Offline
6 posts
since Dec 2008
Jan 1st, 2009
1

Re: Mouse Move

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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jan 1st, 2009
0

Re: Mouse Move

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 1st, 2009
0

Re: Mouse Move

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) .
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jan 1st, 2009
1

Re: Mouse Move

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Qt [undo]
Next Thread in C++ Forum Timeline: C++ in ubuntu





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC