管理使用DirectX DirectInput输入鼠标

时间:2013-09-16 14:06:21

标签: c++ c++11 mouse

我尝试使用DirectX Input来管理输入鼠标。但是,当我尝试获取鼠标的X和Y坐标时,值不正确(为负或似乎是随机的)。

我向您展示了我使用的代码:

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

我的上一次测试给出了this->mouseX = -657this->mouseY = -36,而不是200200(大约)。我在初始化鼠标时检查功能,它们似乎有效(我按照教程)。

1 个答案:

答案 0 :(得分:1)

我认为原因是DirectInput为您提供了鼠标位置的相对数据。 有关如何解释鼠标数据以及如何切换到绝对模式的说明,请参阅:http://msdn.microsoft.com/en-us/library/windows/desktop/ee418272(v=vs.85).aspx

建议使用Raw Input API而不是DirectInput。 (http://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx