This repository was archived by the owner on Sep 17, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathKeyboard.java
More file actions
83 lines (68 loc) · 2.54 KB
/
Copy pathKeyboard.java
File metadata and controls
83 lines (68 loc) · 2.54 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
package Gprocessing.input;
import Gprocessing.graphics.Window;
import imgui.ImGuiIO;
import org.lwjgl.glfw.GLFWKeyCallback;
import static org.lwjgl.glfw.GLFW.*;
public class Keyboard {
public static int SPACE = 32; // Space Bar
public static int W_KEY = 87; // WASD
public static int A_KEY = 65;
public static int S_KEY = 83;
public static int D_KEY = 68;
public static int UP_ARROW = 38;
public static int LEFT_ARROW = 37;
public static int DOWN_ARROW = 40;
public static int RIGHT_ARROW = 39;
static ImGuiIO io = Gprocessing.ImGui.ImGuiLayer.io;
/**
* @param keyCode key-code representing the key to be checked.
* @return Returns true if the key is being pressed, otherwise returns false.
*/
public static boolean keyIsPressed(int keyCode) {
return glfwGetKey(Window.window, keyCode) == GLFW_PRESS;
}
private static boolean returnBoolPressed;
/**
* Returns true if a key is was just pressed, then returns false until the key is released and pressed again.
* @param keyCode key-code representing the key to be checked.
* @return Returns true if the key was just pressed, otherwise returns false.
*/
public static boolean keyPressed(int keyCode) {
glfwSetKeyCallback(Window.window, (w, key, scancode, action, mods) -> {
if (action == GLFW_PRESS) {
io.setKeysDown(key, true);
} else if (action == GLFW_RELEASE) {
io.setKeysDown(key, false);
}
io.setKeyCtrl(io.getKeysDown(GLFW_KEY_LEFT_CONTROL) || io.getKeysDown(GLFW_KEY_RIGHT_CONTROL));
io.setKeyShift(io.getKeysDown(GLFW_KEY_LEFT_SHIFT) || io.getKeysDown(GLFW_KEY_RIGHT_SHIFT));
io.setKeyAlt(io.getKeysDown(GLFW_KEY_LEFT_ALT) || io.getKeysDown(GLFW_KEY_RIGHT_ALT));
io.setKeySuper(io.getKeysDown(GLFW_KEY_LEFT_SUPER) || io.getKeysDown(GLFW_KEY_RIGHT_SUPER));
if (key == keyCode && action == GLFW_PRESS) returnBoolPressed = true;
});
if (returnBoolPressed) {
returnBoolPressed = false;
return true;
}
return false;
}
private static boolean returnBoolReleased;
/**
* Returns true if a key was just released.
* @param keyCode key-code representing the key to be checked.
* @return Returns true if the key was just released, otherwise returns false.
*/
public static boolean keyReleased(int keyCode) {
glfwSetKeyCallback(Window.window, new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == keyCode && action == GLFW_RELEASE) returnBoolReleased = true;
}
});
if (returnBoolReleased) {
returnBoolReleased = false;
return true;
}
return false;
}
}