4

I've been trying to simulate a series of inputs into a process. The only one I was unsuccessful in achieving is mouse movement. I have found the closest bet online:

bool mouse_move(int x, int y)
{
    INPUT input;
    input.type = INPUT_MOUSE;
    input.mi.mouseData = 0;
    input.mi.time = 0;
    input.mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));//x being coord in pixels
    input.mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));//y being coord in pixels
    input.mi.dwFlags = MOUSEEVENTF_MOVE;//MOUSEEVENTF_ABSOLUTE
    SendInput(1, &input, sizeof(input));
    return true;
}

I did not understand the struct as it was explained online. The mouse keeps going to the bottom-right corner of the screen no matter what value I input (except 0 obviously).

It was possible through SetCursorPos() yes, but once I went into the process, that function just didn't work anymore. I need to simulate it as if the user is inputting the mouse movement and so far SendInput() worked. I just can't seem to figure out the positioning.

Bond
  • 16,071
  • 6
  • 30
  • 53
illogical204
  • 41
  • 1
  • 1
  • 2
  • Should help and probably answer: http://stackoverflow.com/questions/7492529/how-to-simulate-a-mouse-movement – user4581301 Jul 25 '15 at 02:04
  • Hmm, start by getting rid of the GetSystemMetrics() calls and just using x and y directly. And consider that you *really* meant to use MOUSEEVENTF_ABSOLUTE and not incremental moves. the latter will send it very quickly into the lower-right corner if you never pass negative values for x and y. – Hans Passant Jul 25 '15 at 07:36

3 Answers3

3

If you want to place cursor in absolute coordinates, you have to add more flags:

input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_ABSOLUTE;

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx

Anton
  • 41
  • 2
1

Only an hint: the best way to handle this kind of formula:

x*(65536/GetSystemMetrics(SM_CXSCREEN))

is to use MulDiv:

MulDiv(x, 65536, GetSystemMetrics(SM_CXSCREEN))

The code gets clearer and easiest to maintain.

stegemma
  • 21
  • 2
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34173641) – user16217248 Apr 09 '23 at 18:37
0

this is not the proper equation : dx = x*(65536/GetSystemMetrics(SM_CXSCREEN))

The proper equation is Y = mX + b, you have Y = mX.

This ONLY works if your primary monitor is the leftmost, and highest vertically of all of your monitors. Otherwise the Virtual Desktop starts at a negative #, not 0. But you are passing 0 in for b.

Normalized inputs are from 0 - 0xffff, but the non normalized coords often go from (<= 0 - >= PrimarymonitorWidth. So you are most likely 1 or more monitors to the right and or down.

When you add in the b, or the Y-Intercept you should get the proper location.

Dan
  • 2,460
  • 2
  • 18
  • 12