0

I am moving the mouse using INPUT to set the cursor position. This is fine, except that I cant use screen values to move the cursor. If I want to set the cursor from 0 to 1680, I have to use 1680*0.66 as dx value to get the right position (inside game or on desktop).

(I use mousemove, as I am setting the cursor position inside a game, and absolute positioning doesnt work there. I have two screens, one is 1680 wide, the other one has 1280)

Any Idea why I have to use this factor or where it comes from? Thanks.

#define MOUSE_MOVE_FACTOR 0.6619

        //Set mouse pos:
        void setMousePos(int iX, int iY){


            iX = (int)((double)iX*MOUSE_MOVE_FACTOR);
            iY = (int)((double)iY*MOUSE_MOVE_FACTOR);


            INPUT *buffer = new INPUT[1];

            buffer->type = INPUT_MOUSE;
            buffer->mi.dx = iX;
            buffer->mi.dy = iY;
            buffer->mi.mouseData = 0;
            buffer->mi.dwFlags = MOUSEEVENTF_MOVE;
            buffer->mi.time = 0;
            buffer->mi.dwExtraInfo = 0;

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

            Sleep(100 + (rand() % 50));

        }
Ilyssis
  • 4,849
  • 7
  • 24
  • 30

4 Answers4

1

Probably has something to to with mouse acceleration. Disabling it, leads to diffrerent mouse positions.

Ilyssis
  • 4,849
  • 7
  • 24
  • 30
0

I know this is old, but just for anyone else. The documentation for MOUSEINPUT and mouse_event explain that absolute positioning is normalized to the range 0-65535.

If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface; coordinate (65535,65535) maps onto the lower-right corner. In a multimonitor system, the coordinates map to the primary monitor.

So the conversion would be x / (w-1) * 65535 and y / (h-1) * 65535.

Puddle
  • 2,993
  • 1
  • 19
  • 32
0

Memory leaks can cause strange things to your application, and this may have an influence.

You are causing a memory leak every time this function is called by your second allocation of buffer at

buffer = new INPUT[1];

josephthomas
  • 3,256
  • 15
  • 20
0

As per the documention on MOUSEINPUT, dx/dy are relative coordinates if MOUSEEVENTF_ABSOLUTE is not set in dwFlags. Try:

 buffer->mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;

and see if that works. Note that you'll have to convert the X/Y to normalized absolute coordinates from 0-65535 as the documentation mentions.

I would also question why you are setting the position to (-3000,-3000) first. I would also guess you don't need the magic factor of MOUSE_MOVE_FACTOR which is probably due to mixing up relative/absolute mouse coordinates.

Edit: Re-reading your question and I missed that you may be using relative mouse position on purpose. I would guess your absolute positioning might have failed due to not using normalized absolute coordinate (I wouldn't have guessed that myself without reading the entire documentation). Also note that 1680x0.66=1109 which is suspiciously close to 1024 and I might guess the game is running at a 1024x? resolution.

uesp
  • 6,194
  • 20
  • 15
  • Yes, absoute positioning does not work here, as the game just looks for movements. The game is running in 1680x1050 too. Howerver it does no difference if I am in the game or on desktop. – Ilyssis Mar 30 '12 at 16:58
  • Hmm, strange then. According to the docs the position should be in pixels and I can't think of anything offhand that would introduce a constant factor like that. – uesp Mar 30 '12 at 21:40