1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#include "stdafx.h"
#ifdef _WINDOWS64
#include "KeyboardMouseInput.h"
KeyboardMouseInput KMInput;
KeyboardMouseInput::KeyboardMouseInput()
: m_mouseDeltaX(0.0f)
, m_mouseDeltaY(0.0f)
, m_mouseDeltaXAccum(0.0f)
, m_mouseDeltaYAccum(0.0f)
, m_scrollDelta(0)
, m_scrollDeltaAccum(0)
, m_captured(false)
, m_hWnd(NULL)
, m_initialized(false)
{
memset(m_keyState, 0, sizeof(m_keyState));
memset(m_keyStatePrev, 0, sizeof(m_keyStatePrev));
memset(m_mouseButtons, 0, sizeof(m_mouseButtons));
memset(m_mouseButtonsPrev, 0, sizeof(m_mouseButtonsPrev));
}
KeyboardMouseInput::~KeyboardMouseInput()
{
if (m_captured)
{
SetCapture(false);
}
}
void KeyboardMouseInput::Init(HWND hWnd)
{
m_hWnd = hWnd;
m_initialized = true;
// Register for raw mouse input
RAWINPUTDEVICE rid;
rid.usUsagePage = HID_USAGE_PAGE_GENERIC;
rid.usUsage = HID_USAGE_GENERIC_MOUSE;
rid.dwFlags = 0;
rid.hwndTarget = hWnd;
RegisterRawInputDevices(&rid, 1, sizeof(rid));
}
void KeyboardMouseInput::Tick()
{
// Snapshot accumulated mouse deltas
m_mouseDeltaX = m_mouseDeltaXAccum;
m_mouseDeltaY = m_mouseDeltaYAccum;
m_mouseDeltaXAccum = 0.0f;
m_mouseDeltaYAccum = 0.0f;
// Snapshot scroll delta
m_scrollDelta = m_scrollDeltaAccum;
m_scrollDeltaAccum = 0;
// Keep cursor pinned to center while captured
if (m_captured)
CenterCursor();
}
void KeyboardMouseInput::EndFrame()
{
// Advance previous state for next frame's edge detection.
// Must be called AFTER all consumers have read IsKeyPressed/Released etc.
memcpy(m_keyStatePrev, m_keyState, sizeof(m_keyState));
memcpy(m_mouseButtonsPrev, m_mouseButtons, sizeof(m_mouseButtons));
}
void KeyboardMouseInput::OnKeyDown(WPARAM vk)
{
if (vk < 256)
{
m_keyState[vk] = true;
}
}
void KeyboardMouseInput::OnKeyUp(WPARAM vk)
{
if (vk < 256)
{
m_keyState[vk] = false;
}
}
void KeyboardMouseInput::OnRawMouseInput(LPARAM lParam)
{
if (!m_captured) return;
UINT dwSize = 0;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
BYTE* lpb = (BYTE*)alloca(dwSize);
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize)
return;
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
if (raw->data.mouse.usFlags == MOUSE_MOVE_RELATIVE)
{
m_mouseDeltaXAccum += (float)raw->data.mouse.lLastX;
m_mouseDeltaYAccum += (float)raw->data.mouse.lLastY;
}
}
}
void KeyboardMouseInput::OnMouseButton(int button, bool down)
{
if (button >= 0 && button < 3)
{
m_mouseButtons[button] = down;
}
}
void KeyboardMouseInput::OnMouseWheel(int delta)
{
m_scrollDeltaAccum += delta;
}
void KeyboardMouseInput::ClearAllState()
{
memset(m_keyState, 0, sizeof(m_keyState));
memset(m_mouseButtons, 0, sizeof(m_mouseButtons));
m_mouseDeltaXAccum = 0.0f;
m_mouseDeltaYAccum = 0.0f;
m_scrollDeltaAccum = 0;
}
// Key queries
bool KeyboardMouseInput::IsKeyDown(int vk) const
{
if (vk < 0 || vk >= 256) return false;
return m_keyState[vk];
}
bool KeyboardMouseInput::IsKeyPressed(int vk) const
{
if (vk < 0 || vk >= 256) return false;
return m_keyState[vk] && !m_keyStatePrev[vk];
}
bool KeyboardMouseInput::IsKeyReleased(int vk) const
{
if (vk < 0 || vk >= 256) return false;
return !m_keyState[vk] && m_keyStatePrev[vk];
}
// Mouse button queries
bool KeyboardMouseInput::IsMouseDown(int btn) const
{
if (btn < 0 || btn >= 3) return false;
return m_mouseButtons[btn];
}
bool KeyboardMouseInput::IsMousePressed(int btn) const
{
if (btn < 0 || btn >= 3) return false;
return m_mouseButtons[btn] && !m_mouseButtonsPrev[btn];
}
bool KeyboardMouseInput::IsMouseReleased(int btn) const
{
if (btn < 0 || btn >= 3) return false;
return !m_mouseButtons[btn] && m_mouseButtonsPrev[btn];
}
// Delta queries
float KeyboardMouseInput::GetMouseDeltaX() const { return m_mouseDeltaX; }
float KeyboardMouseInput::GetMouseDeltaY() const { return m_mouseDeltaY; }
int KeyboardMouseInput::GetScrollDelta() const { return m_scrollDelta; }
// Mouse capture
void KeyboardMouseInput::SetCapture(bool capture)
{
if (capture == m_captured) return;
m_captured = capture;
if (capture)
{
ShowCursor(FALSE);
RECT rect;
GetClientRect(m_hWnd, &rect);
POINT topLeft = { rect.left, rect.top };
POINT bottomRight = { rect.right, rect.bottom };
ClientToScreen(m_hWnd, &topLeft);
ClientToScreen(m_hWnd, &bottomRight);
RECT screenRect = { topLeft.x, topLeft.y, bottomRight.x, bottomRight.y };
ClipCursor(&screenRect);
CenterCursor();
// Flush accumulated deltas so the snap-to-center doesn't cause a jump
m_mouseDeltaXAccum = 0.0f;
m_mouseDeltaYAccum = 0.0f;
}
else
{
ShowCursor(TRUE);
ClipCursor(NULL);
}
}
bool KeyboardMouseInput::IsCaptured() const { return m_captured; }
void KeyboardMouseInput::CenterCursor()
{
RECT rect;
GetClientRect(m_hWnd, &rect);
POINT center = { (rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2 };
ClientToScreen(m_hWnd, ¢er);
SetCursorPos(center.x, center.y);
}
#endif // _WINDOWS64
|