mirror of
https://github.com/GTNewHorizons/lwjgl3ify.git
synced 2026-11-19 19:59:44 +00:00
Add multibyte char event handling by duplicating key events when multiple chars happen after a single keypress (#40)
This commit is contained in:
1 parent
bc0bf578b5
commit
4d73711ded
3 files changed
+44
-9
No files matched your search
@@ -49,6 +49,7 @@ public class Config {
|
||||
public static boolean DEBUG_PRINT_KEY_EVENTS = false;
|
||||
public static boolean DEBUG_PRINT_MOUSE_EVENTS = false;
|
||||
|
||||
public static boolean MBE_ENABLED = true;
|
||||
public static boolean IME_ENABLED = false;
|
||||
public static boolean IME_F12_TOGGLE = false;
|
||||
public static boolean IME_SYS_TOGGLE = false;
|
||||
@@ -136,6 +137,8 @@ public class Config {
|
||||
DEBUG_PRINT_MOUSE_EVENTS,
|
||||
"Print mouse-related events to the log");
|
||||
|
||||
MBE_ENABLED = config
|
||||
.getBoolean("handleMultibyteInput", CATEGORY_CORE, MBE_ENABLED, "Enables multibyte character input.");
|
||||
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.");
|
||||
|
||||
@@ -6,7 +6,7 @@ package org.lwjglx.input;
|
||||
*/
|
||||
class EventQueue {
|
||||
|
||||
private int maxEvents = 32;
|
||||
private final int maxEvents;
|
||||
private int eventCount = 0;
|
||||
private int readEventPos = 0;
|
||||
private int writeEventPos = 1;
|
||||
@@ -42,7 +42,7 @@ class EventQueue {
|
||||
|
||||
/**
|
||||
* Increment the event queue
|
||||
*
|
||||
*
|
||||
* @return - true if there is an event available
|
||||
*/
|
||||
synchronized boolean next() {
|
||||
@@ -67,6 +67,10 @@ class EventQueue {
|
||||
return readEventPos;
|
||||
}
|
||||
|
||||
int getLastWrittenPos() {
|
||||
return (writeEventPos + maxEvents - 1) % maxEvents;
|
||||
}
|
||||
|
||||
int getNextPos() {
|
||||
return writeEventPos;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import java.util.Map;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import me.eigenraven.lwjgl3ify.core.Config;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjglx.LWJGLException;
|
||||
@@ -180,6 +182,7 @@ public class Keyboard {
|
||||
private static boolean doRepeatEvents = true;
|
||||
|
||||
private static int[] keyEvents = new int[queue.getMaxEvents()];
|
||||
private static char[] keySpecificChars = new char[queue.getMaxEvents()];
|
||||
private static KeyState[] keyEventStates = new KeyState[queue.getMaxEvents()];
|
||||
|
||||
static {
|
||||
@@ -239,24 +242,48 @@ public class Keyboard {
|
||||
}
|
||||
default -> state = KeyState.RELEASE;
|
||||
}
|
||||
keyEvents[queue.getNextPos()] = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventStates[queue.getNextPos()] = state;
|
||||
nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime();
|
||||
final int nextPos = queue.getNextPos();
|
||||
keyEvents[nextPos] = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventStates[nextPos] = state;
|
||||
nanoTimeEvents[nextPos] = Sys.getNanoTime();
|
||||
keySpecificChars[nextPos] = '\0';
|
||||
|
||||
queue.add();
|
||||
}
|
||||
|
||||
public static void addKeyEvent(int key, boolean pressed) {
|
||||
keyEvents[queue.getNextPos()] = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventStates[queue.getNextPos()] = pressed ? KeyState.PRESS : KeyState.RELEASE;
|
||||
nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime();
|
||||
final int nextPos = queue.getNextPos();
|
||||
keyEvents[nextPos] = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventStates[nextPos] = pressed ? KeyState.PRESS : KeyState.RELEASE;
|
||||
nanoTimeEvents[nextPos] = Sys.getNanoTime();
|
||||
keySpecificChars[nextPos] = '\0';
|
||||
|
||||
queue.add();
|
||||
}
|
||||
|
||||
private static void duplicateKeyEvent() {
|
||||
final int nextPos = queue.getNextPos();
|
||||
final int prevPos = queue.getLastWrittenPos();
|
||||
keyEvents[nextPos] = keyEvents[prevPos];
|
||||
keyEventStates[nextPos] = keyEventStates[prevPos];
|
||||
nanoTimeEvents[nextPos] = nanoTimeEvents[prevPos];
|
||||
keySpecificChars[nextPos] = keySpecificChars[prevPos];
|
||||
queue.add();
|
||||
}
|
||||
|
||||
public static void addCharEvent(int key, char c) {
|
||||
final int nextPos = queue.getNextPos();
|
||||
final int prevPos = queue.getLastWrittenPos();
|
||||
int index = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventChars[index] = c;
|
||||
if (Config.MBE_ENABLED) {
|
||||
if (keySpecificChars[prevPos] == '\0') {
|
||||
keySpecificChars[prevPos] = c;
|
||||
} else {
|
||||
duplicateKeyEvent();
|
||||
keySpecificChars[nextPos] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void addIMECharEvent(char c) {
|
||||
@@ -306,11 +333,12 @@ public class Keyboard {
|
||||
return imeCharQueue.remove();
|
||||
}
|
||||
final int eventKey = getEventKey();
|
||||
final char eventSpecificChar = keySpecificChars[queue.getCurrentPos()];
|
||||
// On some systems it seems esc and backspace can generate broken chars sometimes, make sure they always work
|
||||
return switch (eventKey) {
|
||||
case KEY_ESCAPE -> '\0';
|
||||
case KEY_BACK -> '\b';
|
||||
default -> keyEventChars[eventKey];
|
||||
default -> (eventSpecificChar != '\0') ? eventSpecificChar : keyEventChars[eventKey];
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user