1 package com.irtimaled.bbor.client.keyboard;
3 import net.minecraft.client.Minecraft;
4 import net.minecraft.client.settings.KeyBinding;
5 import net.minecraft.client.util.InputMappings;
6 import org.lwjgl.glfw.GLFW;
8 import java.util.HashSet;
11 public class KeyListener {
12 private static final Minecraft minecraft = Minecraft.getInstance();
13 private static long mainWindowHandle;
14 private static Set<Key> keys = new HashSet<>();
15 private static Set<CustomKeyBinding> keyBindings = new HashSet<>();
16 public static final String Category = "Bounding Box Outline Reloaded";
18 public static void init() {
19 mainWindowHandle = minecraft.mainWindow.getHandle();
20 GLFW.glfwSetKeyCallback(mainWindowHandle, KeyListener::onKeyEvent);
23 public static Key register(String description, String keyName) {
24 InputMappings.Input input = InputMappings.getInputByName(keyName);
25 CustomKeyBinding keyBinding = new CustomKeyBinding(description, input.getKeyCode());
26 keyBindings.add(keyBinding);
28 Key key = keyBinding.getCustomKey();
33 private static void onKeyEvent(long windowHandle, int keyCode, int scanCode, int action, int modifiers) {
34 boolean isPressed = action > 0;
35 if (windowHandle == mainWindowHandle &&
36 minecraft.currentScreen == null &&
38 !InputMappings.isKeyDown(292) &&
39 handleKeyEvent(keyCode, isPressed))
41 minecraft.keyboardListener.onKeyEvent(windowHandle, keyCode, scanCode, action, modifiers);
44 private static boolean handleKeyEvent(int keyCode, boolean isPressed) {
45 for (Key key : keys) {
46 if (key.handleKeyEvent(keyCode, isPressed)) {
53 public static KeyBinding[] keyBindings() {
54 return keyBindings.toArray(new KeyBinding[0]);