add kbm support

This commit is contained in:
2026-08-13 09:09:09 +03:00
parent 88a26c82a6
commit 4110db8572
6 changed files with 575 additions and 23 deletions
+225 -1
View File
@@ -24,7 +24,231 @@ SOFTWARE.
#include "INP_Keyboard.h"
void CKeyboard::Tick(void) {}
CKeyboard::CKeyboard()
{
m_lParam = nullptr;
m_onInput = nullptr;
ZeroMemory(m_chrText, sizeof(m_chrText));
memset(m_keysPrev, 0, sizeof(m_keysPrev));
memset(m_keysCurr, 0, sizeof(m_keysCurr));
m_mouseX = m_mouseY = 0;
m_lButtonPrev = m_lButtonCurr = false;
m_rButtonPrev = m_rButtonCurr = false;
m_mButtonPrev = m_mButtonCurr = false;
m_mouseWheel = 0;
memset(m_charBuffer, 0, sizeof(m_charBuffer));
m_charCount = 0;
m_mouseCaptured = false;
m_mouseDeltaX = m_mouseDeltaY = 0;
m_windowFocused = true;
}
void CKeyboard::Init()
{
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC
rid.usUsage = 0x02; // HID_USAGE_GENERIC_MOUSE
rid.dwFlags = 0;
rid.hwndTarget = g_hWnd;
RegisterRawInputDevices(&rid, 1, sizeof(rid));
}
void CKeyboard::Tick(void)
{
memcpy(m_keysPrev, m_keysCurr, sizeof(m_keysCurr));
m_lButtonPrev = m_lButtonCurr;
m_rButtonPrev = m_rButtonCurr;
m_mButtonPrev = m_mButtonCurr;
m_mouseWheel = 0;
m_charCount = 0;
m_mouseDeltaX = m_mouseDeltaAccumX;
m_mouseDeltaY = m_mouseDeltaAccumY;
m_mouseDeltaAccumX = 0;
m_mouseDeltaAccumY = 0;
if (m_mouseCaptured && g_hWnd && m_windowFocused && g_hWnd)
{
RECT rc;
GetClientRect(g_hWnd, &rc);
POINT center;
center.x = (rc.right - rc.left) / 2;
center.y = (rc.bottom - rc.top) / 2;
ClientToScreen(g_hWnd, &center);
SetCursorPos(center.x, center.y);
}
}
void CKeyboard::OnKeyDown(int vkCode)
{
if (vkCode >= 0 && vkCode < MAX_KEYS)
{
m_keysCurr[vkCode] = true;
}
}
void CKeyboard::OnKeyUp(int vkCode)
{
if (vkCode >= 0 && vkCode < MAX_KEYS)
{
m_keysCurr[vkCode] = false;
}
}
void CKeyboard::OnChar(wchar_t ch)
{
if (m_charCount < MAX_CHARS_PER_FRAME)
{
m_charBuffer[m_charCount++] = ch;
}
}
bool CKeyboard::IsKeyDown(int vkCode) const
{
if (vkCode >= 0 && vkCode < MAX_KEYS)
return m_keysCurr[vkCode];
return false;
}
bool CKeyboard::IsKeyPressed(int vkCode) const
{
if (vkCode >= 0 && vkCode < MAX_KEYS)
return m_keysCurr[vkCode] && !m_keysPrev[vkCode];
return false;
}
bool CKeyboard::IsKeyReleased(int vkCode) const
{
if (vkCode >= 0 && vkCode < MAX_KEYS)
return !m_keysCurr[vkCode] && m_keysPrev[vkCode];
return false;
}
void CKeyboard::OnMouseMove(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
}
void CKeyboard::OnRawMouseDelta(int dx, int dy)
{
m_mouseDeltaAccumX += dx;
m_mouseDeltaAccumY += dy;
}
void CKeyboard::OnLButtonDown(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_lButtonCurr = true;
}
void CKeyboard::OnLButtonUp(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_lButtonCurr = false;
}
void CKeyboard::OnRButtonDown(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_rButtonCurr = true;
}
void CKeyboard::OnRButtonUp(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_rButtonCurr = false;
}
void CKeyboard::OnMButtonDown(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_mButtonCurr = true;
}
void CKeyboard::OnMButtonUp(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_mButtonCurr = false;
}
void CKeyboard::OnMouseWheel(int delta, int x, int y)
{
m_mouseX = x;
m_mouseY = y;
m_mouseWheel += delta;
}
void CKeyboard::SetMouseCaptured(bool grabbed)
{
if (m_mouseCaptured == grabbed)
return;
m_mouseCaptured = grabbed;
if (grabbed && g_hWnd)
{
while (ShowCursor(FALSE) >= 0) {}
RECT rc;
GetClientRect(g_hWnd, &rc);
POINT topLeft = { rc.left, rc.top };
POINT bottomRight = { rc.right, rc.bottom };
ClientToScreen(g_hWnd, &topLeft);
ClientToScreen(g_hWnd, &bottomRight);
RECT clipRect = { topLeft.x, topLeft.y, bottomRight.x, bottomRight.y };
ClipCursor(&clipRect);
GetClientRect(g_hWnd, &rc);
POINT center;
center.x = (rc.right - rc.left) / 2;
center.y = (rc.bottom - rc.top) / 2;
ClientToScreen(g_hWnd, &center);
SetCursorPos(center.x, center.y);
m_mouseDeltaAccumX = 0;
m_mouseDeltaAccumY = 0;
}
else if (!grabbed && g_hWnd)
{
while (ShowCursor(TRUE) < 0) {}
ClipCursor(nullptr);
}
}
void CKeyboard::SetWindowFocused(bool focused)
{
m_windowFocused = focused;
if (focused)
{
if (m_mouseCaptured)
{
while (ShowCursor(FALSE) >= 0) {}
RECT rc;
GetClientRect(g_hWnd, &rc);
POINT center;
center.x = (rc.right - rc.left) / 2;
center.y = (rc.bottom - rc.top) / 2;
ClientToScreen(g_hWnd, &center);
SetCursorPos(center.x, center.y);
}
else
{
while (ShowCursor(TRUE) < 0) {}
ClipCursor(nullptr);
}
}
else
{
while (ShowCursor(TRUE) < 0) {}
ClipCursor(nullptr);
}
}
EKeyboardResult CKeyboard::RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int (*Func)(LPVOID, const bool), LPVOID lpParam,
C_4JInput::EKeyboardMode eMode)