FreeRDP
Loading...
Searching...
No Matches
KeyboardMapper.java
1/*
2 Android Keyboard Mapping
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.utils;
12
13import android.content.Context;
14import android.view.KeyCharacterMap;
15import android.view.KeyEvent;
16
17import com.freerdp.freerdpcore.R;
18
19public class KeyboardMapper
20{
21 public static final int KEYBOARD_TYPE_FUNCTIONKEYS = 1;
22 public static final int KEYBOARD_TYPE_NUMPAD = 2;
23
24 // defines key states for modifier keys - locked means on and no auto-release if an other key is
25 // pressed
26 public static final int KEYSTATE_ON = 1;
27 public static final int KEYSTATE_LOCKED = 2;
28 public static final int KEYSTATE_OFF = 3;
29 final static int VK_LBUTTON = 0x01;
30 final static int VK_RBUTTON = 0x02;
31 final static int VK_CANCEL = 0x03;
32 final static int VK_MBUTTON = 0x04;
33 final static int VK_XBUTTON1 = 0x05;
34 final static int VK_XBUTTON2 = 0x06;
35 final static int VK_BACK = 0x08;
36 final static int VK_TAB = 0x09;
37 final static int VK_CLEAR = 0x0C;
38 final static int VK_RETURN = 0x0D;
39 final static int VK_SHIFT = 0x10;
40 final static int VK_CONTROL = 0x11;
41 final static int VK_MENU = 0x12;
42 final static int VK_PAUSE = 0x13;
43 final static int VK_CAPITAL = 0x14;
44 final static int VK_KANA = 0x15;
45 final static int VK_HANGUEL = 0x15;
46 final static int VK_HANGUL = 0x15;
47 final static int VK_JUNJA = 0x17;
48 final static int VK_FINAL = 0x18;
49 final static int VK_HANJA = 0x19;
50 final static int VK_KANJI = 0x19;
51 final static int VK_ESCAPE = 0x1B;
52 final static int VK_CONVERT = 0x1C;
53 final static int VK_NONCONVERT = 0x1D;
54 final static int VK_ACCEPT = 0x1E;
55 final static int VK_MODECHANGE = 0x1F;
56 final static int VK_SPACE = 0x20;
57 final static int VK_PRIOR = 0x21;
58 final static int VK_NEXT = 0x22;
59 final static int VK_END = 0x23;
60 final static int VK_HOME = 0x24;
61 final static int VK_LEFT = 0x25;
62 final static int VK_UP = 0x26;
63 final static int VK_RIGHT = 0x27;
64 final static int VK_DOWN = 0x28;
65 final static int VK_SELECT = 0x29;
66 final static int VK_PRINT = 0x2A;
67 final static int VK_EXECUTE = 0x2B;
68 final static int VK_SNAPSHOT = 0x2C;
69 final static int VK_INSERT = 0x2D;
70 final static int VK_DELETE = 0x2E;
71 final static int VK_HELP = 0x2F;
72 final static int VK_KEY_0 = 0x30;
73 final static int VK_KEY_1 = 0x31;
74 final static int VK_KEY_2 = 0x32;
75 final static int VK_KEY_3 = 0x33;
76 final static int VK_KEY_4 = 0x34;
77 final static int VK_KEY_5 = 0x35;
78 final static int VK_KEY_6 = 0x36;
79 final static int VK_KEY_7 = 0x37;
80 final static int VK_KEY_8 = 0x38;
81 final static int VK_KEY_9 = 0x39;
82 final static int VK_KEY_A = 0x41;
83 final static int VK_KEY_B = 0x42;
84 final static int VK_KEY_C = 0x43;
85 final static int VK_KEY_D = 0x44;
86 final static int VK_KEY_E = 0x45;
87 final static int VK_KEY_F = 0x46;
88 final static int VK_KEY_G = 0x47;
89 final static int VK_KEY_H = 0x48;
90 final static int VK_KEY_I = 0x49;
91 final static int VK_KEY_J = 0x4A;
92 final static int VK_KEY_K = 0x4B;
93 final static int VK_KEY_L = 0x4C;
94 final static int VK_KEY_M = 0x4D;
95 final static int VK_KEY_N = 0x4E;
96 final static int VK_KEY_O = 0x4F;
97 final static int VK_KEY_P = 0x50;
98 final static int VK_KEY_Q = 0x51;
99 final static int VK_KEY_R = 0x52;
100 final static int VK_KEY_S = 0x53;
101 final static int VK_KEY_T = 0x54;
102 final static int VK_KEY_U = 0x55;
103 final static int VK_KEY_V = 0x56;
104 final static int VK_KEY_W = 0x57;
105 final static int VK_KEY_X = 0x58;
106 final static int VK_KEY_Y = 0x59;
107 final static int VK_KEY_Z = 0x5A;
108 final static int VK_LWIN = 0x5B;
109 final static int VK_RWIN = 0x5C;
110 final static int VK_APPS = 0x5D;
111 final static int VK_SLEEP = 0x5F;
112 final static int VK_NUMPAD0 = 0x60;
113 final static int VK_NUMPAD1 = 0x61;
114 final static int VK_NUMPAD2 = 0x62;
115 final static int VK_NUMPAD3 = 0x63;
116 final static int VK_NUMPAD4 = 0x64;
117 final static int VK_NUMPAD5 = 0x65;
118 final static int VK_NUMPAD6 = 0x66;
119 final static int VK_NUMPAD7 = 0x67;
120 final static int VK_NUMPAD8 = 0x68;
121 final static int VK_NUMPAD9 = 0x69;
122 final static int VK_MULTIPLY = 0x6A;
123 final static int VK_ADD = 0x6B;
124 final static int VK_SEPARATOR = 0x6C;
125 final static int VK_SUBTRACT = 0x6D;
126 final static int VK_DECIMAL = 0x6E;
127 final static int VK_DIVIDE = 0x6F;
128 final static int VK_F1 = 0x70;
129 final static int VK_F2 = 0x71;
130 final static int VK_F3 = 0x72;
131 final static int VK_F4 = 0x73;
132 final static int VK_F5 = 0x74;
133 final static int VK_F6 = 0x75;
134 final static int VK_F7 = 0x76;
135 final static int VK_F8 = 0x77;
136 final static int VK_F9 = 0x78;
137 final static int VK_F10 = 0x79;
138 final static int VK_F11 = 0x7A;
139 final static int VK_F12 = 0x7B;
140 final static int VK_F13 = 0x7C;
141 final static int VK_F14 = 0x7D;
142 final static int VK_F15 = 0x7E;
143 final static int VK_F16 = 0x7F;
144 final static int VK_F17 = 0x80;
145 final static int VK_F18 = 0x81;
146 final static int VK_F19 = 0x82;
147 final static int VK_F20 = 0x83;
148 final static int VK_F21 = 0x84;
149 final static int VK_F22 = 0x85;
150 final static int VK_F23 = 0x86;
151 final static int VK_F24 = 0x87;
152 final static int VK_NUMLOCK = 0x90;
153 final static int VK_SCROLL = 0x91;
154 final static int VK_LSHIFT = 0xA0;
155 final static int VK_RSHIFT = 0xA1;
156 final static int VK_LCONTROL = 0xA2;
157 final static int VK_RCONTROL = 0xA3;
158 final static int VK_LMENU = 0xA4;
159 final static int VK_RMENU = 0xA5;
160 final static int VK_BROWSER_BACK = 0xA6;
161 final static int VK_BROWSER_FORWARD = 0xA7;
162 final static int VK_BROWSER_REFRESH = 0xA8;
163 final static int VK_BROWSER_STOP = 0xA9;
164 final static int VK_BROWSER_SEARCH = 0xAA;
165 final static int VK_BROWSER_FAVORITES = 0xAB;
166 final static int VK_BROWSER_HOME = 0xAC;
167 final static int VK_VOLUME_MUTE = 0xAD;
168 final static int VK_VOLUME_DOWN = 0xAE;
169 final static int VK_VOLUME_UP = 0xAF;
170 final static int VK_MEDIA_NEXT_TRACK = 0xB0;
171 final static int VK_MEDIA_PREV_TRACK = 0xB1;
172 final static int VK_MEDIA_STOP = 0xB2;
173 final static int VK_MEDIA_PLAY_PAUSE = 0xB3;
174 final static int VK_LAUNCH_MAIL = 0xB4;
175 final static int VK_LAUNCH_MEDIA_SELECT = 0xB5;
176 final static int VK_LAUNCH_APP1 = 0xB6;
177 final static int VK_LAUNCH_APP2 = 0xB7;
178 final static int VK_OEM_1 = 0xBA;
179 final static int VK_OEM_PLUS = 0xBB;
180 final static int VK_OEM_COMMA = 0xBC;
181 final static int VK_OEM_MINUS = 0xBD;
182 final static int VK_OEM_PERIOD = 0xBE;
183 final static int VK_OEM_2 = 0xBF;
184 final static int VK_OEM_3 = 0xC0;
185 final static int VK_ABNT_C1 = 0xC1;
186 final static int VK_ABNT_C2 = 0xC2;
187 final static int VK_OEM_4 = 0xDB;
188 final static int VK_OEM_5 = 0xDC;
189 final static int VK_OEM_6 = 0xDD;
190 final static int VK_OEM_7 = 0xDE;
191 final static int VK_OEM_8 = 0xDF;
192 final static int VK_OEM_102 = 0xE2;
193 final static int VK_PROCESSKEY = 0xE5;
194 final static int VK_PACKET = 0xE7;
195 final static int VK_ATTN = 0xF6;
196 final static int VK_CRSEL = 0xF7;
197 final static int VK_EXSEL = 0xF8;
198 final static int VK_EREOF = 0xF9;
199 final static int VK_PLAY = 0xFA;
200 final static int VK_ZOOM = 0xFB;
201 final static int VK_NONAME = 0xFC;
202 final static int VK_PA1 = 0xFD;
203 final static int VK_OEM_CLEAR = 0xFE;
204 final static int VK_UNICODE = 0x80000000;
205 final static int VK_EXT_KEY = 0x00000100;
206 // key codes to switch between custom keyboard
207 private final static int EXTKEY_KBFUNCTIONKEYS = 0x1100;
208 private final static int EXTKEY_KBNUMPAD = 0x1101;
209 // this flag indicates if we got a VK or a unicode character in our translation map
210 private static final int KEY_FLAG_UNICODE = 0x80000000;
211 // this flag indicates if the key is a toggle key (remains down when pressed and goes up if
212 // pressed again)
213 private static final int KEY_FLAG_TOGGLE = 0x40000000;
214 private static int[] keymapAndroid;
215 private static int[] keymapExt;
216 private static boolean initialized = false;
217 private KeyProcessingListener listener = null;
218 private boolean shiftPressed = false;
219 private boolean ctrlPressed = false;
220 private boolean altPressed = false;
221 private boolean winPressed = false;
222 private boolean isShiftLocked = false;
223 private boolean isCtrlLocked = false;
224 private boolean isAltLocked = false;
225 private boolean isWinLocked = false;
226 private boolean isWinKeyDown = false;
227 private boolean isWinComboUsed = false;
228
229 public void init(Context context)
230 {
231 if (initialized)
232 return;
233
234 keymapAndroid = new int[256];
235
236 keymapAndroid[KeyEvent.KEYCODE_0] = VK_KEY_0;
237 keymapAndroid[KeyEvent.KEYCODE_1] = VK_KEY_1;
238 keymapAndroid[KeyEvent.KEYCODE_2] = VK_KEY_2;
239 keymapAndroid[KeyEvent.KEYCODE_3] = VK_KEY_3;
240 keymapAndroid[KeyEvent.KEYCODE_4] = VK_KEY_4;
241 keymapAndroid[KeyEvent.KEYCODE_5] = VK_KEY_5;
242 keymapAndroid[KeyEvent.KEYCODE_6] = VK_KEY_6;
243 keymapAndroid[KeyEvent.KEYCODE_7] = VK_KEY_7;
244 keymapAndroid[KeyEvent.KEYCODE_8] = VK_KEY_8;
245 keymapAndroid[KeyEvent.KEYCODE_9] = VK_KEY_9;
246
247 keymapAndroid[KeyEvent.KEYCODE_A] = VK_KEY_A;
248 keymapAndroid[KeyEvent.KEYCODE_B] = VK_KEY_B;
249 keymapAndroid[KeyEvent.KEYCODE_C] = VK_KEY_C;
250 keymapAndroid[KeyEvent.KEYCODE_D] = VK_KEY_D;
251 keymapAndroid[KeyEvent.KEYCODE_E] = VK_KEY_E;
252 keymapAndroid[KeyEvent.KEYCODE_F] = VK_KEY_F;
253 keymapAndroid[KeyEvent.KEYCODE_G] = VK_KEY_G;
254 keymapAndroid[KeyEvent.KEYCODE_H] = VK_KEY_H;
255 keymapAndroid[KeyEvent.KEYCODE_I] = VK_KEY_I;
256 keymapAndroid[KeyEvent.KEYCODE_J] = VK_KEY_J;
257 keymapAndroid[KeyEvent.KEYCODE_K] = VK_KEY_K;
258 keymapAndroid[KeyEvent.KEYCODE_L] = VK_KEY_L;
259 keymapAndroid[KeyEvent.KEYCODE_M] = VK_KEY_M;
260 keymapAndroid[KeyEvent.KEYCODE_N] = VK_KEY_N;
261 keymapAndroid[KeyEvent.KEYCODE_O] = VK_KEY_O;
262 keymapAndroid[KeyEvent.KEYCODE_P] = VK_KEY_P;
263 keymapAndroid[KeyEvent.KEYCODE_Q] = VK_KEY_Q;
264 keymapAndroid[KeyEvent.KEYCODE_R] = VK_KEY_R;
265 keymapAndroid[KeyEvent.KEYCODE_S] = VK_KEY_S;
266 keymapAndroid[KeyEvent.KEYCODE_T] = VK_KEY_T;
267 keymapAndroid[KeyEvent.KEYCODE_U] = VK_KEY_U;
268 keymapAndroid[KeyEvent.KEYCODE_V] = VK_KEY_V;
269 keymapAndroid[KeyEvent.KEYCODE_W] = VK_KEY_W;
270 keymapAndroid[KeyEvent.KEYCODE_X] = VK_KEY_X;
271 keymapAndroid[KeyEvent.KEYCODE_Y] = VK_KEY_Y;
272 keymapAndroid[KeyEvent.KEYCODE_Z] = VK_KEY_Z;
273
274 keymapAndroid[KeyEvent.KEYCODE_DEL] = VK_BACK;
275 keymapAndroid[KeyEvent.KEYCODE_ENTER] = VK_RETURN;
276 keymapAndroid[KeyEvent.KEYCODE_SPACE] = VK_SPACE;
277 keymapAndroid[KeyEvent.KEYCODE_TAB] = VK_TAB;
278 keymapAndroid[KeyEvent.KEYCODE_ESCAPE] = VK_ESCAPE;
279 keymapAndroid[KeyEvent.KEYCODE_CAPS_LOCK] = VK_CAPITAL;
280 // keymapAndroid[KeyEvent.KEYCODE_SHIFT_LEFT] = VK_LSHIFT;
281 // keymapAndroid[KeyEvent.KEYCODE_SHIFT_RIGHT] = VK_RSHIFT;
282
283 keymapAndroid[KeyEvent.KEYCODE_DPAD_DOWN] = VK_DOWN | VK_EXT_KEY;
284 keymapAndroid[KeyEvent.KEYCODE_DPAD_LEFT] = VK_LEFT | VK_EXT_KEY;
285 keymapAndroid[KeyEvent.KEYCODE_DPAD_RIGHT] = VK_RIGHT | VK_EXT_KEY;
286 keymapAndroid[KeyEvent.KEYCODE_DPAD_UP] = VK_UP | VK_EXT_KEY;
287 keymapAndroid[KeyEvent.KEYCODE_MOVE_HOME] = VK_HOME | VK_EXT_KEY;
288 keymapAndroid[KeyEvent.KEYCODE_MOVE_END] = VK_END | VK_EXT_KEY;
289 keymapAndroid[KeyEvent.KEYCODE_PAGE_UP] = VK_PRIOR | VK_EXT_KEY;
290 keymapAndroid[KeyEvent.KEYCODE_PAGE_DOWN] = VK_NEXT | VK_EXT_KEY;
291 keymapAndroid[KeyEvent.KEYCODE_FORWARD_DEL] = VK_DELETE | VK_EXT_KEY;
292 keymapAndroid[KeyEvent.KEYCODE_INSERT] = VK_INSERT | VK_EXT_KEY;
293
294 keymapAndroid[KeyEvent.KEYCODE_MINUS] = VK_OEM_MINUS;
295 keymapAndroid[KeyEvent.KEYCODE_EQUALS] = VK_OEM_PLUS;
296 keymapAndroid[KeyEvent.KEYCODE_LEFT_BRACKET] = VK_OEM_4;
297 keymapAndroid[KeyEvent.KEYCODE_RIGHT_BRACKET] = VK_OEM_6;
298 keymapAndroid[KeyEvent.KEYCODE_BACKSLASH] = VK_OEM_5;
299 keymapAndroid[KeyEvent.KEYCODE_SEMICOLON] = VK_OEM_1;
300 keymapAndroid[KeyEvent.KEYCODE_APOSTROPHE] = VK_OEM_7;
301 keymapAndroid[KeyEvent.KEYCODE_GRAVE] = VK_OEM_3;
302 keymapAndroid[KeyEvent.KEYCODE_COMMA] = VK_OEM_COMMA;
303 keymapAndroid[KeyEvent.KEYCODE_PERIOD] = VK_OEM_PERIOD;
304 keymapAndroid[KeyEvent.KEYCODE_SLASH] = VK_OEM_2;
305
306 keymapAndroid[KeyEvent.KEYCODE_F1] = VK_F1;
307 keymapAndroid[KeyEvent.KEYCODE_F2] = VK_F2;
308 keymapAndroid[KeyEvent.KEYCODE_F3] = VK_F3;
309 keymapAndroid[KeyEvent.KEYCODE_F4] = VK_F4;
310 keymapAndroid[KeyEvent.KEYCODE_F5] = VK_F5;
311 keymapAndroid[KeyEvent.KEYCODE_F6] = VK_F6;
312 keymapAndroid[KeyEvent.KEYCODE_F7] = VK_F7;
313 keymapAndroid[KeyEvent.KEYCODE_F8] = VK_F8;
314 keymapAndroid[KeyEvent.KEYCODE_F9] = VK_F9;
315 keymapAndroid[KeyEvent.KEYCODE_F10] = VK_F10;
316 keymapAndroid[KeyEvent.KEYCODE_F11] = VK_F11;
317 keymapAndroid[KeyEvent.KEYCODE_F12] = VK_F12;
318
319 keymapAndroid[KeyEvent.KEYCODE_NUM_LOCK] = VK_NUMLOCK;
320 keymapAndroid[KeyEvent.KEYCODE_SCROLL_LOCK] = VK_SCROLL;
321 keymapAndroid[KeyEvent.KEYCODE_SYSRQ] = VK_SNAPSHOT | VK_EXT_KEY;
322 keymapAndroid[KeyEvent.KEYCODE_BREAK] = VK_PAUSE;
323
324 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_0] = VK_NUMPAD0;
325 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_1] = VK_NUMPAD1;
326 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_2] = VK_NUMPAD2;
327 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_3] = VK_NUMPAD3;
328 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_4] = VK_NUMPAD4;
329 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_5] = VK_NUMPAD5;
330 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_6] = VK_NUMPAD6;
331 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_7] = VK_NUMPAD7;
332 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_8] = VK_NUMPAD8;
333 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_9] = VK_NUMPAD9;
334 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_DIVIDE] = VK_DIVIDE | VK_EXT_KEY;
335 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_MULTIPLY] = VK_MULTIPLY;
336 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_SUBTRACT] = VK_SUBTRACT;
337 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_ADD] = VK_ADD;
338 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_DOT] = VK_DECIMAL;
339 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_COMMA] = VK_DECIMAL;
340 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_ENTER] = VK_RETURN | VK_EXT_KEY;
341 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_EQUALS] = KEY_FLAG_UNICODE | 61;
342 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN] = KEY_FLAG_UNICODE | 40;
343 keymapAndroid[KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN] = KEY_FLAG_UNICODE | 41;
344
345 // special keys mapping
346 keymapExt = new int[256];
347 keymapExt[context.getResources().getInteger(R.integer.keycode_F1)] = VK_F1;
348 keymapExt[context.getResources().getInteger(R.integer.keycode_F2)] = VK_F2;
349 keymapExt[context.getResources().getInteger(R.integer.keycode_F3)] = VK_F3;
350 keymapExt[context.getResources().getInteger(R.integer.keycode_F4)] = VK_F4;
351 keymapExt[context.getResources().getInteger(R.integer.keycode_F5)] = VK_F5;
352 keymapExt[context.getResources().getInteger(R.integer.keycode_F6)] = VK_F6;
353 keymapExt[context.getResources().getInteger(R.integer.keycode_F7)] = VK_F7;
354 keymapExt[context.getResources().getInteger(R.integer.keycode_F8)] = VK_F8;
355 keymapExt[context.getResources().getInteger(R.integer.keycode_F9)] = VK_F9;
356 keymapExt[context.getResources().getInteger(R.integer.keycode_F10)] = VK_F10;
357 keymapExt[context.getResources().getInteger(R.integer.keycode_F11)] = VK_F11;
358 keymapExt[context.getResources().getInteger(R.integer.keycode_F12)] = VK_F12;
359 keymapExt[context.getResources().getInteger(R.integer.keycode_tab)] = VK_TAB;
360 keymapExt[context.getResources().getInteger(R.integer.keycode_print)] = VK_PRINT;
361 keymapExt[context.getResources().getInteger(R.integer.keycode_insert)] =
362 VK_INSERT | VK_EXT_KEY;
363 keymapExt[context.getResources().getInteger(R.integer.keycode_delete)] =
364 VK_DELETE | VK_EXT_KEY;
365 keymapExt[context.getResources().getInteger(R.integer.keycode_home)] = VK_HOME | VK_EXT_KEY;
366 keymapExt[context.getResources().getInteger(R.integer.keycode_end)] = VK_END | VK_EXT_KEY;
367 keymapExt[context.getResources().getInteger(R.integer.keycode_pgup)] =
368 VK_PRIOR | VK_EXT_KEY;
369 keymapExt[context.getResources().getInteger(R.integer.keycode_pgdn)] = VK_NEXT | VK_EXT_KEY;
370
371 // numpad mapping
372 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_0)] = VK_NUMPAD0;
373 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_1)] = VK_NUMPAD1;
374 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_2)] = VK_NUMPAD2;
375 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_3)] = VK_NUMPAD3;
376 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_4)] = VK_NUMPAD4;
377 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_5)] = VK_NUMPAD5;
378 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_6)] = VK_NUMPAD6;
379 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_7)] = VK_NUMPAD7;
380 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_8)] = VK_NUMPAD8;
381 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_9)] = VK_NUMPAD9;
382 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_numlock)] = VK_NUMLOCK;
383 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_add)] = VK_ADD;
384 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_comma)] = VK_DECIMAL;
385 keymapExt[context.getResources().getInteger(R.integer.keycode_comma)] = VK_OEM_COMMA;
386 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_divide)] =
387 VK_DIVIDE | VK_EXT_KEY;
388 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_enter)] =
389 VK_RETURN | VK_EXT_KEY;
390 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_multiply)] =
391 VK_MULTIPLY;
392 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_subtract)] =
393 VK_SUBTRACT;
394 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_equals)] =
395 (KEY_FLAG_UNICODE | 61);
396 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_left_paren)] =
397 (KEY_FLAG_UNICODE | 40);
398 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_right_paren)] =
399 (KEY_FLAG_UNICODE | 41);
400
401 // cursor key codes
402 keymapExt[context.getResources().getInteger(R.integer.keycode_up)] = VK_UP | VK_EXT_KEY;
403 keymapExt[context.getResources().getInteger(R.integer.keycode_down)] = VK_DOWN | VK_EXT_KEY;
404 keymapExt[context.getResources().getInteger(R.integer.keycode_left)] = VK_LEFT | VK_EXT_KEY;
405 keymapExt[context.getResources().getInteger(R.integer.keycode_right)] =
406 VK_RIGHT | VK_EXT_KEY;
407 keymapExt[context.getResources().getInteger(R.integer.keycode_enter)] =
408 VK_RETURN | VK_EXT_KEY;
409 keymapExt[context.getResources().getInteger(R.integer.keycode_backspace)] = VK_BACK;
410
411 // shared keys
412 keymapExt[context.getResources().getInteger(R.integer.keycode_win)] = VK_LWIN | VK_EXT_KEY;
413 keymapExt[context.getResources().getInteger(R.integer.keycode_menu)] = VK_APPS | VK_EXT_KEY;
414 keymapExt[context.getResources().getInteger(R.integer.keycode_esc)] = VK_ESCAPE;
415
416 /* keymapExt[context.getResources().getInteger(R.integer.keycode_modifier_ctrl)] =
417 VK_LCONTROL; keymapExt[context.getResources().getInteger(R.integer.keycode_modifier_alt)]
418 = VK_LMENU;
419 keymapExt[context.getResources().getInteger(R.integer.keycode_modifier_shift)] =
420 VK_LSHIFT;
421 */
422 // get custom keyboard key codes
423 keymapExt[context.getResources().getInteger(R.integer.keycode_specialkeys_keyboard)] =
424 EXTKEY_KBFUNCTIONKEYS;
425 keymapExt[context.getResources().getInteger(R.integer.keycode_numpad_keyboard)] =
426 EXTKEY_KBNUMPAD;
427
428 keymapExt[context.getResources().getInteger(R.integer.keycode_toggle_shift)] =
429 (KEY_FLAG_TOGGLE | VK_LSHIFT);
430 keymapExt[context.getResources().getInteger(R.integer.keycode_toggle_ctrl)] =
431 (KEY_FLAG_TOGGLE | VK_LCONTROL);
432 keymapExt[context.getResources().getInteger(R.integer.keycode_toggle_alt)] =
433 (KEY_FLAG_TOGGLE | VK_LMENU);
434 keymapExt[context.getResources().getInteger(R.integer.keycode_toggle_win)] =
435 (KEY_FLAG_TOGGLE | VK_LWIN);
436
437 initialized = true;
438 }
439
440 public void reset(KeyProcessingListener listener)
441 {
442 shiftPressed = false;
443 ctrlPressed = false;
444 altPressed = false;
445 winPressed = false;
446 isWinKeyDown = false;
447 isWinComboUsed = false;
448 setKeyProcessingListener(listener);
449 }
450
451 public void setKeyProcessingListener(KeyProcessingListener listener)
452 {
453 this.listener = listener;
454 }
455
456 public boolean processAndroidKeyEvent(KeyEvent event)
457 {
458 switch (event.getAction())
459 {
460 // we only process down events
461 case KeyEvent.ACTION_UP:
462 {
463 if (event.getKeyCode() == KeyEvent.KEYCODE_META_LEFT ||
464 event.getKeyCode() == KeyEvent.KEYCODE_META_RIGHT)
465 {
466 if (!isWinKeyDown)
467 return false;
468 isWinKeyDown = false;
469 if (!isWinComboUsed)
470 {
471 listener.processVirtualKey(VK_LWIN | VK_EXT_KEY, true);
472 listener.processVirtualKey(VK_LWIN | VK_EXT_KEY, false);
473 }
474 isWinComboUsed = false;
475 return true;
476 }
477 return false;
478 }
479
480 case KeyEvent.ACTION_DOWN:
481 {
482 /* Physical Win key: buffer until release to distinguish tap vs combo. */
483 if (event.getKeyCode() == KeyEvent.KEYCODE_META_LEFT ||
484 event.getKeyCode() == KeyEvent.KEYCODE_META_RIGHT)
485 {
486 isWinKeyDown = true;
487 isWinComboUsed = false;
488 return true;
489 }
490
491 if (isWinKeyDown)
492 isWinComboUsed = true;
493
494 boolean modifierActive = isModifierPressed();
495 // if a modifier is pressed we will send a VK event (if possible) so that key
496 // combinations will be recognized correctly. Otherwise we will send the unicode
497 // key. At the end we will reset all modifiers and notify our listener.
498 int vkcode = getVirtualKeyCode(event.getKeyCode());
499 if ((vkcode & KEY_FLAG_UNICODE) != 0)
500 listener.processUnicodeKey(vkcode & (~KEY_FLAG_UNICODE));
501 // if we got a valid vkcode send it - except for letters/numbers if a modifier is
502 // active
503 else if (vkcode > 0 && !event.isSymPressed())
504 {
505 boolean sendCtrl = !ctrlPressed && event.isCtrlPressed();
506 boolean sendAlt = !altPressed && event.isAltPressed();
507 boolean sendWin = !winPressed && isWinKeyDown;
508 boolean sendShift = !shiftPressed && event.isShiftPressed();
509
510 if (sendCtrl)
511 listener.processVirtualKey(VK_LCONTROL, true);
512 if (sendAlt)
513 listener.processVirtualKey(VK_LMENU, true);
514 if (sendWin)
515 listener.processVirtualKey(VK_LWIN | VK_EXT_KEY, true);
516 if (sendShift)
517 listener.processVirtualKey(VK_LSHIFT, true);
518
519 listener.processVirtualKey(vkcode, true);
520 listener.processVirtualKey(vkcode, false);
521
522 if (sendShift)
523 listener.processVirtualKey(VK_LSHIFT, false);
524 if (sendWin)
525 listener.processVirtualKey(VK_LWIN | VK_EXT_KEY, false);
526 if (sendAlt)
527 listener.processVirtualKey(VK_LMENU, false);
528 if (sendCtrl)
529 listener.processVirtualKey(VK_LCONTROL, false);
530 }
531 else if (event.getUnicodeChar() != 0)
532 listener.processUnicodeKey(event.getUnicodeChar());
533 else
534 return false;
535
536 // reset any pending toggle states if a modifier was pressed
537 if (modifierActive)
538 resetModifierKeysAfterInput(false);
539 return true;
540 }
541
542 case KeyEvent.ACTION_MULTIPLE:
543 {
544 String str = event.getCharacters();
545 if (str != null)
546 {
547 for (int i = 0; i < str.length(); i++)
548 listener.processUnicodeKey(str.charAt(i));
549 }
550 return true;
551 }
552
553 default:
554 break;
555 }
556 return false;
557 }
558
559 public void processCustomKeyEvent(int keycode)
560 {
561 int extCode = getExtendedKeyCode(keycode);
562 if (extCode == 0)
563 return;
564
565 // toggle button pressed?
566 if ((extCode & KEY_FLAG_TOGGLE) != 0)
567 {
568 processToggleButton(extCode & (~KEY_FLAG_TOGGLE), false);
569 return;
570 }
571
572 // keyboard switch button pressed?
573 if (extCode == EXTKEY_KBFUNCTIONKEYS || extCode == EXTKEY_KBNUMPAD)
574 {
575 switchKeyboard(extCode);
576 return;
577 }
578
579 // nope - see if we got a unicode or vk
580 if ((extCode & KEY_FLAG_UNICODE) != 0)
581 listener.processUnicodeKey(extCode & (~KEY_FLAG_UNICODE));
582 else
583 {
584 listener.processVirtualKey(extCode, true);
585 listener.processVirtualKey(extCode, false);
586 }
587
588 resetModifierKeysAfterInput(false);
589 }
590
591 // Locks a sticky modifier down. Returns false if the keycode is not a modifier.
592 public boolean processCustomKeyLock(int keycode)
593 {
594 int extCode = getExtendedKeyCode(keycode);
595 if ((extCode & KEY_FLAG_TOGGLE) == 0)
596 return false;
597
598 processToggleButton(extCode & (~KEY_FLAG_TOGGLE), true);
599 return true;
600 }
601
602 public void processUnicodeFallback(int unicodeKey)
603 {
604 KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
605 KeyEvent[] events = kcm.getEvents(new char[] { (char)unicodeKey });
606 if (events == null)
607 return;
608 for (KeyEvent event : events)
609 {
610 int vkcode = fallbackVkcode(event.getKeyCode());
611 if (vkcode > 0)
612 listener.processVirtualKey(vkcode, event.getAction() == KeyEvent.ACTION_DOWN);
613 }
614 }
615
616 private int fallbackVkcode(int keycode)
617 {
618 switch (keycode)
619 {
620 case KeyEvent.KEYCODE_SHIFT_LEFT:
621 case KeyEvent.KEYCODE_SHIFT_RIGHT:
622 return VK_LSHIFT;
623 case KeyEvent.KEYCODE_ALT_LEFT:
624 case KeyEvent.KEYCODE_ALT_RIGHT:
625 return VK_LMENU;
626 case KeyEvent.KEYCODE_CTRL_LEFT:
627 case KeyEvent.KEYCODE_CTRL_RIGHT:
628 return VK_LCONTROL;
629 default:
630 return getVirtualKeyCode(keycode);
631 }
632 }
633
634 public void sendAltF4()
635 {
636 listener.processVirtualKey(VK_LMENU, true);
637 listener.processVirtualKey(VK_F4, true);
638 listener.processVirtualKey(VK_F4, false);
639 listener.processVirtualKey(VK_LMENU, false);
640 }
641
642 private boolean isModifierPressed()
643 {
644 return (shiftPressed || ctrlPressed || altPressed || winPressed);
645 }
646
647 public int getModifierState(int keycode)
648 {
649 int modifierCode = getExtendedKeyCode(keycode);
650
651 // check and get real modifier keycode
652 if ((modifierCode & KEY_FLAG_TOGGLE) == 0)
653 return -1;
654 modifierCode = modifierCode & (~KEY_FLAG_TOGGLE);
655
656 switch (modifierCode)
657 {
658 case VK_LSHIFT:
659 {
660 return (shiftPressed ? (isShiftLocked ? KEYSTATE_LOCKED : KEYSTATE_ON)
661 : KEYSTATE_OFF);
662 }
663 case VK_LCONTROL:
664 {
665 return (ctrlPressed ? (isCtrlLocked ? KEYSTATE_LOCKED : KEYSTATE_ON)
666 : KEYSTATE_OFF);
667 }
668 case VK_LMENU:
669 {
670 return (altPressed ? (isAltLocked ? KEYSTATE_LOCKED : KEYSTATE_ON) : KEYSTATE_OFF);
671 }
672 case VK_LWIN:
673 {
674 return (winPressed ? (isWinLocked ? KEYSTATE_LOCKED : KEYSTATE_ON) : KEYSTATE_OFF);
675 }
676 }
677
678 return -1;
679 }
680
681 private int getVirtualKeyCode(int keycode)
682 {
683 if (keycode >= 0 && keycode <= 0xFF)
684 return keymapAndroid[keycode];
685 return 0;
686 }
687
688 private int getExtendedKeyCode(int keycode)
689 {
690 if (keycode >= 0 && keycode <= 0xFF)
691 return keymapExt[keycode];
692 return 0;
693 }
694
695 // A tap arms the modifier for the next key only; a long press latches it until pressed again.
696 private void processToggleButton(int keycode, boolean lock)
697 {
698 switch (keycode)
699 {
700 case VK_LSHIFT:
701 {
702 isShiftLocked = lock && !isShiftLocked;
703 shiftPressed = applyModifier(VK_LSHIFT, shiftPressed, isShiftLocked, lock);
704 break;
705 }
706 case VK_LCONTROL:
707 {
708 isCtrlLocked = lock && !isCtrlLocked;
709 ctrlPressed = applyModifier(VK_LCONTROL, ctrlPressed, isCtrlLocked, lock);
710 break;
711 }
712 case VK_LMENU:
713 {
714 isAltLocked = lock && !isAltLocked;
715 altPressed = applyModifier(VK_LMENU, altPressed, isAltLocked, lock);
716 break;
717 }
718 case VK_LWIN:
719 {
720 isWinLocked = lock && !isWinLocked;
721 winPressed = applyModifier(VK_LWIN | VK_EXT_KEY, winPressed, isWinLocked, lock);
722 break;
723 }
724 }
725 listener.modifiersChanged();
726 }
727
728 // Reports the new pressed state only on a real change, so long pressing an already armed
729 // modifier does not send a second key down.
730 private boolean applyModifier(int vk, boolean pressed, boolean locked, boolean lock)
731 {
732 boolean target = lock ? locked : !pressed;
733 if (target != pressed)
734 listener.processVirtualKey(vk, target);
735 return target;
736 }
737
738 public void clearlAllModifiers()
739 {
740 resetModifierKeysAfterInput(true);
741 }
742
743 private void resetModifierKeysAfterInput(boolean force)
744 {
745 if (shiftPressed && (!isShiftLocked || force))
746 {
747 listener.processVirtualKey(VK_LSHIFT, false);
748 shiftPressed = false;
749 }
750 if (ctrlPressed && (!isCtrlLocked || force))
751 {
752 listener.processVirtualKey(VK_LCONTROL, false);
753 ctrlPressed = false;
754 }
755 if (altPressed && (!isAltLocked || force))
756 {
757 listener.processVirtualKey(VK_LMENU, false);
758 altPressed = false;
759 }
760 if (winPressed && (!isWinLocked || force))
761 {
762 listener.processVirtualKey(VK_LWIN | VK_EXT_KEY, false);
763 winPressed = false;
764 }
765
766 // drop the locks too, a stale lock flag would swallow the next long press
767 if (force)
768 isShiftLocked = isCtrlLocked = isAltLocked = isWinLocked = false;
769
770 if (listener != null)
771 listener.modifiersChanged();
772 }
773
774 private void switchKeyboard(int keycode)
775 {
776 switch (keycode)
777 {
778 case EXTKEY_KBFUNCTIONKEYS:
779 {
780 listener.switchKeyboard(KEYBOARD_TYPE_FUNCTIONKEYS);
781 break;
782 }
783
784 case EXTKEY_KBNUMPAD:
785 {
786 listener.switchKeyboard(KEYBOARD_TYPE_NUMPAD);
787 break;
788 }
789
790 default:
791 break;
792 }
793 }
794
795 // interface that gets called for input handling
796 public interface KeyProcessingListener {
797 void processVirtualKey(int virtualKeyCode, boolean down);
798
799 void processUnicodeKey(int unicodeKey);
800
801 void switchKeyboard(int keyboardType);
802
803 void modifiersChanged();
804 }
805}