From 68be82a4f92f47300b9960cf9cf65040c96f17ed Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 23 Feb 2025 12:12:29 +0100 Subject: Macro keys (#878) Add "macro" keys that behave as if a sequence of keys is typed. Macro can be added to custom layouts or through the "Add keys to the keyboard option". The syntax is: symbol:key1,key2,.. The symbol cannot contain a : character. 'key1', 'key2', etc.. are: - 'String with \' escaping' The key will generate the specified string. - keyevent:123 The key will send a keyevent. - The name of any special key --- srcs/juloo.keyboard2/KeyValue.java | 64 ++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 13 deletions(-) (limited to 'srcs/juloo.keyboard2/KeyValue.java') diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 60d49e4..b2ef5db 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -96,6 +96,7 @@ public final class KeyValue implements Comparable String, // [_payload] is also the string to output, value is unused. Slider, // [_payload] is a [KeyValue.Slider], value is slider repeatition. StringWithSymbol, // [_payload] is a [KeyValue.StringWithSymbol], value is unused. + Macro, // [_payload] is a [KeyValue.Macro], value is unused. } private static final int FLAGS_OFFSET = 20; @@ -105,7 +106,8 @@ public final class KeyValue implements Comparable public static final int FLAG_LATCH = (1 << FLAGS_OFFSET << 0); // Key can be locked by typing twice when enabled in settings public static final int FLAG_DOUBLE_TAP_LOCK = (1 << FLAGS_OFFSET << 1); - // Special keys are not repeated and don't clear latched modifiers. + // Special keys are not repeated. + // Special latchable keys don't clear latched modifiers. public static final int FLAG_SPECIAL = (1 << FLAGS_OFFSET << 2); // Whether the symbol should be greyed out. For example, keys that are not // part of the pending compose sequence. @@ -229,6 +231,12 @@ public final class KeyValue implements Comparable return ((StringWithSymbol)_payload).str; } + /** Defined only when [getKind() == Kind.Macro]. */ + public KeyValue[] getMacro() + { + return ((Macro)_payload).keys; + } + /* Update the char and the symbol. */ public KeyValue withChar(char c) { @@ -460,31 +468,35 @@ public final class KeyValue implements Comparable Kind.StringWithSymbol, 0, flags); } + public static KeyValue makeMacro(String symbol, KeyValue[] keys, int flags) + { + return new KeyValue(new Macro(keys, symbol), Kind.Macro, 0, flags); + } + /** Make a modifier key for passing to [KeyModifier]. */ public static KeyValue makeInternalModifier(Modifier mod) { return new KeyValue("", Kind.Modifier, mod.ordinal(), 0); } - public static KeyValue parseKeyDefinition(String str) + /** Return a key by its name. If the given name doesn't correspond to any + special key, it is parsed with [KeyValueParser]. */ + public static KeyValue getKeyByName(String name) { - if (str.length() < 2 || str.charAt(0) != ':') - return makeStringKey(str); + KeyValue k = getSpecialKeyByName(name); + if (k != null) + return k; try { - return KeyValueParser.parse(str); + return KeyValueParser.parse(name); } catch (KeyValueParser.ParseError _e) { - return makeStringKey(str); + return makeStringKey(name); } } - /** - * Return a key by its name. If the given name doesn't correspond to a key - * defined in this function, it is passed to [parseStringKey] as a fallback. - */ - public static KeyValue getKeyByName(String name) + public static KeyValue getSpecialKeyByName(String name) { switch (name) { @@ -735,8 +747,7 @@ public final class KeyValue implements Comparable case "௲": case "௳": return makeStringKey(name, FLAG_SMALLER_FONT); - /* The key is not one of the special ones. */ - default: return parseKeyDefinition(name); + default: return null; } } @@ -787,4 +798,31 @@ public final class KeyValue implements Comparable @Override public String toString() { return symbol; } }; + + public static final class Macro implements Comparable + { + public final KeyValue[] keys; + private final String _symbol; + + public Macro(KeyValue[] keys_, String sym_) + { + keys = keys_; + _symbol = sym_; + } + + public String toString() { return _symbol; } + + @Override + public int compareTo(Macro snd) + { + int d = keys.length - snd.keys.length; + if (d != 0) return d; + for (int i = 0; i < keys.length; i++) + { + d = keys[i].compareTo(snd.keys[i]); + if (d != 0) return d; + } + return _symbol.compareTo(snd._symbol); + } + }; } -- cgit v1.2.3