我尝试使用DirectX输入来管理输入鼠标。但是当我试图获取鼠标的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 = -657和this->mouseY = -36,而不是200和200 (大约)。当我初始化鼠标时,我会检查这个函数,它们看起来很有效(我遵循了一个教程)。
发布于 2013-09-16 14:15:44
我认为原因是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
https://stackoverflow.com/questions/18830051
复制相似问题