Refactor: Route key input through EventBus

- Post EventKey from 1.8.9 KeyBinding/GuiScreen hooks

- Hook 26.2 KeyboardHandler#keyPress so keys fire while walking

- Normalize 26.2 GLFW key codes to 1.8.9 LWJGL 2 codes

- Drive the GUI toggle from InputController instead of tick polling
This commit is contained in:
xiaozhou233 committed 2026-11-07 16:48:26 +08:00
1 parent 70d8a854ea
commit a84b04877d
18 files changed
+352 -164

No files matched your search

@@ -2,20 +2,21 @@ package cn.xiaozhou233.orangex.event.facade;
import cn.xiaozhou233.orangex.OrangeX;
import cn.xiaozhou233.orangex.input.ClientInput;
import cn.xiaozhou233.orangex.input.KeyCodes;
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
/**
* Entry point used by injected 1.8.9 {@code GuiScreen} / {@code KeyBinding}
* code.
* Input entry point shared by injected 1.8.9 code and the 26.2 input screen.
*
* <p>Every vanilla screen goes through these methods, so each callback first
* checks whether the instance is the empty input screen created by OrangeX.</p>
* checks whether the instance is the empty input screen created by OrangeX.
* Key events are not handled here: they are posted as
* {@link cn.xiaozhou233.orangex.event.impl.EventKey} so features can subscribe
* to them, and {@link cn.xiaozhou233.orangex.input.InputController} consumes
* the GUI toggle.</p>
*/
public final class InputEvents {
/** LWJGL 2 {@code Keyboard.KEY_M}. */
private static final int KEY_M = 50;
private static final boolean DEBUG = Boolean.getBoolean("orangex.debugInput");
private static boolean failureLogged;
@@ -36,44 +37,34 @@ public final class InputEvents {
/**
* Called from {@code KeyBinding#onTick(int)} for every key press while no
* screen is open. This queues the 1.8.9 open; it is applied at the end of
* the tick so the same key event is not also delivered to the new screen.
* screen is open. The injected code already dropped auto-repeat events.
*/
public static void onKeyPressed(int keyCode) {
if (keyCode != KEY_M) {
return;
}
try {
Minecraft minecraft = minecraft();
if (minecraft == null || minecraft.isScreenOpen()) {
return;
}
boolean inWorld = minecraft.isInWorld();
if (DEBUG) {
System.out.println("[OrangeX] M pressed: inWorld=" + inWorld);
}
if (inWorld) {
ClientInput.requestOpen();
}
} catch (Throwable throwable) {
logFailure(throwable);
if (DEBUG) {
System.out.println("[OrangeX] key pressed: " + keyCode);
}
KeyEvents.fire(keyCode);
}
/**
* Called from {@code GuiScreen#keyTyped(char, int)}. This queues the
* 1.8.9 close; it is applied at the end of the tick.
* Called from the 26.2 {@code KeyboardHandler#keyPress} hook. Translates
* the GLFW key code to the canonical 1.8.9 numbering before posting.
*/
public static void onGlfwKeyPressed(int glfwKeyCode) {
onKeyPressed(KeyCodes.fromGlfw(glfwKeyCode));
}
/**
* Called from {@code GuiScreen#keyTyped(char, int)}. Returns {@code true}
* only for the toggle key, so other keys (ESC, etc.) still reach vanilla
* screen handling.
*/
public static boolean onKeyTyped(Object screen, int keyCode) {
if (!isOurs(screen) || keyCode != KEY_M) {
if (!isOurs(screen)) {
return false;
}
try {
ClientInput.requestClose();
} catch (Throwable throwable) {
logFailure(throwable);
}
return true;
KeyEvents.fire(keyCode);
return keyCode == KeyCodes.M;
}
public static void onDrawScreen(Object screen, int mouseX, int mouseY) {
@@ -0,0 +1,41 @@
package cn.xiaozhou233.orangex.event.facade;
import cn.xiaozhou233.orangex.OrangeX;
import cn.xiaozhou233.orangex.event.impl.EventKey;
import cn.xiaozhou233.orangex.input.KeyCodes;
import org.greenrobot.eventbus.EventBus;
/**
* Entry point for raw key events.
*
* <p>Injected code and version-specific input code only call this facade;
* subscribers receive {@link EventKey} through the shared EventBus. All key
* codes are normalised to the 1.8.9 / LWJGL 2 numbering.</p>
*/
public final class KeyEvents {
private static final EventBus BUS = OrangeX.getInstance().getEventBus();
private static boolean failureLogged;
/**
* @param key canonical 1.8.9 (LWJGL 2) key code.
*/
public static void fire(int key) {
if (key == KeyCodes.NONE) {
return;
}
try {
BUS.post(new EventKey(key));
} catch (Throwable throwable) {
if (!failureLogged) {
failureLogged = true;
System.err.println("[OrangeX] Key event dispatch failed");
throwable.printStackTrace();
}
}
}
private KeyEvents() {
}
}
@@ -0,0 +1,18 @@
package cn.xiaozhou233.orangex.event.impl;
import cn.xiaozhou233.orangex.event.Event;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* A key press delivered through the EventBus.
*
* <p>{@link #getKey()} always uses the 1.8.9 / LWJGL 2 numbering, even when
* the game runs on 26.2. Modern GLFW key codes are translated before the
* event is posted (see {@code KeyCodes#fromGlfw(int)}).</p>
*/
@Getter
@AllArgsConstructor
public class EventKey implements Event {
private final int key;
}
@@ -57,11 +57,11 @@ public final class GuiInputHook extends ClassHook {
injectBefore(ctClass, "mouseClickMove", "(IIIJ)V",
"orangexbridge.Hooks.inputDrag($0, $1, $2, $3);");
// Close with M while the empty screen is open. Repeat events are
// ignored so holding the key does not bounce the screen.
// Forward every key typed on our empty screen as an EventKey. The
// facade only consumes the toggle key so ESC and other keys keep
// their vanilla Screen behaviour.
injectBefore(ctClass, "keyTyped", "(CI)V",
"if ($2 == orangexbridge.Legacy.TOGGLE_KEY"
+ " && !orangexbridge.Legacy.isRepeatEvent()"
"if (!orangexbridge.Legacy.isRepeatEvent()"
+ " && orangexbridge.Hooks.inputKeyTyped($0, $2)) return;");
// The empty screen is an overlay: it must not pause singleplayer.
@@ -11,16 +11,28 @@ import javassist.CtClass;
import javassist.CtMethod;
/**
* 1.8.9 key-press hook.
* Key-press hook for both supported versions.
*
* <p>{@code KeyBinding#onTick(int)} is called for every key press while no
* screen is open. Using it for the GUI toggle avoids missing a short tap
* between two 20 Hz client ticks.</p>
* <ul>
* <li>1.8.9: {@code KeyBinding#onTick(int)} is called for every key press
* while no screen is open. This keeps short taps from being missed
* between two 20 Hz client ticks.</li>
* <li>26.2: {@code KeyboardHandler#keyPress(long, int, KeyEvent)} is the
* GLFW callback and therefore runs for every key press, including while
* walking with no screen open. The input screen's own
* {@code Screen#keyPressed} is not enough because the game does not
* deliver keys to a screen that is not open.</li>
* </ul>
*/
public final class KeyPressHook extends ClassHook {
private static final String KEY_BINDING =
"net/minecraft/client/settings/KeyBinding";
private static final String KEYBOARD_HANDLER =
"net/minecraft/client/KeyboardHandler";
/** GLFW_PRESS. */
private static final int GLFW_PRESS = 1;
private static final RuntimeInfo runtimeInfo =
OrangeX.getInstance().getRuntimeInfo();
@@ -32,6 +44,9 @@ public final class KeyPressHook extends ClassHook {
case V1_8_9:
return resolver.mapClass(KEY_BINDING)
.getTargetName().replace("/", ".");
case V26_2:
return resolver.mapClass(KEYBOARD_HANDLER)
.getTargetName().replace("/", ".");
default:
return null;
}
@@ -39,19 +54,42 @@ public final class KeyPressHook extends ClassHook {
@Override
protected void transform(CtClass ctClass) throws Exception {
if (runtimeInfo.getMinecraftVersion() != MinecraftVersion.V1_8_9) {
return;
switch (runtimeInfo.getMinecraftVersion()) {
case V1_8_9:
transformLegacy(ctClass);
break;
case V26_2:
transformModern(ctClass);
break;
default:
break;
}
}
private void transformLegacy(CtClass ctClass) throws Exception {
MappingMethod mapping = resolver.mapMethod(
KEY_BINDING, "onTick", "(I)V");
CtMethod method = ctClass.getMethod(
mapping.getTargetName(), mapping.getTargetDescriptor());
// Static method: $1 is the key code. Ignore auto-repeat so holding
// M does not toggle repeatedly.
// $1 is the key code. Ignore auto-repeat so holding a key does not
// flood the EventBus. Every other key is forwarded as an EventKey.
method.insertBefore(
"if ($1 == orangexbridge.Legacy.TOGGLE_KEY"
+ " && !orangexbridge.Legacy.isRepeatEvent())"
"if (!orangexbridge.Legacy.isRepeatEvent())"
+ " { orangexbridge.Hooks.inputKeyPressed($1); }");
}
private void transformModern(CtClass ctClass) throws Exception {
MappingMethod mapping = resolver.mapMethod(
KEYBOARD_HANDLER,
"keyPress",
"(JILnet/minecraft/client/input/KeyEvent;)V");
CtMethod method = ctClass.getMethod(
mapping.getTargetName(), mapping.getTargetDescriptor());
// $2 is the GLFW action (1 = press, 0 = release, 2 = repeat).
// $3 is the KeyEvent record; its key() is a GLFW key code, which the
// facade normalises to the 1.8.9 numbering.
method.insertBefore(
"if ($2 == " + GLFW_PRESS + ")"
+ " { orangexbridge.Hooks.inputGlfwKeyPressed($3.key()); }");
}
}
@@ -16,8 +16,7 @@ import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
public final class ClientInput {
/**
* GUI toggle key. The actual key code is version-specific and is handled
* by {@code VersionInput}; this is only for documentation.
* GUI toggle key name. The canonical key code is {@link KeyCodes#M}.
*/
public static final String TOGGLE_KEY_NAME = "M";
@@ -28,7 +27,6 @@ public final class ClientInput {
private static volatile boolean rightDown;
private static volatile boolean middleDown;
private static boolean toggleWasDown;
private static boolean started;
private static boolean pendingOpen;
private static boolean pendingClose;
@@ -37,7 +35,9 @@ public final class ClientInput {
}
/**
* Called once per client tick by {@link InputController}.
* Called once per client tick by {@link InputController}. The toggle
* itself is event-driven on every supported version; this only reconciles
* the tracked state with the actual Minecraft screen.
*/
public static void tick(Minecraft minecraft) {
if (!started) {
@@ -52,39 +52,17 @@ public final class ClientInput {
} else if (!open && screenOpen) {
onScreenOpened(minecraft.getGuiMouseX(), minecraft.getGuiMouseY());
}
boolean down = minecraft.isToggleKeyDown();
if (minecraft.isToggleEventDriven()) {
// 1.8.9 opens from KeyBinding#onTick (pendingOpen). Closing is
// polled here and applied at the end of the tick so the same key
// event is not also delivered to the freshly opened screen.
if (open && down && !toggleWasDown) {
pendingClose = true;
}
} else if (down && !toggleWasDown) {
// 26.2: no injected Screen subclass key handling, so opening /
// closing here cannot bounce.
if (open) {
minecraft.closeInputScreen();
} else if (minecraft.isInWorld() && !minecraft.isScreenOpen()) {
minecraft.openInputScreen();
}
}
toggleWasDown = down;
}
/**
* Applies a queued 1.8.9 toggle after the tick's keyboard/mouse handling.
* Applies queued open / close requests after the tick's keyboard and mouse
* handling.
*/
public static void applyPending(Minecraft minecraft) {
if (pendingOpen) {
pendingOpen = false;
if (!open && minecraft.isInWorld() && !minecraft.isScreenOpen()) {
minecraft.openInputScreen();
// The key event that opened the screen is still held down.
toggleWasDown = true;
}
}
@@ -92,7 +70,6 @@ public final class ClientInput {
pendingClose = false;
if (open) {
minecraft.closeInputScreen();
toggleWasDown = true;
}
}
}
@@ -105,6 +82,23 @@ public final class ClientInput {
pendingClose = true;
}
/**
* Handles a key event posted on the EventBus. Only the canonical toggle
* key (M) changes the GUI state; all other keys are ignored here but stay
* available to other subscribers.
*/
public static void onKey(Minecraft minecraft, int key) {
if (minecraft == null || key != KeyCodes.M) {
return;
}
if (minecraft.isInputScreenOpen()) {
requestClose();
} else if (minecraft.isInWorld() && !minecraft.isScreenOpen()) {
requestOpen();
}
}
public static void onMouseMove(double x, double y) {
mouseX = x;
mouseY = y;
@@ -117,7 +111,6 @@ public final class ClientInput {
open = true;
mouseX = x;
mouseY = y;
toggleWasDown = true;
System.out.println("[OrangeX] Input screen opened");
}
@@ -1,5 +1,6 @@
package cn.xiaozhou233.orangex.input;
import cn.xiaozhou233.orangex.event.impl.EventKey;
import cn.xiaozhou233.orangex.event.impl.EventTickPost;
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
@@ -7,7 +8,8 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/**
* Drives {@link ClientInput} from the game tick.
* Drives {@link ClientInput}: handles {@code EventKey} from the EventBus and
* reconciles the input screen state around the game tick.
*/
public final class InputController {
@@ -18,6 +20,20 @@ public final class InputController {
this.minecraft = minecraft;
}
/**
* Consumes key events posted by the injected input hooks. Only the toggle
* key changes the input screen state; every event is still visible to
* other subscribers.
*/
@Subscribe(threadMode = ThreadMode.POSTING)
public void onKey(EventKey event) {
try {
ClientInput.onKey(minecraft, event.getKey());
} catch (Throwable throwable) {
logFailure(throwable);
}
}
@Subscribe(threadMode = ThreadMode.POSTING)
public void onTick(EventTickPre event) {
try {
@@ -0,0 +1,153 @@
package cn.xiaozhou233.orangex.input;
/**
* Canonical key codes used by {@link cn.xiaozhou233.orangex.event.impl.EventKey}.
*
* <p>All events use the 1.8.9 / LWJGL 2 numbering, regardless of the running
* Minecraft version. Modern versions report GLFW key codes, so
* {@link #fromGlfw(int)} translates them before the event is posted. Keys that
* did not exist in 1.8.9 map to {@link #NONE}.</p>
*/
public final class KeyCodes {
/** LWJGL 2 {@code Keyboard.KEY_NONE}. */
public static final int NONE = 0;
/** LWJGL 2 {@code Keyboard.KEY_M}. */
public static final int M = 50;
/**
* Translates a GLFW key code (26.x) to the 1.8.9 LWJGL 2 key code used by
* {@code EventKey}.
*/
public static int fromGlfw(int key) {
switch (key) {
// Printable keys.
case 32: return 57; // SPACE
case 39: return 40; // APOSTROPHE
case 44: return 51; // COMMA
case 45: return 12; // MINUS
case 46: return 52; // PERIOD
case 47: return 53; // SLASH
case 48: return 11; // 0
case 49: return 2; // 1
case 50: return 3; // 2
case 51: return 4; // 3
case 52: return 5; // 4
case 53: return 6; // 5
case 54: return 7; // 6
case 55: return 8; // 7
case 56: return 9; // 8
case 57: return 10; // 9
case 59: return 39; // SEMICOLON
case 61: return 13; // EQUAL
case 65: return 30; // A
case 66: return 48; // B
case 67: return 46; // C
case 68: return 32; // D
case 69: return 18; // E
case 70: return 33; // F
case 71: return 34; // G
case 72: return 35; // H
case 73: return 23; // I
case 74: return 36; // J
case 75: return 37; // K
case 76: return 38; // L
case 77: return 50; // M
case 78: return 49; // N
case 79: return 24; // O
case 80: return 25; // P
case 81: return 16; // Q
case 82: return 19; // R
case 83: return 31; // S
case 84: return 20; // T
case 85: return 22; // U
case 86: return 47; // V
case 87: return 17; // W
case 88: return 45; // X
case 89: return 21; // Y
case 90: return 44; // Z
case 91: return 26; // LEFT_BRACKET
case 92: return 43; // BACKSLASH
case 93: return 27; // RIGHT_BRACKET
case 96: return 41; // GRAVE_ACCENT
// Function keys.
case 256: return 1; // ESCAPE
case 257: return 28; // ENTER
case 258: return 15; // TAB
case 259: return 14; // BACKSPACE
case 260: return 210; // INSERT
case 261: return 211; // DELETE
case 262: return 205; // RIGHT
case 263: return 203; // LEFT
case 264: return 208; // DOWN
case 265: return 200; // UP
case 266: return 201; // PAGE_UP
case 267: return 209; // PAGE_DOWN
case 268: return 199; // HOME
case 269: return 207; // END
case 280: return 58; // CAPS_LOCK
case 281: return 70; // SCROLL_LOCK
case 282: return 69; // NUM_LOCK
case 283: return 183; // PRINT_SCREEN
case 284: return 197; // PAUSE
case 290: return 59; // F1
case 291: return 60; // F2
case 292: return 61; // F3
case 293: return 62; // F4
case 294: return 63; // F5
case 295: return 64; // F6
case 296: return 65; // F7
case 297: return 66; // F8
case 298: return 67; // F9
case 299: return 68; // F10
case 300: return 87; // F11
case 301: return 88; // F12
case 302: return 100; // F13
case 303: return 101; // F14
case 304: return 102; // F15
case 305: return 103; // F16
case 306: return 104; // F17
case 307: return 105; // F18
case 308: return 113; // F19
// Keypad.
case 320: return 82; // KP_0
case 321: return 79; // KP_1
case 322: return 80; // KP_2
case 323: return 81; // KP_3
case 324: return 75; // KP_4
case 325: return 76; // KP_5
case 326: return 77; // KP_6
case 327: return 71; // KP_7
case 328: return 72; // KP_8
case 329: return 73; // KP_9
case 330: return 83; // KP_DECIMAL
case 331: return 181; // KP_DIVIDE
case 332: return 55; // KP_MULTIPLY
case 333: return 74; // KP_SUBTRACT
case 334: return 78; // KP_ADD
case 335: return 156; // KP_ENTER
case 336: return 141; // KP_EQUAL
// Modifiers.
case 340: return 42; // LEFT_SHIFT
case 341: return 29; // LEFT_CONTROL
case 342: return 56; // LEFT_ALT
case 343: return 219; // LEFT_SUPER
case 344: return 54; // RIGHT_SHIFT
case 345: return 157; // RIGHT_CONTROL
case 346: return 184; // RIGHT_ALT
case 347: return 220; // RIGHT_SUPER
case 348: return 221; // MENU
default:
return NONE;
}
}
private KeyCodes() {
}
}
@@ -36,18 +36,6 @@ public interface VersionInput {
void closeInputScreen();
/**
* M: {@code Keyboard.KEY_M} on 1.8.9, GLFW 77 on 26.2.
*/
boolean isToggleKeyDown();
/**
* {@code true} when opening/closing is driven by injected key events
* instead of polling {@link #isToggleKeyDown()} once per tick. 1.8.9 uses
* this so a very short key tap is not missed between ticks.
*/
boolean isToggleEventDriven();
double getGuiMouseX();
double getGuiMouseY();
@@ -36,9 +36,6 @@ import java.lang.reflect.Modifier;
*/
public final class V1_8_9Input implements VersionInput {
/** LWJGL 2 {@code Keyboard.KEY_M}. */
public static final int TOGGLE_KEY = Keyboard.KEY_M;
private static final String MINECRAFT_CLASS = "net/minecraft/client/Minecraft";
private static final String GUI_SCREEN_CLASS = "net/minecraft/client/gui/GuiScreen";
private static final String GENERATED_SCREEN =
@@ -274,16 +271,6 @@ public final class V1_8_9Input implements VersionInput {
}
}
@Override
public boolean isToggleKeyDown() {
return Keyboard.isKeyDown(TOGGLE_KEY);
}
@Override
public boolean isToggleEventDriven() {
return true;
}
/**
* Used by injected {@code GuiScreen}/{@code KeyBinding} code to ignore
* LWJGL auto-repeat events.
@@ -3,7 +3,6 @@ package cn.xiaozhou233.orangex.input.v26_2;
import cn.xiaozhou233.orangex.input.ClientInput;
import cn.xiaozhou233.orangex.input.internal.VersionInput;
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
@@ -15,9 +14,6 @@ import net.minecraft.client.gui.screens.Screen;
*/
public final class V26_2Input implements VersionInput {
/** GLFW_KEY_M. */
private static final int TOGGLE_KEY = 77;
private final Minecraft minecraft;
private Screen shellScreen;
@@ -68,16 +64,6 @@ public final class V26_2Input implements VersionInput {
ClientInput.reset();
}
@Override
public boolean isToggleKeyDown() {
return InputConstants.isKeyDown(minecraft.getWindow(), TOGGLE_KEY);
}
@Override
public boolean isToggleEventDriven() {
return false;
}
@Override
public double getGuiMouseX() {
return minecraft.mouseHandler.getScaledXPos(minecraft.getWindow());
@@ -49,17 +49,6 @@ public interface Minecraft {
*/
void closeInputScreen();
/**
* {@code true} while the GUI toggle key (M) is held.
*/
boolean isToggleKeyDown();
/**
* {@code true} when the toggle is driven by key events (1.8.9) rather
* than per-tick polling (26.2).
*/
boolean isToggleEventDriven();
/**
* Current cursor X in Minecraft GUI pixels.
*/
@@ -97,16 +97,6 @@ public final class V1_8_9Minecraft implements Minecraft {
input().closeInputScreen();
}
@Override
public boolean isToggleKeyDown() {
return input().isToggleKeyDown();
}
@Override
public boolean isToggleEventDriven() {
return input().isToggleEventDriven();
}
@Override
public double getGuiMouseX() {
return input().getGuiMouseX();
@@ -95,16 +95,6 @@ public final class V26_2Minecraft implements Minecraft {
input().closeInputScreen();
}
@Override
public boolean isToggleKeyDown() {
return input().isToggleKeyDown();
}
@Override
public boolean isToggleEventDriven() {
return input().isToggleEventDriven();
}
@Override
public double getGuiMouseX() {
return input().getGuiMouseX();
@@ -61,6 +61,10 @@ public final class Hooks {
InputEvents.onKeyPressed(keyCode);
}
public static void inputGlfwKeyPressed(int glfwKeyCode) {
InputEvents.onGlfwKeyPressed(glfwKeyCode);
}
public static boolean isOurScreen(Object screen) {
return InputEvents.isOurInputScreen(screen);
}
@@ -10,9 +10,6 @@ import cn.xiaozhou233.orangex.input.internal.v1_8_9.V1_8_9Input;
*/
public final class Legacy {
/** LWJGL 2 {@code Keyboard.KEY_M}. */
public static final int TOGGLE_KEY = 50;
public static boolean isRepeatEvent() {
return V1_8_9Input.isRepeatEvent();
}
+2 -1
View File
@@ -289,8 +289,9 @@ OrangeX/src/main/java/cn/xiaozhou233/orangex/
├── facade/
│ ├── TickEvents.java
│ ├── Render2DEvents.java
│ ├── KeyEvents.java
│ └── InputEvents.java
└── impl/EventTickPre.java, EventTickPost.java, EventRender2D.java
└── impl/EventTickPre.java, EventTickPost.java, EventRender2D.java, EventKey.java
OrangeX/src/main/java/orangexbridge/
├── Hooks.java 注入代码统一入口
+17 -11
View File
@@ -280,29 +280,35 @@ Minecraft262Stubs/
OrangeX/src/main/java/cn/xiaozhou233/orangex/
├── input/
│ ├── ClientInput.java 跨版本输入状态:开/关、鼠标坐标、按键状态
│ ├── InputController.java 在 EventTickPre 中驱动 ClientInput
│ ├── InputController.java 订阅 EventKey 并驱动 ClientInput
│ ├── KeyCodes.java 统一的按键编号(1.8.9 LWJGL 2)+ GLFW 转换
│ ├── internal/
│ │ ├── VersionInput.java 版本输入接口
│ │ └── v1_8_9/V1_8_9Input.java 1.8.9:运行时生成 GuiScreen 子类 + LWJGL 2
│ └── v26_2/
│ ├── V26_2Input.java 26.2:gui.setScreen + InputConstants
│ ├── V26_2Input.java 26.2:gui.setScreen
│ └── V26_2InputScreen.java 26.2 空 Screen
├── event/facade/InputEvents.java 1.8.9 注入代码的门面
├── event/
│ ├── facade/InputEvents.java 输入门面(1.8.9 注入代码)
│ ├── facade/KeyEvents.java 按键事件统一投递到 EventBus
│ └── impl/EventKey.java 按键事件(统一使用 1.8.9 key code)
└── hook/impl/
├── GuiInputHook.java 1.8.9 GuiScreen 输入 / 关闭 Hook
└── KeyPressHook.java 1.8.9 KeyBinding.onTick 打开 Hook
├── GuiInputHook.java 1.8.9 GuiScreen 输入 / 按键 Hook
└── KeyPressHook.java 1.8.9 KeyBinding#onTick / 26.2 KeyboardHandler#keyPress 按键 Hook
```
行为:
- M 键在**世界内**打开空 GUI;再次按下关闭。
- 1.8.9 的开关走事件而不是 tick 轮询:
- 打开:`KeyBinding#onTick(int)` → `InputEvents.onKeyPressed` → `ClientInput.requestOpen()`
- 关闭:`GuiScreen#keyTyped(char, int)` → `InputEvents.onKeyTyped` → `ClientInput.requestClose()`
- 两者都只在 tick 末尾(`EventTickPost` → `ClientInput.applyPending`)真正执行。
- 所有 `EventKey#getKey()` 都使用 1.8.9 / LWJGL 2 键码,26.2 的 GLFW 键码在投递前通过 `KeyCodes.fromGlfw` 转换。
- 按键统一走 EventBus:
- 1.8.9 打开:`KeyBinding#onTick(int)` → `InputEvents.onKeyPressed` → `KeyEvents.fire` → `EventBus.post(EventKey)` → `InputController.onKey` → `ClientInput.requestOpen()`
- 1.8.9 关闭:`GuiScreen#keyTyped(char, int)` → `InputEvents.onKeyTyped` → 同一条 EventBus 链路 → `requestClose()`
- 26.2:`KeyboardHandler#keyPress(long, int, KeyEvent)` 全局 Hook → `InputEvents.onGlfwKeyPressed` → 键码转换 → 同一条 EventBus 链路。行走、无界面时也能收到按键,不再依赖 tick 轮询。
- 其他功能可以直接 `@Subscribe` `EventKey` 读取按键,不需要再改注入代码。
- 打开 / 关闭都只在 tick 末尾(`EventTickPost` → `ClientInput.applyPending`)真正执行。
- 原因:如果在键盘循环里直接打开 Screen,同一个 M 事件会立刻被 `currentScreen.handleKeyboardInput()` 再处理一次,导致刚打开就关闭。
- 自动重复事件通过 `V1_8_9Input.isRepeatEvent()` 过滤,按住 M 不会反复开关。
- 26.2 仍然每 tick 用 `InputConstants.isKeyDown(window, 77)` 轮询 M。
- 自动重复事件:1.8.9 通过 `Keyboard.isRepeatEvent()` 过滤,26.2 只处理 GLFW action == 1(press),按住 M 不会反复开关。
- 打开时:
- 1.8.9:运行时生成 `GuiScreen` 具体子类,再 `Minecraft#displayGuiScreen(...)`;`doesGuiPauseGame()` 对自己的空壳返回 `false`。
- 26.2:`Gui#setScreen(new V26_2InputScreen())`,`isPauseScreen()` 返回 `false`。