Problem with setting Mouse Position with SendInput()

Hi,

I have an OpenGL Application and want to use the mouse for a Free-Flight Camera.
The mouse should not be restricted to the Monitor boundaries...

I get the mouse-position with WM_MOUSEMOVE and stuff.

Because of the requirement of no boundaries i have to use a different system of saving my Mouse Position.

I want to save an absolute position of the mouse x and y.
To calculate the x and y I want to get a relative mouse movement each frame.

The idea is to reset the camera to a certain position und get the difference each frame.
After that i want to reset the mouse.

I want to reset the mouse position by SendInput() which uses the INPUT structure defined in winuser.h

it looks like this:

typedef struct tagINPUT
{
    DWORD type;
    union
    {
        MOUSEINPUT mi;
        KEYBDINPUT ki;
        HARDWAREINPUT hi;
    };
} INPUT, *PINPUT, FAR* LPINPUT;

typedef struct tagMOUSEINPUT {
LONG dx;
LONG dy;
DWORD mouseData;
DWORD dwFlags;
DWORD time;
DWORD dwExtraInfo;
} MOUSEINPUT, *PMOUSEINPUT, FAR* LPMOUSEINPUT;


My code looks like this:

#include <windows.h>
#include <time.h>

class Mouse
{
    int x,y,dx,dy,new_x,new_y; //mouse position variables
    INPUT reset; // packet for SendInput command to reset the mouseposition

    Mouse();
    void Update();
    void Render();
};

Mouse::Mouse()
{
    reset.type = INPUT_MOUSE; // we want to send a mouse input
    reset.mi.dx = reset.mi.dy = 1;   // reset the position to 1,1
    reset.mi.mouseData = 0;   //dunno, unimportant
    reset.mi.dwFlags = ( MOUSEEVENTF_MOVE || MOUSEEVENTF_ABSOLUTE ); // move the mouse || coordinates are absolute, not relative
    reset.mi.time = time(NULL); // actual time
    reset.mi.dwExtraInfo = GetMessageExtraInfo(); //sendinput wants that
}

void KMouse::Update()
{
    // calc the new position
    GetNewMousePosition(&new_x, &new_y);
    dx = x - new_x; dy = y - new_y;
    x = new_x; y = new_y;
   
    //reset the mouse position
    reset.mi.time = time(NULL); // get the actual time
    SendInput(1,&reset,sizeof(reset));
 
    //render a crosshair to the screen
    Render();    
}

anyways, the mouse position is not reset, the mouse moves slowly to the lower right corner, as if the MOUSEEVENTF_ABSOLUTE is not set.... any suggestions on that

thanks a lot

TomEn


Answer this question

Problem with setting Mouse Position with SendInput()

  • DaveRogers

    Here's what I did for a similar problem. 

        POINTS pts;
        pts.x = 100;
        pts.y = 100;

        // Get total screen coordinates
        screen_x = GetSystemMetrics(SM_CXSCREEN);
        screen_y = GetSystemMetrics(SM_CYSCREEN);

        // Get location of window
        GetWindowPlacement(hWndTable, &wp);

        // MOUSEINPUT struct uses a truely bizarre coordinate system
        //   the screen is mapped to a scale from 0 to 65535 in both axis.
        //Rectangle screen = Screen.PrimaryScreen.Bounds;
        x = ( 65535 * (pts.x + wp.rcNormalPosition.left) ) / screen_x;
        y = ( 65535 * (pts.y + wp.rcNormalPosition.top) ) / screen_y;

        buffer[0].type = INPUT_MOUSE;
        buffer[0].mi.dx = x;
        buffer[0].mi.dy = y;
        buffer[0].mi.mouseData = 0;
        buffer[0].mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);
        buffer[0].mi.time = 0;
        buffer[0].mi.dwExtraInfo = 0;

    Leaving the time value in the struct zero makes the event happen now.  I hope this helps.
    If not - this wasn't my idea...


  • Duy Le - MSFT

    Try using SetCursorPos() instead.
  • Problem with setting Mouse Position with SendInput()