abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/KeyValue.java
blob: 1f6d0a8d6dbc9f8391b6b321081e0d4160e92709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package juloo.keyboard2;

import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import java.util.HashMap;

class KeyValue
{
	public static final int		EVENT_NONE = -1;
	public static final int		EVENT_CONFIG = -2;
	public static final int		EVENT_SWITCH_TEXT = -3;
	public static final int		EVENT_SWITCH_NUMERIC = -4;
	public static final int		EVENT_SWITCH_EMOJI = -5;
	public static final int		EVENT_SWITCH_BACK_EMOJI = -6;
	public static final int		EVENT_CHANGE_METHOD = -7;
	public static final char	CHAR_NONE = '\0';

	public static final int		FLAG_KEEP_ON = 1;
	public static final int		FLAG_LOCK = (1 << 1);
	public static final int		FLAG_CTRL = (1 << 2);
	public static final int		FLAG_SHIFT = (1 << 3);
	public static final int		FLAG_ALT = (1 << 4);
	public static final int		FLAG_NOREPEAT = (1 << 5);
	public static final int		FLAG_NOCHAR = (1 << 6);
	public static final int		FLAG_LOCKED = (1 << 8);

	public static final int		FLAG_KEY_FONT = (1 << 12);

	public static final int		FLAG_ACCENT1 = (1 << 16);
	public static final int		FLAG_ACCENT2 = (1 << 17);
	public static final int		FLAG_ACCENT3 = (1 << 18);
	public static final int		FLAG_ACCENT4 = (1 << 19);
	public static final int		FLAG_ACCENT5 = (1 << 20);
	public static final int		FLAG_ACCENT6 = (1 << 21);

  public static final int   FLAGS_ACCENTS = FLAG_ACCENT1 | FLAG_ACCENT2 |
    FLAG_ACCENT3 | FLAG_ACCENT4 | FLAG_ACCENT5 | FLAG_ACCENT6;

  private final String _name;
  private final String _symbol;
  private final char _char;
  private final int _eventCode;
  private final int _flags;

	private int			_cacheFlags;
	private String		_cacheSymbol;

	public String		getName()
	{
		return (_name);
	}

	public String		getSymbol(int flags)
	{
		if (_symbol == null)
		{
			if (flags != _cacheFlags)
			{
				_cacheSymbol = String.valueOf(getChar(flags));
				_cacheFlags = flags;
			}
			return (_cacheSymbol);
		}
		return (_symbol);
	}

	public char			getChar(int flags)
	{
		if (flags != 0)
		{
			char c = _char;
			if ((flags & FLAG_SHIFT) != 0)
				c = Character.toUpperCase(_char);
			if ((flags & FLAG_ACCENT1) != 0)
				c = (char)KeyCharacterMap.getDeadChar('\u02CB', (int)c);
			if ((flags & FLAG_ACCENT2) != 0)
				c = (char)KeyCharacterMap.getDeadChar('\u00B4', (int)c);
			if ((flags & FLAG_ACCENT3) != 0)
				c = (char)KeyCharacterMap.getDeadChar('\u02C6', (int)c);
			if ((flags & FLAG_ACCENT4) != 0)
				c = (char)KeyCharacterMap.getDeadChar('\u02DC', (int)c);
			if ((flags & FLAG_ACCENT5) != 0)
				c = (char)KeyCharacterMap.getDeadChar('\u00B8', (int)c);
			if ((flags & FLAG_ACCENT6) != 0)
				c = (char)KeyCharacterMap.getDeadChar('\u00A8', (int)c);
			if (c != 0)
				return (c);
		}
		return (_char);
	}

	public int			getEventCode()
	{
		return (_eventCode);
	}

	public int			getFlags()
	{
		return (_flags);
	}

	private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>();

	protected KeyValue(String name, String symbol, char c, int eventCode, int flags)
	{
		_name = name;
		_symbol = symbol;
		_char = c;
		_eventCode = eventCode;
		_flags = flags;
		_cacheFlags = -1;
	}

	public static KeyValue	getKeyByName(String name)
	{
		return (KeyValue.keys.get(name));
	}

