-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboard.cpp
More file actions
72 lines (58 loc) · 1.45 KB
/
Keyboard.cpp
File metadata and controls
72 lines (58 loc) · 1.45 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
#include "Keyboard.h"
#include <algorithm>
bool Keyboard::mode[3];
std::vector<int> Keyboard::p_keys;
void Keyboard::callback(GLFWwindow* window, int key, int scancode, int action, int _mode) {
if (action == GLFW_PRESS) {
p_keys.push_back(key);
} else if (action == GLFW_RELEASE) {
p_keys.erase(std::remove(p_keys.begin(), p_keys.end(), key), p_keys.end());
}
switch (_mode) {
case GLFW_MOD_CONTROL:
mode[0] = true;
break;
case GLFW_MOD_SHIFT:
mode[1] = true;
break;
case GLFW_MOD_ALT:
mode[2] = true;
break;
default:
mode[0] = mode[1] = mode[2] = false;
}
}
Keyboard::Keyboard(GLFWwindow* window) {
glfwSetKeyCallback(window, callback);
}
bool Keyboard::keyPressed(int key) const {
if (!p_keys.empty())
return std::find(p_keys.begin(), p_keys.end(), key) != p_keys.end();
else
return false;
}
bool Keyboard::keyPressedOnce(int key) const {
if (!p_keys.empty()) {
if (std::find(p_keys.begin(), p_keys.end(), key) != p_keys.end()) {
p_keys.erase(std::remove(p_keys.begin(), p_keys.end(), key), p_keys.end());
return true;
}
}
return false;
}
int Keyboard::getPressedKey() const {
if (!p_keys.empty())
return p_keys.front();
else
return Keyboard::UNKNOWN;
}
std::vector<int> Keyboard::pressedKeys() {
return p_keys;
}
int Keyboard::keyMode() const {
int result = 0;
if (mode[0]) result |= GLFW_MOD_CONTROL;
if (mode[1]) result |= GLFW_MOD_SHIFT;
if (mode[2]) result |= GLFW_MOD_ALT;
return result;
}