/* MIT License Copyright (c) 2026 Patoke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "INP_Keyboard.h" 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, ¢er); 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, ¢er); 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, ¢er); 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) { ClearGlobalText(); SeedEditBox(); Func(lpParam, C_4JInput::EKeyboardMode_Numeric); return EKeyboard_ResultAccept; } void CKeyboard::GetText(uint16_t *UTF16String) { uint16_t *keyString = GetGlobalText(); for (unsigned int index = 0; keyString[index] && index < 0x200; index++) { UTF16String[index] = keyString[index]; } }