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:

#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

Recommended Answers

All 10 Replies

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

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:

#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(...);

Sure.

POINT mousePos;
GetCursorPos(&mousePos);
std::cout<< "X: " << mousePos.x << std::endl;
std::cout<< "Y: " << mousePos.y << std::endl;

hope that makes sense.

Chris

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 :)

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

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.

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

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.

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.

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;

}

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.

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

I will take your advice, and improve it. But cut me some slack :icon_lol: ...
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) :D .

I will take your advice, and improve it. But cut me some slack :icon_lol: ...
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) :D .

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

>I guess blocking input might be a bad idea since then you have no way of using the ctrl
Well, I still have the right control key on my keyboard.

But the point I was making is, in my keyboard hook, I made it so windows completely ignores that the control key was ever pressed, so no need to send the release key message, but for some reason... it didn't work, which is why i'm going to try Block Input :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.