From bc0bf578b5a80772084e6ed4a1ae5dd8a5d40b60 Mon Sep 17 00:00:00 2001 From: kappa-maintainer <11038263+kappa-maintainer@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:20:06 +0800 Subject: [PATCH] Add workaround for input method users (#33) * Add IME workaround. * spotless * Clearer naming for KeyCodes conversion functions * add -> offer to not crash/deadlock if the queue overflows * Make the IME hacks configurable, and off by default (because they're quite hacky) --------- Co-authored-by: Raven Szewczyk --- .../me/eigenraven/lwjgl3ify/core/Config.java | 14 ++ src/main/java/org/lwjglx/input/KeyCodes.java | 236 +++++++++++++++++- src/main/java/org/lwjglx/input/Keyboard.java | 22 +- src/main/java/org/lwjglx/opengl/Display.java | 40 ++- 4 files changed, 299 insertions(+), 13 deletions(-) diff --git a/src/main/java/me/eigenraven/lwjgl3ify/core/Config.java b/src/main/java/me/eigenraven/lwjgl3ify/core/Config.java index 3857399..40935f8 100644 --- a/src/main/java/me/eigenraven/lwjgl3ify/core/Config.java +++ b/src/main/java/me/eigenraven/lwjgl3ify/core/Config.java @@ -49,6 +49,10 @@ public class Config { public static boolean DEBUG_PRINT_KEY_EVENTS = false; public static boolean DEBUG_PRINT_MOUSE_EVENTS = false; + public static boolean IME_ENABLED = false; + public static boolean IME_F12_TOGGLE = false; + public static boolean IME_SYS_TOGGLE = false; + public static boolean SHOW_JAVA_VERSION = true; public static boolean SHOW_LWJGL_VERSION = true; @@ -69,6 +73,7 @@ public class Config { public static final String CATEGORY_MIXIN = "mixin"; public static final String CATEGORY_CORE = "core"; + public static final String CATEGORY_IME = "ime"; public static final String CATEGORY_DEBUG = "debug"; public static final String CATEGORY_WINDOW = "window"; public static final String CATEGORY_INPUT = "input"; @@ -131,6 +136,15 @@ public class Config { DEBUG_PRINT_MOUSE_EVENTS, "Print mouse-related events to the log"); + IME_ENABLED = config.getBoolean("enabled", CATEGORY_IME, IME_ENABLED, "Enables IME support for CJK input."); + IME_F12_TOGGLE = config + .getBoolean("f12Toggle", CATEGORY_IME, IME_F12_TOGGLE, "Enables switching IME mode by pressing F12."); + IME_SYS_TOGGLE = config.getBoolean( + "sysToggle", + CATEGORY_IME, + IME_SYS_TOGGLE, + "Enables switching IME mode by pressing Ctrl/LWin + Shift/Space"); + SHOW_JAVA_VERSION = config .getBoolean("showJavaVersion", CATEGORY_CORE, SHOW_JAVA_VERSION, "Show java version in the debug hud"); SHOW_LWJGL_VERSION = config.getBoolean( diff --git a/src/main/java/org/lwjglx/input/KeyCodes.java b/src/main/java/org/lwjglx/input/KeyCodes.java index 60714b1..b7e67cb 100644 --- a/src/main/java/org/lwjglx/input/KeyCodes.java +++ b/src/main/java/org/lwjglx/input/KeyCodes.java @@ -1,10 +1,12 @@ package org.lwjglx.input; +import java.awt.event.KeyEvent; + import org.lwjgl.glfw.GLFW; public class KeyCodes { - public static int toLwjglKey(int glfwKeyCode) { + public static int glfwToLwjgl(int glfwKeyCode) { if (glfwKeyCode > GLFW.GLFW_KEY_LAST) { return glfwKeyCode; } @@ -150,7 +152,7 @@ public class KeyCodes { }; } - public static int toGlfwKey(int lwjglKeyCode) { + public static int lwjglToGlfw(int lwjglKeyCode) { if (lwjglKeyCode > GLFW.GLFW_KEY_LAST) { return lwjglKeyCode; } @@ -299,4 +301,234 @@ public class KeyCodes { default -> GLFW.GLFW_KEY_UNKNOWN; }; } + + public static int awtToLwjgl(int awtCode) { + return switch (awtCode) { + case KeyEvent.VK_ESCAPE -> Keyboard.KEY_ESCAPE; + case KeyEvent.VK_1 -> Keyboard.KEY_1; + case KeyEvent.VK_2 -> Keyboard.KEY_2; + case KeyEvent.VK_3 -> Keyboard.KEY_3; + case KeyEvent.VK_4 -> Keyboard.KEY_4; + case KeyEvent.VK_5 -> Keyboard.KEY_5; + case KeyEvent.VK_6 -> Keyboard.KEY_6; + case KeyEvent.VK_7 -> Keyboard.KEY_7; + case KeyEvent.VK_8 -> Keyboard.KEY_8; + case KeyEvent.VK_9 -> Keyboard.KEY_9; + case KeyEvent.VK_0 -> Keyboard.KEY_0; + case KeyEvent.VK_MINUS -> Keyboard.KEY_MINUS; + case KeyEvent.VK_EQUALS -> Keyboard.KEY_EQUALS; + case KeyEvent.VK_BACK_SPACE -> Keyboard.KEY_BACK; + case KeyEvent.VK_TAB -> Keyboard.KEY_TAB; + case KeyEvent.VK_Q -> Keyboard.KEY_Q; + case KeyEvent.VK_W -> Keyboard.KEY_W; + case KeyEvent.VK_E -> Keyboard.KEY_E; + case KeyEvent.VK_R -> Keyboard.KEY_R; + case KeyEvent.VK_T -> Keyboard.KEY_T; + case KeyEvent.VK_Y -> Keyboard.KEY_Y; + case KeyEvent.VK_U -> Keyboard.KEY_U; + case KeyEvent.VK_I -> Keyboard.KEY_I; + case KeyEvent.VK_O -> Keyboard.KEY_O; + case KeyEvent.VK_P -> Keyboard.KEY_P; + case KeyEvent.VK_OPEN_BRACKET -> Keyboard.KEY_LBRACKET; + case KeyEvent.VK_CLOSE_BRACKET -> Keyboard.KEY_RBRACKET; + case KeyEvent.VK_ENTER -> Keyboard.KEY_RETURN; + case KeyEvent.VK_CONTROL -> Keyboard.KEY_LCONTROL; + case KeyEvent.VK_A -> Keyboard.KEY_A; + case KeyEvent.VK_S -> Keyboard.KEY_S; + case KeyEvent.VK_D -> Keyboard.KEY_D; + case KeyEvent.VK_F -> Keyboard.KEY_F; + case KeyEvent.VK_G -> Keyboard.KEY_G; + case KeyEvent.VK_H -> Keyboard.KEY_H; + case KeyEvent.VK_J -> Keyboard.KEY_J; + case KeyEvent.VK_K -> Keyboard.KEY_K; + case KeyEvent.VK_L -> Keyboard.KEY_L; + case KeyEvent.VK_SEMICOLON -> Keyboard.KEY_SEMICOLON; + case KeyEvent.VK_QUOTE -> Keyboard.KEY_APOSTROPHE; + case KeyEvent.VK_DEAD_GRAVE -> Keyboard.KEY_GRAVE; + case KeyEvent.VK_SHIFT -> Keyboard.KEY_LSHIFT; + case KeyEvent.VK_BACK_SLASH -> Keyboard.KEY_BACKSLASH; + case KeyEvent.VK_Z -> Keyboard.KEY_Z; + case KeyEvent.VK_X -> Keyboard.KEY_X; + case KeyEvent.VK_C -> Keyboard.KEY_C; + case KeyEvent.VK_V -> Keyboard.KEY_V; + case KeyEvent.VK_B -> Keyboard.KEY_B; + case KeyEvent.VK_N -> Keyboard.KEY_N; + case KeyEvent.VK_M -> Keyboard.KEY_M; + case KeyEvent.VK_COMMA -> Keyboard.KEY_COMMA; + case KeyEvent.VK_PERIOD -> Keyboard.KEY_PERIOD; + case KeyEvent.VK_SLASH -> Keyboard.KEY_SLASH; + case KeyEvent.VK_MULTIPLY -> Keyboard.KEY_MULTIPLY; + case KeyEvent.VK_ALT -> Keyboard.KEY_LMENU; + case KeyEvent.VK_SPACE -> Keyboard.KEY_SPACE; + case KeyEvent.VK_CAPS_LOCK -> Keyboard.KEY_CAPITAL; + case KeyEvent.VK_F1 -> Keyboard.KEY_F1; + case KeyEvent.VK_F2 -> Keyboard.KEY_F2; + case KeyEvent.VK_F3 -> Keyboard.KEY_F3; + case KeyEvent.VK_F4 -> Keyboard.KEY_F4; + case KeyEvent.VK_F5 -> Keyboard.KEY_F5; + case KeyEvent.VK_F6 -> Keyboard.KEY_F6; + case KeyEvent.VK_F7 -> Keyboard.KEY_F7; + case KeyEvent.VK_F8 -> Keyboard.KEY_F8; + case KeyEvent.VK_F9 -> Keyboard.KEY_F9; + case KeyEvent.VK_F10 -> Keyboard.KEY_F10; + case KeyEvent.VK_NUM_LOCK -> Keyboard.KEY_NUMLOCK; + case KeyEvent.VK_SCROLL_LOCK -> Keyboard.KEY_SCROLL; + case KeyEvent.VK_NUMPAD7 -> Keyboard.KEY_NUMPAD7; + case KeyEvent.VK_NUMPAD8 -> Keyboard.KEY_NUMPAD8; + case KeyEvent.VK_NUMPAD9 -> Keyboard.KEY_NUMPAD9; + case KeyEvent.VK_SUBTRACT -> Keyboard.KEY_SUBTRACT; + case KeyEvent.VK_NUMPAD4 -> Keyboard.KEY_NUMPAD4; + case KeyEvent.VK_NUMPAD5 -> Keyboard.KEY_NUMPAD5; + case KeyEvent.VK_NUMPAD6 -> Keyboard.KEY_NUMPAD6; + case KeyEvent.VK_ADD -> Keyboard.KEY_ADD; + case KeyEvent.VK_NUMPAD1 -> Keyboard.KEY_NUMPAD1; + case KeyEvent.VK_NUMPAD2 -> Keyboard.KEY_NUMPAD2; + case KeyEvent.VK_NUMPAD3 -> Keyboard.KEY_NUMPAD3; + case KeyEvent.VK_NUMPAD0 -> Keyboard.KEY_NUMPAD0; + case KeyEvent.VK_DECIMAL -> Keyboard.KEY_DECIMAL; + case KeyEvent.VK_F11 -> Keyboard.KEY_F11; + case KeyEvent.VK_F12 -> Keyboard.KEY_F12; + case KeyEvent.VK_F13 -> Keyboard.KEY_F13; + case KeyEvent.VK_F14 -> Keyboard.KEY_F14; + case KeyEvent.VK_F15 -> Keyboard.KEY_F15; + case KeyEvent.VK_KANA -> Keyboard.KEY_KANA; + case KeyEvent.VK_CONVERT -> Keyboard.KEY_CONVERT; + case KeyEvent.VK_NONCONVERT -> Keyboard.KEY_NOCONVERT; + case KeyEvent.VK_CIRCUMFLEX -> Keyboard.KEY_CIRCUMFLEX; + case KeyEvent.VK_AT -> Keyboard.KEY_AT; + case KeyEvent.VK_COLON -> Keyboard.KEY_COLON; + case KeyEvent.VK_UNDERSCORE -> Keyboard.KEY_UNDERLINE; + case KeyEvent.VK_KANJI -> Keyboard.KEY_KANJI; + case KeyEvent.VK_STOP -> Keyboard.KEY_STOP; + case KeyEvent.VK_DIVIDE -> Keyboard.KEY_DIVIDE; + case KeyEvent.VK_PAUSE -> Keyboard.KEY_PAUSE; + case KeyEvent.VK_HOME -> Keyboard.KEY_HOME; + case KeyEvent.VK_UP -> Keyboard.KEY_UP; + case KeyEvent.VK_PAGE_UP -> Keyboard.KEY_PRIOR; + case KeyEvent.VK_LEFT -> Keyboard.KEY_LEFT; + case KeyEvent.VK_RIGHT -> Keyboard.KEY_RIGHT; + case KeyEvent.VK_END -> Keyboard.KEY_END; + case KeyEvent.VK_DOWN -> Keyboard.KEY_DOWN; + case KeyEvent.VK_PAGE_DOWN -> Keyboard.KEY_NEXT; + case KeyEvent.VK_INSERT -> Keyboard.KEY_INSERT; + case KeyEvent.VK_DELETE -> Keyboard.KEY_DELETE; + case KeyEvent.VK_META -> Keyboard.KEY_LWIN; + default -> Keyboard.KEY_NONE; + }; + } + + public static int lwjglToAwt(int lwjglCode) { + return switch (lwjglCode) { + case Keyboard.KEY_ESCAPE -> KeyEvent.VK_ESCAPE; + case Keyboard.KEY_1 -> KeyEvent.VK_1; + case Keyboard.KEY_2 -> KeyEvent.VK_2; + case Keyboard.KEY_3 -> KeyEvent.VK_3; + case Keyboard.KEY_4 -> KeyEvent.VK_4; + case Keyboard.KEY_5 -> KeyEvent.VK_5; + case Keyboard.KEY_6 -> KeyEvent.VK_6; + case Keyboard.KEY_7 -> KeyEvent.VK_7; + case Keyboard.KEY_8 -> KeyEvent.VK_8; + case Keyboard.KEY_9 -> KeyEvent.VK_9; + case Keyboard.KEY_0 -> KeyEvent.VK_0; + case Keyboard.KEY_MINUS -> KeyEvent.VK_MINUS; + case Keyboard.KEY_EQUALS -> KeyEvent.VK_EQUALS; + case Keyboard.KEY_BACK -> KeyEvent.VK_BACK_SPACE; + case Keyboard.KEY_TAB -> KeyEvent.VK_TAB; + case Keyboard.KEY_Q -> KeyEvent.VK_Q; + case Keyboard.KEY_W -> KeyEvent.VK_W; + case Keyboard.KEY_E -> KeyEvent.VK_E; + case Keyboard.KEY_R -> KeyEvent.VK_R; + case Keyboard.KEY_T -> KeyEvent.VK_T; + case Keyboard.KEY_Y -> KeyEvent.VK_Y; + case Keyboard.KEY_U -> KeyEvent.VK_U; + case Keyboard.KEY_I -> KeyEvent.VK_I; + case Keyboard.KEY_O -> KeyEvent.VK_O; + case Keyboard.KEY_P -> KeyEvent.VK_P; + case Keyboard.KEY_LBRACKET -> KeyEvent.VK_OPEN_BRACKET; + case Keyboard.KEY_RBRACKET -> KeyEvent.VK_CLOSE_BRACKET; + case Keyboard.KEY_RETURN -> KeyEvent.VK_ENTER; + case Keyboard.KEY_LCONTROL -> KeyEvent.VK_CONTROL; + case Keyboard.KEY_A -> KeyEvent.VK_A; + case Keyboard.KEY_S -> KeyEvent.VK_S; + case Keyboard.KEY_D -> KeyEvent.VK_D; + case Keyboard.KEY_F -> KeyEvent.VK_F; + case Keyboard.KEY_G -> KeyEvent.VK_G; + case Keyboard.KEY_H -> KeyEvent.VK_H; + case Keyboard.KEY_J -> KeyEvent.VK_J; + case Keyboard.KEY_K -> KeyEvent.VK_K; + case Keyboard.KEY_L -> KeyEvent.VK_L; + case Keyboard.KEY_SEMICOLON -> KeyEvent.VK_SEMICOLON; + case Keyboard.KEY_APOSTROPHE -> KeyEvent.VK_QUOTE; + case Keyboard.KEY_GRAVE -> KeyEvent.VK_DEAD_GRAVE; + case Keyboard.KEY_LSHIFT -> KeyEvent.VK_SHIFT; + case Keyboard.KEY_BACKSLASH -> KeyEvent.VK_BACK_SLASH; + case Keyboard.KEY_Z -> KeyEvent.VK_Z; + case Keyboard.KEY_X -> KeyEvent.VK_X; + case Keyboard.KEY_C -> KeyEvent.VK_C; + case Keyboard.KEY_V -> KeyEvent.VK_V; + case Keyboard.KEY_B -> KeyEvent.VK_B; + case Keyboard.KEY_N -> KeyEvent.VK_N; + case Keyboard.KEY_M -> KeyEvent.VK_M; + case Keyboard.KEY_COMMA -> KeyEvent.VK_COMMA; + case Keyboard.KEY_PERIOD -> KeyEvent.VK_PERIOD; + case Keyboard.KEY_SLASH -> KeyEvent.VK_SLASH; + case Keyboard.KEY_MULTIPLY -> KeyEvent.VK_MULTIPLY; + case Keyboard.KEY_LMENU -> KeyEvent.VK_ALT; + case Keyboard.KEY_SPACE -> KeyEvent.VK_SPACE; + case Keyboard.KEY_CAPITAL -> KeyEvent.VK_CAPS_LOCK; + case Keyboard.KEY_F1 -> KeyEvent.VK_F1; + case Keyboard.KEY_F2 -> KeyEvent.VK_F2; + case Keyboard.KEY_F3 -> KeyEvent.VK_F3; + case Keyboard.KEY_F4 -> KeyEvent.VK_F4; + case Keyboard.KEY_F5 -> KeyEvent.VK_F5; + case Keyboard.KEY_F6 -> KeyEvent.VK_F6; + case Keyboard.KEY_F7 -> KeyEvent.VK_F7; + case Keyboard.KEY_F8 -> KeyEvent.VK_F8; + case Keyboard.KEY_F9 -> KeyEvent.VK_F9; + case Keyboard.KEY_F10 -> KeyEvent.VK_F10; + case Keyboard.KEY_NUMLOCK -> KeyEvent.VK_NUM_LOCK; + case Keyboard.KEY_SCROLL -> KeyEvent.VK_SCROLL_LOCK; + case Keyboard.KEY_NUMPAD7 -> KeyEvent.VK_NUMPAD7; + case Keyboard.KEY_NUMPAD8 -> KeyEvent.VK_NUMPAD8; + case Keyboard.KEY_NUMPAD9 -> KeyEvent.VK_NUMPAD9; + case Keyboard.KEY_SUBTRACT -> KeyEvent.VK_SUBTRACT; + case Keyboard.KEY_NUMPAD4 -> KeyEvent.VK_NUMPAD4; + case Keyboard.KEY_NUMPAD5 -> KeyEvent.VK_NUMPAD5; + case Keyboard.KEY_NUMPAD6 -> KeyEvent.VK_NUMPAD6; + case Keyboard.KEY_ADD -> KeyEvent.VK_ADD; + case Keyboard.KEY_NUMPAD1 -> KeyEvent.VK_NUMPAD1; + case Keyboard.KEY_NUMPAD2 -> KeyEvent.VK_NUMPAD2; + case Keyboard.KEY_NUMPAD3 -> KeyEvent.VK_NUMPAD3; + case Keyboard.KEY_NUMPAD0 -> KeyEvent.VK_NUMPAD0; + case Keyboard.KEY_DECIMAL -> KeyEvent.VK_DECIMAL; + case Keyboard.KEY_F11 -> KeyEvent.VK_F11; + case Keyboard.KEY_F12 -> KeyEvent.VK_F12; + case Keyboard.KEY_F13 -> KeyEvent.VK_F13; + case Keyboard.KEY_F14 -> KeyEvent.VK_F14; + case Keyboard.KEY_F15 -> KeyEvent.VK_F15; + case Keyboard.KEY_KANA -> KeyEvent.VK_KANA; + case Keyboard.KEY_CONVERT -> KeyEvent.VK_CONVERT; + case Keyboard.KEY_NOCONVERT -> KeyEvent.VK_NONCONVERT; + case Keyboard.KEY_CIRCUMFLEX -> KeyEvent.VK_CIRCUMFLEX; + case Keyboard.KEY_AT -> KeyEvent.VK_AT; + case Keyboard.KEY_COLON -> KeyEvent.VK_COLON; + case Keyboard.KEY_UNDERLINE -> KeyEvent.VK_UNDERSCORE; + case Keyboard.KEY_KANJI -> KeyEvent.VK_KANJI; + case Keyboard.KEY_STOP -> KeyEvent.VK_STOP; + case Keyboard.KEY_DIVIDE -> KeyEvent.VK_DIVIDE; + case Keyboard.KEY_PAUSE -> KeyEvent.VK_PAUSE; + case Keyboard.KEY_HOME -> KeyEvent.VK_HOME; + case Keyboard.KEY_UP -> KeyEvent.VK_UP; + case Keyboard.KEY_PRIOR -> KeyEvent.VK_PAGE_UP; + case Keyboard.KEY_LEFT -> KeyEvent.VK_LEFT; + case Keyboard.KEY_RIGHT -> KeyEvent.VK_RIGHT; + case Keyboard.KEY_END -> KeyEvent.VK_END; + case Keyboard.KEY_DOWN -> KeyEvent.VK_DOWN; + case Keyboard.KEY_NEXT -> KeyEvent.VK_PAGE_DOWN; + case Keyboard.KEY_INSERT -> KeyEvent.VK_INSERT; + case Keyboard.KEY_DELETE -> KeyEvent.VK_DELETE; + case Keyboard.KEY_LWIN -> KeyEvent.VK_META; + default -> Keyboard.KEY_NONE; + }; + } } diff --git a/src/main/java/org/lwjglx/input/Keyboard.java b/src/main/java/org/lwjglx/input/Keyboard.java index 1f7f986..d8e28d1 100644 --- a/src/main/java/org/lwjglx/input/Keyboard.java +++ b/src/main/java/org/lwjglx/input/Keyboard.java @@ -5,6 +5,8 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; import org.apache.commons.lang3.StringUtils; import org.lwjgl.glfw.GLFW; @@ -160,6 +162,7 @@ public class Keyboard { public static final int keyCount; private static EventQueue queue = new EventQueue(128); + private static BlockingQueue imeCharQueue = new ArrayBlockingQueue<>(128); private enum KeyState { @@ -218,7 +221,7 @@ public class Keyboard { keyMap.put(keyName[i], i); } for (int key = 32; key < 128; key++) { - keyEventChars[KeyCodes.toLwjglKey(key)] = (char) key; + keyEventChars[KeyCodes.glfwToLwjgl(key)] = (char) key; } keyEventChars[KEY_NONE] = '\0'; } @@ -236,7 +239,7 @@ public class Keyboard { } default -> state = KeyState.RELEASE; } - keyEvents[queue.getNextPos()] = KeyCodes.toLwjglKey(key); + keyEvents[queue.getNextPos()] = KeyCodes.glfwToLwjgl(key); keyEventStates[queue.getNextPos()] = state; nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime(); @@ -244,7 +247,7 @@ public class Keyboard { } public static void addKeyEvent(int key, boolean pressed) { - keyEvents[queue.getNextPos()] = KeyCodes.toLwjglKey(key); + keyEvents[queue.getNextPos()] = KeyCodes.glfwToLwjgl(key); keyEventStates[queue.getNextPos()] = pressed ? KeyState.PRESS : KeyState.RELEASE; nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime(); @@ -252,14 +255,18 @@ public class Keyboard { } public static void addCharEvent(int key, char c) { - int index = KeyCodes.toLwjglKey(key); + int index = KeyCodes.glfwToLwjgl(key); keyEventChars[index] = c; } + public static void addIMECharEvent(char c) { + imeCharQueue.offer(c); + } + public static void create() throws LWJGLException {} public static boolean isKeyDown(int key) { - return GLFW.glfwGetKey(Display.getWindow(), KeyCodes.toGlfwKey(key)) == GLFW.GLFW_PRESS; + return GLFW.glfwGetKey(Display.getWindow(), KeyCodes.lwjglToGlfw(key)) == GLFW.GLFW_PRESS; } public static void poll() { @@ -295,6 +302,9 @@ public class Keyboard { } public static char getEventCharacter() { + if (!imeCharQueue.isEmpty() && Display.imeOn) { + return imeCharQueue.remove(); + } final int eventKey = getEventKey(); // On some systems it seems esc and backspace can generate broken chars sometimes, make sure they always work return switch (eventKey) { @@ -305,7 +315,7 @@ public class Keyboard { } public static boolean getEventKeyState() { - return keyEventStates[queue.getCurrentPos()].isPressed; + return keyEventStates[queue.getCurrentPos()].isPressed || (!imeCharQueue.isEmpty() && Display.imeOn); } public static long getEventNanoseconds() { diff --git a/src/main/java/org/lwjglx/opengl/Display.java b/src/main/java/org/lwjglx/opengl/Display.java index eceaea0..305c563 100644 --- a/src/main/java/org/lwjglx/opengl/Display.java +++ b/src/main/java/org/lwjglx/opengl/Display.java @@ -1,9 +1,9 @@ package org.lwjglx.opengl; -import static org.lwjgl.glfw.Callbacks.*; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.system.MemoryUtil.NULL; +import java.awt.event.KeyEvent; import java.nio.ByteBuffer; import java.nio.IntBuffer; @@ -25,6 +25,7 @@ import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL11; import org.lwjglx.BufferUtils; import org.lwjglx.Sys; +import org.lwjglx.input.KeyCodes; import org.lwjglx.input.Keyboard; import org.lwjglx.input.Mouse; @@ -56,6 +57,7 @@ public class Display { private static boolean latestResized = false; private static int latestWidth = 0; private static int latestHeight = 0; + public static boolean imeOn = false; private static ByteBuffer[] savedIcons; static { @@ -153,17 +155,38 @@ public class Display { public void invoke(long window, int key, int scancode, int action, int mods) { if (Config.DEBUG_PRINT_KEY_EVENTS) { Lwjgl3ify.LOG.info( - "[DEBUG-KEY] key window:{} key:{} scancode:{} action:{} mods:{} char:{}", + "[DEBUG-KEY] key window:{} key:{} scancode:{} action:{} mods:{} charname:{} char:{}", window, key, scancode, action, mods, + KeyEvent.getKeyText(KeyCodes.lwjglToAwt(KeyCodes.glfwToLwjgl(key))), (key >= 32 && key < 127) ? ((char) key) : '?'); } latestEventKey = key; - - Keyboard.addGlfwKeyEvent(window, key, scancode, action, mods); + if (Config.IME_ENABLED) { + if (Config.IME_F12_TOGGLE && key == GLFW_KEY_F12 && action == 0) { + imeOn = !imeOn; + } + if (Config.IME_SYS_TOGGLE + && (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL) + || Keyboard.isKeyDown(Keyboard.KEY_LWIN))) { + if (key == GLFW_KEY_SPACE || key == GLFW_KEY_LEFT_SHIFT || key == GLFW_KEY_RIGHT_SHIFT) { + imeOn = !imeOn; + return; + } + } + if (imeOn) { + if (key > 256) { + Keyboard.addGlfwKeyEvent(window, key, scancode, action, mods); + } + } else { + Keyboard.addGlfwKeyEvent(window, key, scancode, action, mods); + } + } else { + Keyboard.addGlfwKeyEvent(window, key, scancode, action, mods); + } // Ctrl should generate ASCII modifier keys (0x01-0x1F), glfw does not give use char events for this if ((mods & GLFW_MOD_CONTROL) != 0 && key >= GLFW_KEY_A && key <= GLFW_KEY_Z) { int codepoint = key & 0x1F; @@ -183,7 +206,14 @@ public class Display { codepoint, (char) codepoint); } - Keyboard.addCharEvent(latestEventKey, (char) codepoint); + if (imeOn) { + Keyboard.addKeyEvent(GLFW_KEY_UNKNOWN, true); + Keyboard.addIMECharEvent((char) codepoint); + Keyboard.addKeyEvent(GLFW_KEY_UNKNOWN, false); + + } else { + Keyboard.addCharEvent(latestEventKey, (char) codepoint); + } } };