【问题标题】:Management inputs Mouse with DirectX DirectInput管理输入 带 DirectX DirectInput 的鼠标
【发布时间】:2013-09-16 14:06:21
【问题描述】:

我尝试使用 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-&gt;mouseX = -657this-&gt;mouseY = -36 而不是200200(大约)。我在初始化鼠标时检查了该功能,它们似乎可以工作(我按照教程进行操作)。

【问题讨论】:

    标签: c++ c++11 mouse


    【解决方案1】:

    我认为原因是 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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多