我正在制作基于fps的游戏。目前我有一个wnd_proc函数来捕获输入的所有事件(WM_MOUSEMOVE,WM_KEYUP,WM_KEYDOWN,...)。我的鼠标用于像fps一样移动相机。当我只移动鼠标时,它工作正常。但是当我按下按键移动角色并同时移动鼠标时,在游戏中检测鼠标移动会有延迟。当我按下多个键时,我也会注意到延迟。
对于键盘事件,我使用GetAsyncKeyState()来读取键盘内容,而不是使用我自己的输入类来检查键是否被按下,这样就修复了多键的延迟。但我的鼠标仍然是问题。是否有鼠标移动的异步功能?
- 编辑 -
这是我在wndproc函数中调用的输入系统类。
class InputSystem : public System
{
...
...
...
void ProcessInput(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LPBYTE lpb;
UINT dwSize;
RAWINPUT *raw;
switch (message)
{
case WM_INPUT:
{
GetRawInputData((HRAWINPUT)lParam,
RID_INPUT,
NULL,
&dwSize,
sizeof(RAWINPUTHEADER));
lpb = new BYTE[dwSize];
if (lpb == NULL)
return;
if (GetRawInputData((HRAWINPUT)lParam,
RID_INPUT,
lpb,
&dwSize,
sizeof(RAWINPUTHEADER)) != dwSize)
MessageBox(hWnd, L"GetRawInputData returning wrong size!", L"Orhhor! Bao zha liao!", MB_OK);
raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
if (raw->data.keyboard.Message == WM_KEYDOWN ||
raw->data.keyboard.Message == WM_SYSKEYDOWN)
{
input.ReadInput(raw->data.keyboard.VKey);
}
else if (raw->data.keyboard.Message == WM_KEYUP ||
raw->data.keyboard.Message == WM_SYSKEYUP)
{
input.ResetInput(raw->data.keyboard.VKey);
}
}
delete[] lpb;
return;
}
以下是我的输入类
class Input
{
bool keys[256];
bool prevKeys[256];
...
...
...
}
void Input::InitInput()
{
...
...
...
auto hWnd = CORE->GetSystem<WindowManager>()->GetWindowHandle();
RAWINPUTDEVICE Rid[2];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x06; //keyboard
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = hWnd;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == false)
assert("Keyboard not registered.");
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02; //mouse
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = hWnd;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[1])) == false)
assert("Mouse not registered.");
for (int i = 0; i < 256; ++i)
{
keys[i] = false;
prevKeys[i] = false;
}
}
void Input::ReadInput(WPARAM w)
{
keys[w] = true;
return;
}
void Input::ResetInput(WPARAM w)
{
keys[w] = prevKeys[w] = false;
return;
}
bool Input::KeyPressed(int key)
{
if (!prevKeys[key] && keys[key])
{
prevKeys[key] = true;
return true;
}
return false;
}
bool Input::KeyDown(int key)
{
return keys[key];
}