1 package com.irtimaled.bbor.client.keyboard;
3 import net.minecraft.client.MinecraftClient;
4 import net.minecraft.client.options.KeyBinding;
5 import net.minecraft.client.util.InputUtil;
6 import org.lwjgl.glfw.GLFW;
8 import java.util.HashSet;
11 public class KeyListener {
12 private static final MinecraftClient minecraft = MinecraftClient.getInstance();
13 private static long mainWindowHandle;
14 private static final Set<Key> keys = new HashSet<>();
15 private static final Set<CustomKeyBinding> keyBindings = new HashSet<>();
16 public static final String Category = "Bounding Box Outline Reloaded";
18 public static void init() {
19 mainWindowHandle = minecraft.getWindow().getHandle();
20 GLFW.glfwSetKeyCallback(mainWindowHandle, KeyListener::onKeyEvent);
23 public static Key register(String description, String keyName) {
24 InputUtil.Key input = InputUtil.fromTranslationKey(keyName);
25 CustomKeyBinding keyBinding = new CustomKeyBinding(description, input.getCode());
26 keyBindings.add(keyBinding);
28 Key key = keyBinding.getKey();
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 !InputUtil.isKeyPressed(mainWindowHandle, 292) &&
39 handleKeyEvent(keyCode, isPressed))
41 minecraft.keyboard.onKey(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]);