  private static void addKey(String name, String symbol, char c, int event, int flags)
  {
    keys.put(name, new KeyValue(name, symbol, c, event, flags));
  }

  private static void addCharKey(char c, int event)
  {
    String name = String.valueOf(c);
    addKey(name, name, c, event, 0);
  }

  private static void addModifierKey(String name, String symbol, int extra_flags)
  {
    addKey(name, symbol, CHAR_NONE, EVENT_NONE,
        FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_NOREPEAT | extra_flags);
  }

  private static void addSpecialKey(String name, String symbol, int event)
  {
    addKey(name, symbol, CHAR_NONE, event, FLAG_NOREPEAT);
  }

  static
  {
    String chars = "<>&\"'(-_)=°+"
      + "~#{[|`\\^@]}"
      + "^$*,;:!£%µ?./§";
    for (int i = 0; i < chars.length(); i++)
      addCharKey(chars.charAt(i), EVENT_NONE);

    addModifierKey("shift", "⇧", FLAG_LOCK | FLAG_SHIFT);
    addModifierKey("ctrl", "Ctrl", FLAG_CTRL);
    addModifierKey("alt", "Alt", FLAG_ALT);
    addModifierKey("accent1", "\u02CB", FLAG_ACCENT1);
    addModifierKey("accent2", "\u00B4", FLAG_ACCENT2);
    addModifierKey("accent3", "\u02C6", FLAG_ACCENT3);
    addModifierKey("accent4", "\u02DC", FLAG_ACCENT4);
    addModifierKey("accent5", "\u00B8", FLAG_ACCENT5);
    addModifierKey("accent6", "\u00A8", FLAG_ACCENT6);

    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);

    addSpecialKey("config", "Conf", EVENT_CONFIG);
    addSpecialKey("switch_text", "ABC", EVENT_SWITCH_TEXT);
    addSpecialKey("switch_numeric", "123+", EVENT_SWITCH_NUMERIC);
    addSpecialKey("switch_emoji", ":)", EVENT_SWITCH_EMOJI);
    addSpecialKey("switch_back_emoji", "ABC", EVENT_SWITCH_BACK_EMOJI);
    addSpecialKey("change_method", "⊞", EVENT_CHANGE_METHOD);

    addKey("esc",		"Esc",		CHAR_NONE,	KeyEvent.KEYCODE_ESCAPE,		0);
    addKey("enter",	"\uE800",	CHAR_NONE,	KeyEvent.KEYCODE_ENTER,			FLAG_KEY_FONT);
    addKey("up",		"\uE80B",	CHAR_NONE,	KeyEvent.KEYCODE_DPAD_UP,		FLAG_KEY_FONT);
    addKey("right",	"\uE80C",	CHAR_NONE,	KeyEvent.KEYCODE_DPAD_RIGHT,	FLAG_KEY_FONT);
    addKey("down",	"\uE809",	CHAR_NONE,	KeyEvent.KEYCODE_DPAD_DOWN,		FLAG_KEY_FONT);
    addKey("left",	"\uE80A",	CHAR_NONE,	KeyEvent.KEYCODE_DPAD_LEFT,		FLAG_KEY_FONT);
    addKey("page_up",	"⇞",		CHAR_NONE,	KeyEvent.KEYCODE_PAGE_DOWN,		0);
    addKey("page_down", "⇟",		CHAR_NONE,	KeyEvent.KEYCODE_PAGE_UP,		0);
    addKey("home",	"↖",		CHAR_NONE,	KeyEvent.KEYCODE_HOME,			0);
    addKey("end",		"↗",		CHAR_NONE,	KeyEvent.KEYCODE_MOVE_END,		0);
    addKey("backspace", "⌫",		CHAR_NONE,	KeyEvent.KEYCODE_DEL,			0);
    addKey("delete",	"⌦",		CHAR_NONE,	KeyEvent.KEYCODE_FORWARD_DEL,	0);
    addKey("insert",	"Ins",		CHAR_NONE,	KeyEvent.KEYCODE_INSERT,		0);

    addKey("tab",		"↹",		'\t',		KeyEvent.KEYCODE_TAB,			0);
    addKey("space",	null,		' ',		KeyEvent.KEYCODE_SPACE,			0);
  }
}