From c1a816d3d41218b5b4bdaa80db01898e3a25ded2 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 5 Jun 2022 17:44:00 +0200 Subject: [PATCH] Refactor: Associate key events in KeyModifier Two advantages: - No need to distinguish modifiers in KeyEventHandler. The KeyValue is enough to decide what action to do. - Keys are never a Char and Event at the same time, fields can be merged. --- srcs/juloo.keyboard2/KeyEventHandler.java | 21 ------- srcs/juloo.keyboard2/KeyModifier.java | 69 +++++++++++++++++++++ srcs/juloo.keyboard2/KeyValue.java | 74 ++++------------------- 3 files changed, 81 insertions(+), 83 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index 90f4a1a..8be8bf7 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -19,10 +19,6 @@ class KeyEventHandler implements Config.IKeyEventHandler switch (key.getKind()) { case Char: - // Send an event if some modifiers are active. - event = key.getCharEvent(); - if (shouldSendEvents(mods) && event != KeyValue.EVENT_NONE) - handleKeyUpWithModifier(event, mods); _recv.commitChar(key.getChar()); break; case String: @@ -102,23 +98,6 @@ class KeyEventHandler implements Config.IKeyEventHandler metaState = sendMetaKeyForModifier(mods.get(i), metaState, false); } - /** Whether to send up and down events (true) or commit the text (false). */ - private boolean shouldSendEvents(Pointers.Modifiers mods) - { - // Check for modifiers - for (int i = 0; i < mods.size(); i++) - { - switch (mods.get(i)) - { - case KeyValue.MOD_CTRL: - case KeyValue.MOD_ALT: - case KeyValue.MOD_META: return true; - default: break; - } - } - return false; - } - public static interface IReceiver { public void switchToNextInputMethod(); diff --git a/srcs/juloo.keyboard2/KeyModifier.java b/srcs/juloo.keyboard2/KeyModifier.java index a85f5f7..7d01560 100644 --- a/srcs/juloo.keyboard2/KeyModifier.java +++ b/srcs/juloo.keyboard2/KeyModifier.java @@ -37,6 +37,9 @@ class KeyModifier { switch (mod) { + case KeyValue.MOD_CTRL: + case KeyValue.MOD_ALT: + case KeyValue.MOD_META: return turn_into_keyevent(k); case KeyValue.MOD_FN: return apply_fn(k); case KeyValue.MOD_SHIFT: return apply_shift(k); case KeyValue.MOD_GRAVE: return apply_dead_char(k, '\u02CB'); @@ -186,6 +189,72 @@ class KeyModifier return KeyValue.getKeyByName(name); } + private static KeyValue turn_into_keyevent(KeyValue k) + { + if (k.getKind() != KeyValue.Kind.Char) + return k; + int e; + switch (k.getChar()) + { + case 'a': e = KeyEvent.KEYCODE_A; break; + case 'b': e = KeyEvent.KEYCODE_B; break; + case 'c': e = KeyEvent.KEYCODE_C; break; + case 'd': e = KeyEvent.KEYCODE_D; break; + case 'e': e = KeyEvent.KEYCODE_E; break; + case 'f': e = KeyEvent.KEYCODE_F; break; + case 'g': e = KeyEvent.KEYCODE_G; break; + case 'h': e = KeyEvent.KEYCODE_H; break; + case 'i': e = KeyEvent.KEYCODE_I; break; + case 'j': e = KeyEvent.KEYCODE_J; break; + case 'k': e = KeyEvent.KEYCODE_K; break; + case 'l': e = KeyEvent.KEYCODE_L; break; + case 'm': e = KeyEvent.KEYCODE_M; break; + case 'n': e = KeyEvent.KEYCODE_N; break; + case 'o': e = KeyEvent.KEYCODE_O; break; + case 'p': e = KeyEvent.KEYCODE_P; break; + case 'q': e = KeyEvent.KEYCODE_Q; break; + case 'r': e = KeyEvent.KEYCODE_R; break; + case 's': e = KeyEvent.KEYCODE_S; break; + case 't': e = KeyEvent.KEYCODE_T; break; + case 'u': e = KeyEvent.KEYCODE_U; break; + case 'v': e = KeyEvent.KEYCODE_V; break; + case 'w': e = KeyEvent.KEYCODE_W; break; + case 'x': e = KeyEvent.KEYCODE_X; break; + case 'y': e = KeyEvent.KEYCODE_Y; break; + case 'z': e = KeyEvent.KEYCODE_Z; break; + case '0': e = KeyEvent.KEYCODE_0; break; + case '1': e = KeyEvent.KEYCODE_1; break; + case '2': e = KeyEvent.KEYCODE_2; break; + case '3': e = KeyEvent.KEYCODE_3; break; + case '4': e = KeyEvent.KEYCODE_4; break; + case '5': e = KeyEvent.KEYCODE_5; break; + case '6': e = KeyEvent.KEYCODE_6; break; + case '7': e = KeyEvent.KEYCODE_7; break; + case '8': e = KeyEvent.KEYCODE_8; break; + case '9': e = KeyEvent.KEYCODE_9; break; + case '`': e = KeyEvent.KEYCODE_GRAVE; break; + case '-': e = KeyEvent.KEYCODE_MINUS; break; + case '=': e = KeyEvent.KEYCODE_EQUALS; break; + case '[': e = KeyEvent.KEYCODE_LEFT_BRACKET; break; + case ']': e = KeyEvent.KEYCODE_RIGHT_BRACKET; break; + case '\\': e = KeyEvent.KEYCODE_BACKSLASH; break; + case ';': e = KeyEvent.KEYCODE_SEMICOLON; break; + case '\'': e = KeyEvent.KEYCODE_APOSTROPHE; break; + case '/': e = KeyEvent.KEYCODE_SLASH; break; + case '@': e = KeyEvent.KEYCODE_AT; break; + case '+': e = KeyEvent.KEYCODE_PLUS; break; + case ',': e = KeyEvent.KEYCODE_COMMA; break; + case '.': e = KeyEvent.KEYCODE_PERIOD; break; + case '*': e = KeyEvent.KEYCODE_STAR; break; + case '#': e = KeyEvent.KEYCODE_POUND; break; + case '(': e = KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN; break; + case ')': e = KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN; break; + case ' ': e = KeyEvent.KEYCODE_SPACE; break; + default: return k; + } + return k.withEvent(e); + } + /** Remove placeholder keys that haven't been modified into something. */ private static KeyValue remove_placeholders(KeyValue k) { diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index f19e49b..24dd84a 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -93,10 +93,10 @@ class KeyValue { if ((_flags & FLAG_MODIFIER) != 0) return Kind.Modifier; - if (_char != CHAR_NONE) - return Kind.Char; if (_code != EVENT_NONE) return Kind.Event; + if (_char != CHAR_NONE) + return Kind.Char; return Kind.String; } @@ -162,6 +162,11 @@ class KeyValue return new KeyValue(n, s, _char, _code, _flags); } + public KeyValue withEvent(int event) + { + return new KeyValue(name, _symbol, _char, event, (_flags & ~FLAG_MODIFIER)); + } + public KeyValue withFlags(int f) { return new KeyValue(name, _symbol, _char, _code, f); @@ -208,10 +213,9 @@ class KeyValue keys.put(name, new KeyValue(name, symbol, c, event, flags)); } - private static void addCharKey(char c, int event) + private static void addCharKey(String name, String symbol, char c, int flags) { - String name = String.valueOf(c); - addKey(name, name, c, event, 0); + addKey(name, symbol, c, EVENT_NONE, flags); } private static void addModifierKey(String name, String symbol, int code, int extra_flags) @@ -260,60 +264,6 @@ class KeyValue addModifierKey("fn", "Fn", MOD_FN, FLAG_SMALLER_FONT); addModifierKey("meta", "Meta", MOD_META, FLAG_SMALLER_FONT); - addCharKey('a', KeyEvent.KEYCODE_A); - addCharKey('b', KeyEvent.KEYCODE_B); - addCharKey('c', KeyEvent.KEYCODE_C); - addCharKey('d', KeyEvent.KEYCODE_D); - addCharKey('e', KeyEvent.KEYCODE_E); - addCharKey('f', KeyEvent.KEYCODE_F); - addCharKey('g', KeyEvent.KEYCODE_G); - addCharKey('h', KeyEvent.KEYCODE_H); - addCharKey('i', KeyEvent.KEYCODE_I); - addCharKey('j', KeyEvent.KEYCODE_J); - addCharKey('k', KeyEvent.KEYCODE_K); - addCharKey('l', KeyEvent.KEYCODE_L); - addCharKey('m', KeyEvent.KEYCODE_M); - addCharKey('n', KeyEvent.KEYCODE_N); - addCharKey('o', KeyEvent.KEYCODE_O); - addCharKey('p', KeyEvent.KEYCODE_P); - addCharKey('q', KeyEvent.KEYCODE_Q); - addCharKey('r', KeyEvent.KEYCODE_R); - addCharKey('s', KeyEvent.KEYCODE_S); - addCharKey('t', KeyEvent.KEYCODE_T); - addCharKey('u', KeyEvent.KEYCODE_U); - addCharKey('v', KeyEvent.KEYCODE_V); - addCharKey('w', KeyEvent.KEYCODE_W); - addCharKey('x', KeyEvent.KEYCODE_X); - addCharKey('y', KeyEvent.KEYCODE_Y); - addCharKey('z', KeyEvent.KEYCODE_Z); - addCharKey('0', KeyEvent.KEYCODE_0); - addCharKey('1', KeyEvent.KEYCODE_1); - addCharKey('2', KeyEvent.KEYCODE_2); - addCharKey('3', KeyEvent.KEYCODE_3); - addCharKey('4', KeyEvent.KEYCODE_4); - addCharKey('5', KeyEvent.KEYCODE_5); - addCharKey('6', KeyEvent.KEYCODE_6); - addCharKey('7', KeyEvent.KEYCODE_7); - addCharKey('8', KeyEvent.KEYCODE_8); - addCharKey('9', KeyEvent.KEYCODE_9); - addCharKey('`', KeyEvent.KEYCODE_GRAVE); - addCharKey('-', KeyEvent.KEYCODE_MINUS); - addCharKey('=', KeyEvent.KEYCODE_EQUALS); - addCharKey('[', KeyEvent.KEYCODE_LEFT_BRACKET); - addCharKey(']', KeyEvent.KEYCODE_RIGHT_BRACKET); - addCharKey('\\', KeyEvent.KEYCODE_BACKSLASH); - addCharKey(';', KeyEvent.KEYCODE_SEMICOLON); - addCharKey('\'', KeyEvent.KEYCODE_APOSTROPHE); - addCharKey('/', KeyEvent.KEYCODE_SLASH); - addCharKey('@', KeyEvent.KEYCODE_AT); - addCharKey('+', KeyEvent.KEYCODE_PLUS); - addCharKey(',', KeyEvent.KEYCODE_COMMA); - addCharKey('.', KeyEvent.KEYCODE_PERIOD); - addCharKey('*', KeyEvent.KEYCODE_STAR); - addCharKey('#', KeyEvent.KEYCODE_POUND); - addCharKey('(', KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN); - addCharKey(')', KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN); - addSpecialKey("config", "\u0004", EVENT_CONFIG, FLAG_KEY_FONT | FLAG_SMALLER_FONT); addSpecialKey("switch_text", "ABC", EVENT_SWITCH_TEXT, FLAG_SMALLER_FONT); addSpecialKey("switch_numeric", "123+", EVENT_SWITCH_NUMERIC, FLAG_SMALLER_FONT); @@ -350,8 +300,8 @@ class KeyValue addEventKey("f12", "F12", KeyEvent.KEYCODE_F12, FLAG_SMALLER_FONT); addEventKey("tab", "\u000F", KeyEvent.KEYCODE_TAB, FLAG_KEY_FONT | FLAG_SMALLER_FONT); - addKey("\\t", "\\t", '\t', EVENT_NONE, 0); // Send the tab character - addKey("space", "\r", ' ', KeyEvent.KEYCODE_SPACE, FLAG_KEY_FONT); - addKey("nbsp", "\u237d", '\u00a0', EVENT_NONE, FLAG_KEY_FONT | FLAG_SMALLER_FONT); + addCharKey("\\t", "\\t", '\t', 0); // Send the tab character + addCharKey("space", "\r", ' ', FLAG_KEY_FONT); + addCharKey("nbsp", "\u237d", '\u00a0', FLAG_KEY_FONT | FLAG_SMALLER_FONT); } }