1

我尝试使用 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 = -657andthis->mouseY = -36而不是200and 200(大约)。我在初始化鼠标时检查了该功能,它们似乎有效(我按照教程进行了操作)。

4

1 回答 1

2

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

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

于 2013-09-16T14:15:44.437 回答