gpt4 book ai didi

c++ - 管理输入 Mouse with DirectX DirectInput

转载 作者:行者123 更新时间:2023-11-30 04:14:35 30 4
gpt4 key购买 nike

我尝试使用 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 而不是 200 200(大约)。我在初始化鼠标时检查了功能,它们似乎可以正常工作(我遵循了教程)。

最佳答案

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

关于c++ - 管理输入 Mouse with DirectX DirectInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830051/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com