Improve the numeric keyboard

This commit is contained in:
Jules Aguillon 2021-05-08 02:00:47 +02:00
parent 3fbc35135e
commit 8fb89c5c71
3 changed files with 104 additions and 46 deletions

View File

@ -3,11 +3,11 @@
<row>
<key width="0.75" key0="esc" key2="~" key4="!" />
<key width="0.75" key0="(" key2="[" key4="{" />
<key key0="7" key2="&lt;" key3="home" />
<key key0="8" />
<key key0="9" key2="page_up" key3="&gt;" />
<key width="0.75" key0="*" />
<key width="0.75" key0="/" key1="%" key3=":" />
<key key0="7" key3="&lt;" key4="&gt;" />
<key key0="8" key2="∞" />
<key key0="9" key2="π" />
<key width="0.75" key0="*" key1="√" key2="×" />
<key width="0.75" key0="/" key1="%" key3="÷" />
</row>
<row>
<key width="0.75" key0="tab" key1=";" key2="|" key4="\\" />
@ -15,14 +15,14 @@
<key key0="4" />
<key key0="5" key1="up" key2="right" key3="left" key4="down" />
<key key0="6" />
<key width="0.75" key0="+" key2="$" />
<key width="0.75" key0="+" key1="Σ" key2="$" />
<key width="0.75" key0="-" key2="^" />
</row>
<row>
<key shift="0.35" width="1.15" key0="shift" key4="alt" />
<key key0="1" key3="end" />
<key shift="0.35" width="1.15" key0="shift" key2="fn" key4="alt" />
<key key0="1" key1="superscript" key3="subscript" />
<key key0="2" />
<key key0="3" key4="page_down" />
<key key0="3" />
<key width="1.15" key0="backspace" key2="delete" />
</row>
<row height="0.95">
@ -30,6 +30,6 @@
<key width="1.5" key0="0" />
<key width="0.75" key0="." key2="," />
<key width="0.75" key0="space" key1="&quot;" key2="'" key4="_" />
<key width="1.5" key0="enter" key3="=" />
<key width="1.5" key0="enter" key2="±" key3="=" />
</row>
</keyboard>

View File

@ -20,31 +20,43 @@ class KeyModifier
KeyValue r = ks.get(flags);
if (r != null) // Found in cache
return r;
if ((r = handleChar(k, flags)) != null) ;
else if ((r = handleFn(k, flags)) != null) ;
else r = k;
r = k;
r = handleFn(r, flags);
r = handleShift(r, flags);
r = handleAccents(r, flags);
ks.put(flags, r);
return r;
}
/* Returns [null] if had no effect. */
private static KeyValue handleChar(KeyValue k, int flags)
private static KeyValue handleAccents(KeyValue k, int flags)
{
char c = k.char_;
if (c == KeyValue.CHAR_NONE)
return null;
if ((flags & KeyValue.FLAG_SHIFT) != 0) // Shift
c = Character.toUpperCase(c);
if ((flags & KeyValue.FLAGS_ACCENTS) != 0) // Accents, after shift is applied
c = handleAccentChar(c, flags);
if (k.char_ == KeyValue.CHAR_NONE || (flags & KeyValue.FLAGS_ACCENTS) == 0)
return k;
char c = handleAccentChar(k.char_, flags);
if (c == 0 || c == k.char_)
return null;
return k.withCharAndSymbol(String.valueOf(c), c);
return k;
return k.withCharAndSymbol(c);
}
private static KeyValue handleShift(KeyValue k, int flags)
{
if ((flags & KeyValue.FLAG_SHIFT) == 0)
return k;
char c = k.char_;
if (k.char_ != KeyValue.CHAR_NONE)
c = Character.toUpperCase(c);
if (c == k.char_) // More rules if toUpperCase() did nothing
switch (k.symbol)
{
case "": c = '⇒'; break;
case "": c = '⇐'; break;
default: return k;
}
return k.withCharAndSymbol(c);
}
private static char handleAccentChar(char c, int flags)
{
char accent;
switch ((flags & KeyValue.FLAGS_ACCENTS))
{
case KeyValue.FLAG_ACCENT1:
@ -58,6 +70,32 @@ class KeyModifier
default: return (char)KeyCharacterMap.getDeadChar('\u00B4', c);
}
case KeyValue.FLAG_ACCENT3:
switch (c)
{
case '*': return '°';
default: return (char)KeyCharacterMap.getDeadChar('\u02C6', c);
}
case KeyValue.FLAG_ACCENT4:
return (char)KeyCharacterMap.getDeadChar('\u02DC', c);
case KeyValue.FLAG_ACCENT5:
switch (c)
{
case 'u': return 'µ';
case '"': return '„';
case '-': return '¬';
case 'a': return 'æ';
case 'o': return 'œ';
default: return (char)KeyCharacterMap.getDeadChar('\u00B8', c);
}
case KeyValue.FLAG_ACCENT6:
switch (c)
{
case '-': return '÷';
case '?': return '¿';
case '!': return '¡';
default: return (char)KeyCharacterMap.getDeadChar('\u00A8', c);
}
case KeyValue.FLAG_ACCENT_SUPERSCRIPT:
switch (c)
{
case '1': return '¹';
@ -70,12 +108,16 @@ class KeyModifier
case '8': return '⁸';
case '9': return '⁹';
case '0': return '⁰';
case '*': return '°';
default: return (char)KeyCharacterMap.getDeadChar('\u02C6', c);
case 'i': return 'ⁱ';
case '+': return '⁺';
case '-': return '⁻';
case '=': return '⁼';
case '(': return '⁽';
case ')': return '⁾';
case 'n': return 'ⁿ';
default: return c;
}
case KeyValue.FLAG_ACCENT4:
return (char)KeyCharacterMap.getDeadChar('\u02DC', c);
case KeyValue.FLAG_ACCENT5:
case KeyValue.FLAG_ACCENT_SUBSCRIPT:
switch (c)
{
case '1': return '₁';
@ -88,18 +130,16 @@ class KeyModifier
case '8': return '₈';
case '9': return '₉';
case '0': return '₀';
case 'u': return 'µ';
case '"': return '„';
case '-': return '¬';
default: return (char)KeyCharacterMap.getDeadChar('\u00B8', c);
}
case KeyValue.FLAG_ACCENT6:
switch (c)
{
case '-': return '÷';
case '?': return '¿';
case '!': return '¡';
default: return (char)KeyCharacterMap.getDeadChar('\u00A8', c);
case '+': return '₊';
case '-': return '₋';
case '=': return '₌';
case '(': return '₍';
case ')': return '₎';
case 'e': return 'ₑ';
case 'a': return 'ₐ';
case 'x': return 'ₓ';
case 'o': return 'ₒ';
default: return c;
}
default: return c; // Can't happen
}
@ -108,7 +148,7 @@ class KeyModifier
private static KeyValue handleFn(KeyValue k, int flags)
{
if ((flags & KeyValue.FLAG_FN) == 0)
return null;
return k;
String name;
switch (k.name)
{
@ -126,7 +166,9 @@ class KeyModifier
case "down": name = "page_down"; break;
case "left": name = "home"; break;
case "right": name = "end"; break;
default: return null;
case ">": name = ""; break;
case "<": name = ""; break;
default: return k;
}
return KeyValue.getKeyByName(name);
}

