the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 91 lines 2.3 kB view raw
1#pragma once 2 3#ifdef _WINDOWS64 4 5#include <windows.h> 6 7// HID usage page and usage for raw input registration 8#ifndef HID_USAGE_PAGE_GENERIC 9#define HID_USAGE_PAGE_GENERIC ((USHORT)0x01) 10#endif 11#ifndef HID_USAGE_GENERIC_MOUSE 12#define HID_USAGE_GENERIC_MOUSE ((USHORT)0x02) 13#endif 14 15class KeyboardMouseInput 16{ 17public: 18 KeyboardMouseInput(); 19 ~KeyboardMouseInput(); 20 21 void Init(HWND hWnd); 22 void Tick(); 23 void EndFrame(); 24 25 // Called from WndProc 26 void OnKeyDown(WPARAM vk); 27 void OnKeyUp(WPARAM vk); 28 void OnRawMouseInput(LPARAM lParam); 29 void OnMouseButton(int button, bool down); 30 void OnMouseWheel(int delta); 31 void ClearAllState(); 32 33 // Per-frame edge detection (for UI / per-frame logic like Alt toggle) 34 bool IsKeyDown(int vk) const; 35 bool IsKeyPressed(int vk) const; 36 bool IsKeyReleased(int vk) const; 37 bool IsMouseDown(int btn) const; 38 bool IsMousePressed(int btn) const; 39 bool IsMouseReleased(int btn) const; 40 41 // Game-tick consume methods: accumulate across frames, clear on read. 42 // Use these from code that runs at game tick rate (20Hz). 43 bool ConsumeKeyPress(int vk); 44 bool ConsumeMousePress(int btn); 45 bool ConsumeMouseRelease(int btn); 46 void ConsumeMouseDelta(float &dx, float &dy); 47 int ConsumeScrollDelta(); 48 49 // Absolute cursor position (client-area coordinates, for GUI when not captured) 50 void OnMouseMove(int x, int y); 51 int GetMouseX() const; 52 int GetMouseY() const; 53 HWND GetHWnd() const; 54 55 // Mouse capture for FPS look 56 void SetCapture(bool capture); 57 bool IsCaptured() const; 58 59private: 60 void CenterCursor(); 61 62 // Per-frame double-buffered state (for IsKeyPressed/Released per-frame edge detection) 63 bool m_keyState[256]; 64 bool m_keyStatePrev[256]; 65 bool m_mouseButtons[3]; 66 bool m_mouseButtonsPrev[3]; 67 68 // Sticky press accumulators (persist until consumed by game tick) 69 bool m_keyPressedAccum[256]; 70 bool m_mousePressedAccum[3]; 71 bool m_mouseReleasedAccum[3]; 72 73 // Mouse delta accumulators (persist until consumed by game tick) 74 float m_mouseDeltaXAccum; 75 float m_mouseDeltaYAccum; 76 77 // Scroll accumulator (persists until consumed by game tick) 78 int m_scrollDeltaAccum; 79 80 bool m_captured; 81 HWND m_hWnd; 82 bool m_initialized; 83 84 // Absolute cursor position in client coordinates 85 int m_mouseX; 86 int m_mouseY; 87}; 88 89extern KeyboardMouseInput KMInput; 90 91#endif // _WINDOWS64