-
-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathinput.cpp
More file actions
192 lines (175 loc) · 6.82 KB
/
input.cpp
File metadata and controls
192 lines (175 loc) · 6.82 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
/*
* Copyright (C) 2014 Patrick Mours
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "input.hpp"
#include <cassert>
reshade::input::input(window_handle window)
: _window(window)
{
}
bool reshade::input::is_key_down(unsigned int keycode) const
{
assert(keycode < std::size(_keys));
return keycode < std::size(_keys) && (_keys[keycode] & 0x80) == 0x80;
}
bool reshade::input::is_key_pressed(unsigned int keycode) const
{
assert(keycode < std::size(_keys));
return keycode > 0 && keycode < std::size(_keys) && (_keys[keycode] & 0x88) == 0x88 && !is_key_repeated(keycode);
}
bool reshade::input::is_key_pressed(unsigned int keycode, bool ctrl, bool shift, bool alt, bool force_modifiers) const
{
if (keycode == 0)
return false;
const bool key_down = is_key_pressed(keycode), ctrl_down = is_key_down(key_ctrl), shift_down = is_key_down(key_shift), alt_down = is_key_down(key_alt);
if (force_modifiers) // Modifier state is required to match
return key_down && (ctrl == ctrl_down && shift == shift_down && alt == alt_down);
else // Modifier state is optional and only has to match when down
return key_down && (!ctrl || ctrl_down) && (!shift || shift_down) && (!alt || alt_down);
}
bool reshade::input::is_key_released(unsigned int keycode) const
{
assert(keycode < std::size(_keys));
return keycode > 0 && keycode < std::size(_keys) && (_keys[keycode] & 0x88) == 0x08;
}
bool reshade::input::is_key_repeated(unsigned int keycode) const
{
assert(keycode < std::size(_keys));
return keycode < std::size(_keys) && (_last_keys[keycode] & 0x80) == 0x80 && (_keys[keycode] & 0x80) == 0x80;
}
bool reshade::input::is_any_key_down() const
{
// Skip mouse buttons
for (unsigned int i = key_button_xbutton2 + 1; i < std::size(_keys); i++)
if (is_key_down(i))
return true;
return false;
}
bool reshade::input::is_any_key_pressed() const
{
return last_key_pressed() != 0;
}
bool reshade::input::is_any_key_released() const
{
return last_key_released() != 0;
}
unsigned int reshade::input::last_key_pressed() const
{
for (unsigned int i = key_button_middle; i < std::size(_keys); i++)
if (is_key_pressed(i))
return i;
return 0;
}
unsigned int reshade::input::last_key_released() const
{
for (unsigned int i = key_button_middle; i < std::size(_keys); i++)
if (is_key_released(i))
return i;
return 0;
}
bool reshade::input::is_mouse_button_down(unsigned int button) const
{
assert(button < 5);
return is_key_down(key_button_left + button + (button < 2 ? 0 : 1)); // VK_CANCEL is being ignored by runtime
}
bool reshade::input::is_mouse_button_pressed(unsigned int button) const
{
assert(button < 5);
return is_key_pressed(key_button_left + button + (button < 2 ? 0 : 1)); // VK_CANCEL is being ignored by runtime
}
bool reshade::input::is_mouse_button_released(unsigned int button) const
{
assert(button < 5);
return is_key_released(key_button_left + button + (button < 2 ? 0 : 1)); // VK_CANCEL is being ignored by runtime
}
bool reshade::input::is_any_mouse_button_down() const
{
for (unsigned int i = 0; i < 5; i++)
if (is_mouse_button_down(i))
return true;
return false;
}
bool reshade::input::is_any_mouse_button_pressed() const
{
for (unsigned int i = 0; i < 5; i++)
if (is_mouse_button_pressed(i))
return true;
return false;
}
bool reshade::input::is_any_mouse_button_released() const
{
for (unsigned int i = 0; i < 5; i++)
if (is_mouse_button_released(i))
return true;
return false;
}
std::string reshade::input::key_name(unsigned int keycode)
{
if (keycode >= 256)
return std::string();
if (keycode == key_home && is_keyboard_layout_german())
return "Pos1";
static const char *keyboard_keys[256] = {
"", "Left Mouse", "Right Mouse", "Cancel", "Middle Mouse", "X1 Mouse", "X2 Mouse", "", "Backspace", "Tab", "", "", "Clear", "Enter", "", "",
"Shift", "Control", "Alt", "Pause", "Caps Lock", "", "", "", "", "", "", "Escape", "", "", "", "",
"Space", "Page Up", "Page Down", "End", "Home", "Left Arrow", "Up Arrow", "Right Arrow", "Down Arrow", "Select", "", "", "Print Screen", "Insert", "Delete", "Help",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "", "", "", "", "",
"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Left Windows", "Right Windows", "Apps", "", "Sleep",
"Numpad 0", "Numpad 1", "Numpad 2", "Numpad 3", "Numpad 4", "Numpad 5", "Numpad 6", "Numpad 7", "Numpad 8", "Numpad 9", "Numpad *", "Numpad +", "", "Numpad -", "Numpad Decimal", "Numpad /",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16",
"F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "", "", "", "", "", "", "", "",
"Num Lock", "Scroll Lock", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Left Shift", "Right Shift", "Left Control", "Right Control", "Left Menu", "Right Menu", "Browser Back", "Browser Forward", "Browser Refresh", "Browser Stop", "Browser Search", "Browser Favorites", "Browser Home", "Volume Mute", "Volume Down", "Volume Up",
"Next Track", "Previous Track", "Media Stop", "Media Play/Pause", "Mail", "Media Select", "Launch App 1", "Launch App 2", "", "", "OEM ;", "OEM +", "OEM ,", "OEM -", "OEM .", "OEM /",
"OEM ~", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "OEM [", "OEM \\", "OEM ]", "OEM '", "OEM 8",
"", "", "OEM <", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "Attn", "CrSel", "ExSel", "Erase EOF", "Play", "Zoom", "", "PA1", "OEM Clear", ""
};
return keyboard_keys[keycode];
}
std::string reshade::input::key_name(const unsigned int key[4])
{
assert(key[0] != key_ctrl && key[0] != key_shift && key[0] != key_alt);
return (key[1] ? "Ctrl + " : std::string()) + (key[2] ? "Shift + " : std::string()) + (key[3] ? "Alt + " : std::string()) + key_name(key[0]);
}
void reshade::input::block_mouse_input(bool enable)
{
_block_mouse = enable;
}
void reshade::input::block_keyboard_input(bool enable)
{
_block_keyboard = enable;
}
reshade::input_gamepad::input_gamepad()
{
}
bool reshade::input_gamepad::is_button_down(unsigned int button) const
{
assert(
button == button_dpad_up ||
button == button_dpad_down ||
button == button_dpad_left ||
button == button_dpad_right ||
button == button_start ||
button == button_back ||
button == button_left_thumb ||
button == button_right_thumb ||
button == button_left_shoulder ||
button == button_right_shoulder ||
button == button_a ||
button == button_b ||
button == button_x ||
button == button_y);
return (_buttons & button) != 0;
}
bool reshade::input_gamepad::is_button_pressed(unsigned int button) const
{
return (_buttons & button) != 0 && (_last_buttons & button) == 0;
}
bool reshade::input_gamepad::is_button_released(unsigned int button) const
{
return (_buttons & button) == 0 && (_last_buttons & button) != 0;
}