Agerith
Agerith

Reputation: 13

Management inputs Mouse with DirectX DirectInput

I try to use DirectX Input to manage input mouse. But when I try to get X and Y coordinates of my mouse, values are incorrect (negative or seem to be random).

I show you the code that I used :

bool    System::frame()
{
    bool result;
    if (input->isButtonDown(BUTTON_L)) //if left button is down
    {
        result = ReadMouse();
        if(!result)
            return false;
        ProcessInput();
    }
}

bool System::ReadMouse()
{
    HRESULT result;

    //this->mouseState is a DIMOUSESTATE ; this->mouse is a LDIRECTINPUTDEVICE8
    result = this->mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&this->mouseState);
    if(FAILED(result))
    {
        if((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
            this->mouse->Acquire();
        else
            return false;
    }
    return true;
}

void System::ProcessInput()
{
    this->mouseX = this->mouseState.lX;
    this->mouseY = this->mouseState.lY;

    if(this->mouseX < 0)
        this->mouseX = 0;
    if(this->mouseY < 0)
        this->mouseY = 0;

    if(this->mouseX > this->ScreenWidth)
        this->mouseX = this->ScreenWidth;
    if(this->mouseY > this->ScreenHeight)
        this->mouseY = this->ScreenHeight;
    return;
}

My last test give this->mouseX = -657 and this->mouseY = -36 instead of 200 and 200 (approximately). I check the function when I initialize the mouse, they seem to works (I followed a tutorial).

Upvotes: 1

Views: 3323

Answers (1)

pag3faul7
pag3faul7

Reputation: 428

I think the reason is that DirectInput gives you relative data for the position of the mouse. Please see: http://msdn.microsoft.com/en-us/library/windows/desktop/ee418272(v=vs.85).aspx for an explanation of how to interpret data from mouse and how to switch to absolute mode.

It is recommended to use the Raw Input API instead of DirectInput. (http://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx)

Upvotes: 2

Related Questions