-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keyboard.h
61 lines (52 loc) · 1.57 KB
/
Keyboard.h
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
#ifndef FAGL_KEYBOARD_H
#define FAGL_KEYBOARD_H
#include "FAGL.h"
namespace fagl {
bool* key_held_down;
void (*HoldKeyAction)(int);
void (*PressedKeyAction)(int);
void (*ReleasedKeyAction)(int);
void KeyPressed(int key) {
key_held_down[key] = true;
PressedKeyAction(key);
}
void KeyReleased(int key) {
key_held_down[key] = false;
ReleasedKeyAction(key);
}
void ParseKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
KeyPressed(key);
}
else if (action == GLFW_RELEASE) {
KeyReleased(key);
}
}
/// <summary>
/// Parses currently held keys. Should be called once a frame.
/// </summary>
void ParseHoldKeys() {
for (unsigned int i = 0; i <= GLFW_KEY_LAST; i++) {
if (key_held_down[i])
HoldKeyAction(i);
}
}
/// <summary>
/// Sets the keyboard response of the window.
/// </summary>
/// <param name="window"> The window whose keyboard response will be set.</param>
/// <param name="hold"> The function to be called when a key is hold.</param>
/// <param name="press"> The function to be called when a key is pressed.</param>
/// <param name="release"> The function to be called when a key is released.</param>
void InitKeyboard(GLFWwindow* window, void (*hold)(int), void (*press)(int), void (*release)(int)) {
HoldKeyAction = hold;
PressedKeyAction = press;
ReleasedKeyAction = release;
key_held_down = new bool[GLFW_KEY_LAST + 1];
for (unsigned int i = 0; i <= GLFW_KEY_LAST; i++) {
key_held_down[i] = false;
}
glfwSetKeyCallback(window, ParseKeyboardEvent);
}
}
#endif