-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputs.h
More file actions
193 lines (171 loc) · 6.17 KB
/
Copy pathInputs.h
File metadata and controls
193 lines (171 loc) · 6.17 KB
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
///----------------------------------------------------------------------------------------------------
/// Copyright (c) Raidcore.GG - All rights reserved.
///
/// Name : Inputs.h
/// Description : Contains a variety of utility for WndProc/Inputs.
/// Authors : K. Bieniek
///----------------------------------------------------------------------------------------------------
#pragma once
#include <cstdint>
#include <windows.h>
///----------------------------------------------------------------------------------------------------
/// EMouseButtons Enumeration
///----------------------------------------------------------------------------------------------------
enum class EMouseButtons
{
None,
LMB,
RMB,
MMB,
M4,
M5
};
///----------------------------------------------------------------------------------------------------
/// EModifiers Enumeration
///----------------------------------------------------------------------------------------------------
enum class EModifiers
{
None = 0,
Shift = 1 << 0,
Ctrl = 1 << 1,
Alt = 1 << 2
};
#ifdef DEFINE_ENUM_FLAG_OPERATORS
DEFINE_ENUM_FLAG_OPERATORS(EModifiers);
#endif
///----------------------------------------------------------------------------------------------------
/// KeystrokeMessageFlags struct
///----------------------------------------------------------------------------------------------------
struct KeystrokeMessageFlags
{
unsigned RepeatCount : 16;
unsigned ScanCode : 8;
unsigned ExtendedFlag : 1;
unsigned Reserved : 4;
unsigned ContextCode : 1;
unsigned PreviousKeyState : 1;
unsigned TransitionState : 1;
///----------------------------------------------------------------------------------------------------
/// GetScanCode:
/// Returns the scancode (including extended key flag) of the LParam.
///----------------------------------------------------------------------------------------------------
inline uint16_t GetScanCode()
{
uint16_t ret = ScanCode;
if (ExtendedFlag)
{
ret |= 0xE000;
}
return ret;
}
KeystrokeMessageFlags() = default;
inline KeystrokeMessageFlags(LPARAM aLParam)
{
/* direct assignment is not possible, so we cast and then copy */
KeystrokeMessageFlags tmp = *(KeystrokeMessageFlags*)&aLParam;
RepeatCount = tmp.RepeatCount;
ScanCode = tmp.ScanCode;
ExtendedFlag = tmp.ExtendedFlag;
Reserved = tmp.Reserved;
ContextCode = tmp.ContextCode;
PreviousKeyState = tmp.PreviousKeyState;
TransitionState = tmp.TransitionState;
}
};
///----------------------------------------------------------------------------------------------------
/// LParamToKMF:
/// Casts an LPARAM to a KeystrokeMessageFlags.
///----------------------------------------------------------------------------------------------------
inline KeystrokeMessageFlags& LParamToKMF(LPARAM& aLParam)
{
return *(KeystrokeMessageFlags*)&aLParam;
}
///----------------------------------------------------------------------------------------------------
/// KMFToLParam:
/// Casts an KeystrokeMessageFlags to a LPARAM.
///----------------------------------------------------------------------------------------------------
inline LPARAM& KMFToLParam(KeystrokeMessageFlags& aKmf)
{
return *(LPARAM*)&aKmf;
}
///----------------------------------------------------------------------------------------------------
/// GetKeyMessageLPARAM:
/// Creates an LPARAM for key messages.
///----------------------------------------------------------------------------------------------------
inline LPARAM GetKeyMessageLPARAM(unsigned aVKey, bool aIsDown, bool aIsSystem)
{
KeystrokeMessageFlags lp;
UINT sc = MapVirtualKeyA(aVKey, MAPVK_VK_TO_VSC_EX);
lp.RepeatCount = 1;
lp.ScanCode = LOBYTE(sc);
lp.ExtendedFlag = (HIBYTE(sc) == 0xE0 || HIBYTE(sc) == 0xE1) ? 1 : 0;
lp.Reserved = 0;
lp.ContextCode = aIsSystem ? 1 : 0;
lp.PreviousKeyState = aIsDown ? 0 : 1;
lp.TransitionState = aIsDown ? 0 : 1;
return KMFToLParam(lp);
}
///----------------------------------------------------------------------------------------------------
/// GetKeyMessageLPARAM_ScanCode:
/// Creates an LPARAM for key messages.
///----------------------------------------------------------------------------------------------------
inline LPARAM GetKeyMessageLPARAM_ScanCode(unsigned short aScanCode, bool aIsDown, bool aIsSystem)
{
KeystrokeMessageFlags lp = GetKeyMessageLPARAM(0, aIsDown, aIsSystem);
lp.ScanCode = LOBYTE(aScanCode);
lp.ExtendedFlag = (HIBYTE(aScanCode) == 0xE0 || HIBYTE(aScanCode) == 0xE1) ? 1 : 0;
return KMFToLParam(lp);
}
///----------------------------------------------------------------------------------------------------
/// GetMouseMessageWPARAM:
/// Creates an WPARAM for mouse messages.
///----------------------------------------------------------------------------------------------------
inline WPARAM GetMouseMessageWPARAM(EMouseButtons aMouseButton, bool aIsCtrl, bool aIsShift, bool aIsDown)
{
int lo = 0;
int ho = 0;
switch (aMouseButton)
{
case EMouseButtons::LMB:
lo = aIsDown ? MK_LBUTTON : 0;
break;
case EMouseButtons::RMB:
lo = aIsDown ? MK_RBUTTON : 0;
break;
case EMouseButtons::MMB:
lo = aIsDown ? MK_MBUTTON : 0;
break;
case EMouseButtons::M4:
lo = aIsDown ? MK_XBUTTON1 : 0;
ho = XBUTTON1;
break;
case EMouseButtons::M5:
lo = aIsDown ? MK_XBUTTON2 : 0;
ho = XBUTTON2;
break;
}
return MAKEWPARAM(
(
(aIsCtrl ? MK_CONTROL : 0) |
(aIsShift ? MK_SHIFT : 0) |
(lo)
),( (ho) )
);
}
///----------------------------------------------------------------------------------------------------
/// Inputs Namespace
///----------------------------------------------------------------------------------------------------
namespace Inputs
{
///----------------------------------------------------------------------------------------------------
/// IsCursorHidden:
/// Returns true if the cursor is hidden.
///----------------------------------------------------------------------------------------------------
inline bool IsCursorHidden()
{
CURSORINFO curInfo{};
curInfo.cbSize = sizeof(CURSORINFO);
GetCursorInfo(&curInfo);
return !(curInfo.flags & CURSOR_SHOWING);
}
}