INPUT_MOUSE: mouse does not move with given dx/dy values

旧巷老猫 提交于 2019-12-08 12:28:47

问题


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

        }

回答1:


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




回答2:


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];




回答3:


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.



来源:https://stackoverflow.com/questions/9946871/input-mouse-mouse-does-not-move-with-given-dx-dy-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!