refactor: Apply the modmap in KeyModifier

This makes possible to apply other modifiers to mapped keys and ensures
that the modmap really override the shift key behavior.
This commit is contained in:
Jules Aguillon 2024-04-01 22:57:53 +02:00
parent f17afba7be
commit 3f6b6fd232
2 changed files with 12 additions and 9 deletions

View File

@ -10,6 +10,11 @@ public final class KeyModifier
private static HashMap<KeyValue, HashMap<Pointers.Modifiers, KeyValue>> _cache = private static HashMap<KeyValue, HashMap<Pointers.Modifiers, KeyValue>> _cache =
new HashMap<KeyValue, HashMap<Pointers.Modifiers, KeyValue>>(); new HashMap<KeyValue, HashMap<Pointers.Modifiers, KeyValue>>();
/** The optional modmap takes priority over modifiers usual behaviors. Set to
[null] to disable. */
private static KeyboardData.Modmap _modmap = null;
public static void set_modmap(KeyboardData.Modmap mm) { _modmap = mm; }
/** Modify a key according to modifiers. */ /** Modify a key according to modifiers. */
public static KeyValue modify(KeyValue k, Pointers.Modifiers mods) public static KeyValue modify(KeyValue k, Pointers.Modifiers mods)
{ {
@ -130,6 +135,12 @@ public final class KeyModifier
private static KeyValue apply_shift(KeyValue k) private static KeyValue apply_shift(KeyValue k)
{ {
if (_modmap != null)
{
KeyValue mapped = _modmap.shift.get(k);
if (mapped != null)
return mapped;
}
switch (k.getKind()) switch (k.getKind())
{ {
case Char: case Char:

View File

@ -104,6 +104,7 @@ public class Keyboard2View extends View
} }
_compose_kv = KeyValue.getKeyByName("compose"); _compose_kv = KeyValue.getKeyByName("compose");
_compose_key = _keyboard.findKeyWithValue(_compose_kv); _compose_key = _keyboard.findKeyWithValue(_compose_kv);
KeyModifier.set_modmap(_keyboard.modmap);
reset(); reset();
} }
@ -137,15 +138,6 @@ public class Keyboard2View extends View
public KeyValue modifyKey(KeyValue k, Pointers.Modifiers mods) public KeyValue modifyKey(KeyValue k, Pointers.Modifiers mods)
{ {
if (_keyboard.modmap != null)
{
if (mods.has(KeyValue.Modifier.SHIFT))
{
KeyValue km = _keyboard.modmap.shift.get(k);
if (km != null)
return km;
}
}
return KeyModifier.modify(k, mods); return KeyModifier.modify(k, mods);
} }