View File

@ -39,9 +39,12 @@ class KeyValue
public static final int FLAG_ACCENT4 = (1 << 19); // Tilde
public static final int FLAG_ACCENT5 = (1 << 20); // Cédille
public static final int FLAG_ACCENT6 = (1 << 21); // Tréma
public static final int FLAG_ACCENT_SUPERSCRIPT = (1 << 22);
public static final int FLAG_ACCENT_SUBSCRIPT = (1 << 23);
public static final int FLAGS_ACCENTS = FLAG_ACCENT1 | FLAG_ACCENT2 |
FLAG_ACCENT3 | FLAG_ACCENT4 | FLAG_ACCENT5 | FLAG_ACCENT6;
FLAG_ACCENT3 | FLAG_ACCENT4 | FLAG_ACCENT5 | FLAG_ACCENT6 |
FLAG_ACCENT_SUPERSCRIPT | FLAG_ACCENT_SUBSCRIPT;
public final String name;
public final String symbol;
@ -49,6 +52,12 @@ class KeyValue
public final int eventCode;
public final int flags;
/* Update the char and the symbol. */
public KeyValue withCharAndSymbol(char c)
{
return withCharAndSymbol(String.valueOf(c), c);
}
public KeyValue withCharAndSymbol(String s, char c)
{
return new KeyValue(name, s, c, eventCode, flags);
@ -67,7 +76,12 @@ class KeyValue
public static KeyValue getKeyByName(String name)
{
return (KeyValue.keys.get(name));
if (name == null)
return null;
KeyValue kv = KeyValue.keys.get(name);
if (kv != null)
return kv;
return new KeyValue(name, name, CHAR_NONE, EVENT_NONE, 0);
}
private static void addKey(String name, String symbol, char c, int event, int flags)
@ -117,6 +131,8 @@ class KeyValue
addModifierKey("accent_tilde", "◌̃", FLAG_ACCENT4);
addModifierKey("accent_cedille", "◌̧", FLAG_ACCENT5);
addModifierKey("accent_trema", "◌̈", FLAG_ACCENT6);
addModifierKey("superscript", "◌͆", FLAG_ACCENT_SUPERSCRIPT);
addModifierKey("subscript", "◌̺", FLAG_ACCENT_SUBSCRIPT);
addModifierKey("fn", "Fn", FLAG_FN);
addCharKey('a', KeyEvent.KEYCODE_A);