0

In the below codes, it is expected my mouse cursor would be set to (100,100) on the screen. However, it turns out the cursor is moved to (2,1).

And when MOUSEEVENTF_ABSOLUTE is not set, I expected the mouse would move 100 pixels to the right and bottom respectively. While the result is it moved from (352, 65) to (670, 383).

INPUT Input[1];
ZeroMemory(Input, sizeof(INPUT) * 1);
Input[0].type = INPUT_MOUSE;
Input[0].mi.dx = 100;
Input[0].mi.dy = 100;
Input[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;

SendInput(1, Input, sizeof(INPUT));

Posts I have read:

I have realized that there is another post about the similar situation. But I suspect whether memory leak is the answer since I used cout to check the value of Input[0].mi.dx and Input[0].mi.dy and they are exact 100.

From the solution of the top voted post in here, factors are used to modify the xy values. Even so, the result is not accurate to your input. And the answer did not explain why a factor need to be used.

My question:

1) What is the accurate way to move cursor in Windows beside using SetCursorPos()? (there is a reason I don't want to use it)

2) Does anyone knows the reasons behind the unreasonable movement with MOUSEEVENTF_MOVE?

Community
  • 1
  • 1
Samuel
  • 113
  • 2
  • 8

3 Answers3

1

Okay in QT how to find the Mouse Position: I am Current making a program that uses the QMouseMoveEvent Quite heavily.

So step by step, lets create a new project, with a blank MainMenu form. Slap a new Label down so we can see the X Y coords change.

This works for QT Create a new function and call it

void MainWindow::mouseMoveEvent(QMouseEvent *event), make sure to add it in the .h too!

Then using the event varible create a pointer to it. QPoint pos = event->pos();

Then do pos.x(), pos.y(); and put them into your label/output and there we go!

Then use them coords to do as you wish with.

I hope this is the answer your looking for!

If you need more help i can link you to my GitHub repo and you can view some of my work on X Y positions and Lat Lon to X Y;

Also just as a last thing if you are looking to do Lat Lon to X Y its this

X is current mouse X coord; Y is current mouse Y Coord;

double lat = (y * 180) / ("Height in pixels of scene")-90

double lon = (x * 360) / ("Width in Pixels of scene")-180

Also one last thing to set the positon in QT simply use

QCursor my_cursor; my_cursor.setPos(X,Y);

Not sure about getting the Dx/Dy but this is a small example that may help you understand more about this. Anyway I hope what I put has helped

Jake Wade
  • 571
  • 3
  • 19
  • Also have a read through this - explains how QMouseMoveEvent works and what its derived of etc http://qt-project.org/doc/qt-4.8/qmouseevent.html – Jake Wade Feb 12 '15 at 10:08
0

Are you taking into account that X/Y Coords start at 0,0 at the top left hand corner?

What Complier are you using? Its hard to fix a problem when we don't know what libraries we need to use to fix this.

Because if this is in QT its an easy fix. But im not too sure about using it in MFC or another C++ GUI.

More detail would be nice :) - get back to me ill have another look into your issue

Jake Wade
  • 571
  • 3
  • 19
  • Yes, top left corner of primary monitor is considered (0,0). I have tried it in both Qt and visual C++, the problem occurs in both of them. It would also be great if you can provide a Qt method for solving it! – Samuel Feb 12 '15 at 08:48
0

A couple things come to mind:

int CURSOR_X;
int CURSOR_Y;

int mousex;
int mousey;

CURSOR_X = 100;
CURSOR_Y = 100; 

SetCursorPos(CURSOR_X, CURSOR_Y);

mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_MOVE, 100, mousex, mousey, 100);
Haglund
  • 1
  • 2