add kbm support

This commit is contained in:
2026-09-14 15:46:56 -07:00
parent f2d81a0bc6
commit d0db40a3ee
6 changed files with 646 additions and 22 deletions
+274 -1
View File
@@ -24,7 +24,280 @@ 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;
}
}
int CKeyboard::GetClipboardText(wchar_t *outText, int outCapacity) const
{
if (!outText || outCapacity <= 0)
return 0;
outText[0] = 0;
if (!OpenClipboard(g_hWnd))
return 0;
int copied = 0;
HANDLE h = GetClipboardData(CF_UNICODETEXT);
if (h)
{
const wchar_t *src = (const wchar_t *)GlobalLock(h);
if (src)
{
while (src[copied] && copied + 1 < outCapacity)
{
outText[copied] = src[copied];
++copied;
}
outText[copied] = 0;
GlobalUnlock(h);
}
}
CloseClipboard();
return copied;
}
bool CKeyboard::SetClipboardText(const wchar_t *text) const
{
if (!text)
return false;
size_t len = wcslen(text);
if (!OpenClipboard(g_hWnd))
return false;
EmptyClipboard();
bool ok = false;
HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(wchar_t));
if (h)
{
memcpy(GlobalLock(h), text, (len + 1) * sizeof(wchar_t));
GlobalUnlock(h);
ok = (SetClipboardData(CF_UNICODETEXT, h) != NULL);
if (!ok)
GlobalFree(h);
}
CloseClipboard();
return ok;
}
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)