| | |
Mouse Move
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
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
Here is the code:
c++ Syntax (Toggle Plain Text)
#include <windows.h> #include <iostream> #include <cstdlib> void MoveMouse(int x, int y); using namespace std; int main(){ bool boo = true; int x = 0; int y = 0; while(boo){ if(GetKeyState(VK_LEFT)<0){ x--; if(x < 0){ x = 0; } MoveMouse(x,NULL); } if(GetKeyState(VK_RIGHT)<0){ x++; if(x < 0){ x = 0; } MoveMouse(x,NULL); } if(GetKeyState(VK_UP)<0){ y--; if(y < 0){ y = 0; } MoveMouse(NULL,y); } if(GetKeyState(VK_DOWN)<0){ y++; if(y < 0){ y = 0; } MoveMouse(NULL,y); } if(GetKeyState(VK_SPACE)<0){ boo = false; } Sleep(30); system("cls"); cout << x << endl; cout << y << endl; } system("cls"); } void MoveMouse(int x, int y){ int buffer; for(buffer = 0; x >= buffer && y >= buffer; buffer++){ mouse_event(0x0001,x,y,NULL,NULL); Sleep(1); } }
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.
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
Chris
Knowledge is power -- But experience is everything
•
•
•
•
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
hope it works:
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <iostream> #include <cstdlib> void MoveMouse(int x,int y); using namespace std; int main(){ bool boo = true; bool r = false; int x = 0; int y = 0; while(boo){ if(GetKeyState(VK_LEFT)<0){ x--; if(x < 0){ x = 0; } r = true; MoveMouse(x,y); } if(GetKeyState(VK_RIGHT)<0){ x++; if(x < 0){ x = 0; } r = true; MoveMouse(x,y); } if(GetKeyState(VK_UP)<0){ y--; if(y < 0){ y = 0; } r = true; MoveMouse(x,y); } if(GetKeyState(VK_DOWN)<0){ y++; if(y < 0){ y = 0; } r = true; MoveMouse(x,y); } if(GetKeyState(VK_SPACE)<0){ boo = false; } if(r) { system("cls"); cout << x << endl; cout << y << endl; r = false; } } system("cls"); } void MoveMouse(int x, int y){ int buffer; for(buffer = 0; x >= buffer && y >= buffer; buffer++){ SetCursorPos(x,y); //Sleep(1); } }
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.
Sure. hope that makes sense.
Chris
C++ Syntax (Toggle Plain Text)
POINT mousePos; GetCursorPos(&mousePos); std::cout<< "X: " << mousePos.x << std::endl; std::cout<< "Y: " << mousePos.y << std::endl;
Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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
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.
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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.
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.
As for allowing the user to left click using only the left ctrl key, I put this code in the keyboard hook process. 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.
C++ Syntax (Toggle Plain Text)
bool ld = 0, // Left button down rd = 0, // Right button down ud = 0, // Up button down dd = 0, // Down button down lcd = 0; // Left Control key down
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)
case WM_TIMER: { // Check if keys are down if (ld) vx -= cursorSpeed; // Left key if (rd) vx += cursorSpeed; // Right key if (ud) vy -= cursorSpeed; // Up key if (dd) vy += cursorSpeed; // Down key GetCursorPos(&mousePos); // Add velocity to current coordinates x += vx; y += vy; // Slows the velocity of the cursor down vx *= cursorFriction; vy *= cursorFriction; // Calculate the average speed avSpeed = sqrt( vx * vx + vy * vy ); if (avSpeed > 0.2) { // If cursor isn't almost stopped SetCursorPos(x, y); } else { // Allow user to move cursor with the mouse instead x = mousePos.x; y = mousePos.y; } } 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)
if (p->vkCode == VK_LCONTROL) { if (LOWORD(wParam) == WM_KEYDOWN && !lcd) { // Ctrl key pressed = left mouse button down mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); lcd = true; // Set flag } else if (LOWORD(wParam) == WM_KEYUP && lcd) { // Ctrl key released = left mouse button up mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); lcd = false; // Set flag } // We are handling the ctrl key, so don't let windows process it too eat = true; }
Hope this helps.
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
Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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)
.
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)
. •
•
•
•
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).
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
![]() |
Similar Threads
- Simulate Mouse Move (C++)
- Mouse Issue (Windows NT / 2000 / XP)
- mouse over (Visual Basic 4 / 5 / 6)
- Blinking Mouse Pointer (Motherboards, CPUs and RAM)
- how to simulate mouse movement and click (C)
Other Threads in the C++ Forum
- Previous Thread: Qt [undo]
- Next Thread: C++ in ubuntu
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






