blob: 885a75f641682ed0890f7b95065131abc6e60fbe [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
19import android.text.method.MetaKeyKeyListener;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080020import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.util.SparseIntArray;
22import android.os.RemoteException;
23import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.SparseArray;
25
26import java.lang.Character;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Jeff Brownc5ed5912010-07-14 18:48:53 -070028/**
Jeff Brown6b53e8d2010-11-10 16:03:06 -080029 * Describes the keys provided by a keyboard device and their associated labels.
Jeff Brownc5ed5912010-07-14 18:48:53 -070030 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -080031public class KeyCharacterMap {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 /**
33 * The id of the device's primary built in keyboard is always 0.
Jeff Brown1f245102010-11-18 20:53:46 -080034 *
35 * @deprecated This constant should no longer be used because there is no
36 * guarantee that a device has a built-in keyboard that can be used for
37 * typing text. There might not be a built-in keyboard, the built-in keyboard
38 * might be a {@link #NUMERIC} or {@link #SPECIAL_FUNCTION} keyboard, or there
39 * might be multiple keyboards installed including external keyboards.
40 * When interpreting key presses received from the framework, applications should
Jeff Brown46a5ae62010-11-30 19:52:29 -080041 * use the device id specified in the {@link KeyEvent} received.
Jeff Brown1f245102010-11-18 20:53:46 -080042 * When synthesizing key presses for delivery elsewhere or when translating key presses
43 * from unknown keyboards, applications should use the special {@link #VIRTUAL_KEYBOARD}
44 * device id.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Jeff Brown1f245102010-11-18 20:53:46 -080046 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 public static final int BUILT_IN_KEYBOARD = 0;
48
Jeff Brown6b53e8d2010-11-10 16:03:06 -080049 /**
50 * The id of a generic virtual keyboard with a full layout that can be used to
51 * synthesize key events. Typically used with {@link #getEvents}.
52 */
53 public static final int VIRTUAL_KEYBOARD = -1;
54
55 /**
56 * A numeric (12-key) keyboard.
57 * <p>
58 * A numeric keyboard supports text entry using a multi-tap approach.
59 * It may be necessary to tap a key multiple times to generate the desired letter
60 * or symbol.
61 * </p><p>
62 * This type of keyboard is generally designed for thumb typing.
63 * </p>
64 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 public static final int NUMERIC = 1;
66
Jeff Brown6b53e8d2010-11-10 16:03:06 -080067 /**
68 * A keyboard with all the letters, but with more than one letter per key.
69 * <p>
70 * This type of keyboard is generally designed for thumb typing.
71 * </p>
72 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 public static final int PREDICTIVE = 2;
74
Jeff Brown6b53e8d2010-11-10 16:03:06 -080075 /**
76 * A keyboard with all the letters, and maybe some numbers.
77 * <p>
78 * An alphabetic keyboard supports text entry directly but may have a condensed
79 * layout with a small form factor. In contrast to a {@link #FULL full keyboard}, some
80 * symbols may only be accessible using special on-screen character pickers.
81 * In addition, to improve typing speed and accuracy, the framework provides
82 * special affordances for alphabetic keyboards such as auto-capitalization
83 * and toggled / locked shift and alt keys.
84 * </p><p>
85 * This type of keyboard is generally designed for thumb typing.
86 * </p>
87 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int ALPHA = 3;
89
90 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -080091 * A full PC-style keyboard.
92 * <p>
93 * A full keyboard behaves like a PC keyboard. All symbols are accessed directly
94 * by pressing keys on the keyboard without on-screen support or affordances such
95 * as auto-capitalization.
96 * </p><p>
97 * This type of keyboard is generally designed for full two hand typing.
98 * </p>
99 */
100 public static final int FULL = 4;
101
102 /**
103 * A keyboard that is only used to control special functions rather than for typing.
104 * <p>
105 * A special function keyboard consists only of non-printing keys such as
106 * HOME and POWER that are not actually used for typing.
107 * </p>
108 */
109 public static final int SPECIAL_FUNCTION = 5;
110
111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * This private-use character is used to trigger Unicode character
113 * input by hex digits.
114 */
115 public static final char HEX_INPUT = '\uEF00';
116
117 /**
118 * This private-use character is used to bring up a character picker for
119 * miscellaneous symbols.
120 */
121 public static final char PICKER_DIALOG_INPUT = '\uEF01';
122
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800123 /**
124 * Modifier keys may be chorded with character keys.
125 *
126 * @see {#link #getModifierBehavior()} for more details.
127 */
128 public static final int MODIFIER_BEHAVIOR_CHORDED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Jeff Brownc5ed5912010-07-14 18:48:53 -0700130 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800131 * Modifier keys may be chorded with character keys or they may toggle
132 * into latched or locked states when pressed independently.
133 *
134 * @see {#link #getModifierBehavior()} for more details.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700135 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800136 public static final int MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED = 1;
137
138 private static SparseArray<KeyCharacterMap> sInstances = new SparseArray<KeyCharacterMap>();
139
140 private final int mDeviceId;
141 private int mPtr;
142
143 private static native int nativeLoad(int id);
144 private static native void nativeDispose(int ptr);
145
146 private static native char nativeGetCharacter(int ptr, int keyCode, int metaState);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800147 private static native boolean nativeGetFallbackAction(int ptr, int keyCode, int metaState,
148 FallbackAction outFallbackAction);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800149 private static native char nativeGetNumber(int ptr, int keyCode);
150 private static native char nativeGetMatch(int ptr, int keyCode, char[] chars, int metaState);
151 private static native char nativeGetDisplayLabel(int ptr, int keyCode);
152 private static native int nativeGetKeyboardType(int ptr);
153 private static native KeyEvent[] nativeGetEvents(int ptr, int deviceId, char[] chars);
154
155 private KeyCharacterMap(int deviceId, int ptr) {
156 mDeviceId = deviceId;
157 mPtr = ptr;
158 }
159
160 @Override
161 protected void finalize() throws Throwable {
162 if (mPtr != 0) {
163 nativeDispose(mPtr);
164 mPtr = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166 }
167
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800168 /**
169 * Loads the key character maps for the keyboard with the specified device id.
170 *
171 * @param deviceId The device id of the keyboard.
172 * @return The associated key character map.
Jeff Brown7e1e21f2011-01-19 17:05:01 -0800173 * @throws {@link UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800174 * could not be loaded because it was malformed or the default key character map
175 * is missing from the system.
176 */
177 public static KeyCharacterMap load(int deviceId) {
178 synchronized (sInstances) {
179 KeyCharacterMap map = sInstances.get(deviceId);
180 if (map == null) {
181 int ptr = nativeLoad(deviceId); // might throw
182 map = new KeyCharacterMap(deviceId, ptr);
183 sInstances.put(deviceId, map);
184 }
185 return map;
186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188
189 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800190 * Gets the Unicode character generated by the specified key and meta
191 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <p>
193 * Returns the Unicode character that the specified key would produce
194 * when the specified meta bits (see {@link MetaKeyKeyListener})
195 * were active.
196 * </p><p>
197 * Returns 0 if the key is not one that is used to type Unicode
198 * characters.
199 * </p><p>
200 * If the return value has bit {@link #COMBINING_ACCENT} set, the
201 * key is a "dead key" that should be combined with another to
202 * actually produce a character -- see {@link #getDeadChar} --
203 * after masking with {@link #COMBINING_ACCENT_MASK}.
204 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800205 *
206 * @param keyCode The key code.
207 * @param metaState The meta key modifier state.
208 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800210 public int get(int keyCode, int metaState) {
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800211 metaState = KeyEvent.normalizeMetaState(metaState);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800212 char ch = nativeGetCharacter(mPtr, keyCode, metaState);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800213
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800214 int map = COMBINING.get(ch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 if (map != 0) {
216 return map;
217 } else {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800218 return ch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
220 }
221
222 /**
Jeff Brown49ed71d2010-12-06 17:13:33 -0800223 * Gets the fallback action to perform if the application does not
224 * handle the specified key.
225 * <p>
226 * When an application does not handle a particular key, the system may
227 * translate the key to an alternate fallback key (specified in the
228 * fallback action) and dispatch it to the application.
229 * The event containing the fallback key is flagged
230 * with {@link KeyEvent#FLAG_FALLBACK}.
231 * </p>
232 *
233 * @param keyCode The key code.
234 * @param metaState The meta key modifier state.
235 * @param outFallbackAction The fallback action object to populate.
236 * @return True if a fallback action was found, false otherwise.
237 *
238 * @hide
239 */
240 public boolean getFallbackAction(int keyCode, int metaState,
241 FallbackAction outFallbackAction) {
242 if (outFallbackAction == null) {
243 throw new IllegalArgumentException("fallbackAction must not be null");
244 }
245
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800246 metaState = KeyEvent.normalizeMetaState(metaState);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800247 return nativeGetFallbackAction(mPtr, keyCode, metaState, outFallbackAction);
248 }
249
250 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800251 * Gets the number or symbol associated with the key.
252 * <p>
253 * The character value is returned, not the numeric value.
254 * If the key is not a number, but is a symbol, the symbol is retuned.
255 * </p><p>
256 * This method is intended to to support dial pads and other numeric or
257 * symbolic entry on keyboards where certain keys serve dual function
258 * as alphabetic and symbolic keys. This method returns the number
259 * or symbol associated with the key independent of whether the user
260 * has pressed the required modifier.
261 * </p><p>
262 * For example, on one particular keyboard the keys on the top QWERTY row generate
263 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
264 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
265 * so that the user can type numbers without pressing ALT when it makes sense.
266 * </p>
267 *
268 * @param keyCode The key code.
269 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800271 public char getNumber(int keyCode) {
272 return nativeGetNumber(mPtr, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274
275 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800276 * Gets the first character in the character array that can be generated
277 * by the specified key code.
278 * <p>
279 * This is a convenience function that returns the same value as
280 * {@link #getMatch(int,char[],int) getMatch(keyCode, chars, 0)}.
281 * </p>
282 *
283 * @param keyCode The keycode.
284 * @param chars The array of matching characters to consider.
285 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800287 public char getMatch(int keyCode, char[] chars) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 return getMatch(keyCode, chars, 0);
289 }
290
291 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800292 * Gets the first character in the character array that can be generated
293 * by the specified key code. If there are multiple choices, prefers
294 * the one that would be generated with the specified meta key modifier state.
295 *
296 * @param keyCode The key code.
297 * @param chars The array of matching characters to consider.
298 * @param metaState The preferred meta key modifier state.
299 * @return The matching associated character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800301 public char getMatch(int keyCode, char[] chars, int metaState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 if (chars == null) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800303 throw new IllegalArgumentException("chars must not be null.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
Jeff Brown49ed71d2010-12-06 17:13:33 -0800305
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800306 metaState = KeyEvent.normalizeMetaState(metaState);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800307 return nativeGetMatch(mPtr, keyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 }
309
310 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800311 * Gets the primary character for this key.
312 * In other words, the label that is physically printed on it.
313 *
314 * @param keyCode The key code.
315 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800317 public char getDisplayLabel(int keyCode) {
318 return nativeGetDisplayLabel(mPtr, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
320
321 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800322 * Get the character that is produced by putting accent on the character c.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 * For example, getDeadChar('`', 'e') returns &egrave;.
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800324 *
325 * @param accent The accent character. eg. '`'
326 * @param c The basic character.
327 * @return The combined character, or 0 if the characters cannot be combined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800329 public static int getDeadChar(int accent, int c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 return DEAD.get((accent << 16) | c);
331 }
332
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800333 /**
334 * Describes the character mappings associated with a key.
335 *
336 * @deprecated instead use {@link KeyCharacterMap#getDisplayLabel(int)},
337 * {@link KeyCharacterMap#getNumber(int)} and {@link KeyCharacterMap#get(int, int)}.
338 */
339 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 public static class KeyData {
341 public static final int META_LENGTH = 4;
342
343 /**
344 * The display label (see {@link #getDisplayLabel}).
345 */
346 public char displayLabel;
347 /**
348 * The "number" value (see {@link #getNumber}).
349 */
350 public char number;
351 /**
352 * The character that will be generated in various meta states
353 * (the same ones used for {@link #get} and defined as
354 * {@link KeyEvent#META_SHIFT_ON} and {@link KeyEvent#META_ALT_ON}).
355 * <table>
356 * <tr><th>Index</th><th align="left">Value</th></tr>
357 * <tr><td>0</td><td>no modifiers</td></tr>
358 * <tr><td>1</td><td>caps</td></tr>
359 * <tr><td>2</td><td>alt</td></tr>
360 * <tr><td>3</td><td>caps + alt</td></tr>
361 * </table>
362 */
363 public char[] meta = new char[META_LENGTH];
364 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800367 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800369 * @param keyCode The keyCode to query.
370 * @param results A {@link KeyData} instance that will be filled with the results.
371 * @return True if the key was mapped. If the key was not mapped, results is not modified.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800373 * @deprecated instead use {@link KeyCharacterMap#getDisplayLabel(int)},
374 * {@link KeyCharacterMap#getNumber(int)} or {@link KeyCharacterMap#get(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800376 @Deprecated
377 public boolean getKeyData(int keyCode, KeyData results) {
378 if (results.meta.length < KeyData.META_LENGTH) {
379 throw new IndexOutOfBoundsException(
380 "results.meta.length must be >= " + KeyData.META_LENGTH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800382
383 char displayLabel = nativeGetDisplayLabel(mPtr, keyCode);
384 if (displayLabel == 0) {
385 return false;
386 }
387
388 results.displayLabel = displayLabel;
389 results.number = nativeGetNumber(mPtr, keyCode);
390 results.meta[0] = nativeGetCharacter(mPtr, keyCode, 0);
391 results.meta[1] = nativeGetCharacter(mPtr, keyCode, KeyEvent.META_SHIFT_ON);
392 results.meta[2] = nativeGetCharacter(mPtr, keyCode, KeyEvent.META_ALT_ON);
393 results.meta[3] = nativeGetCharacter(mPtr, keyCode,
394 KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON);
395 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397
398 /**
399 * Get an array of KeyEvent objects that if put into the input stream
400 * could plausibly generate the provided sequence of characters. It is
401 * not guaranteed that the sequence is the only way to generate these
402 * events or that it is optimal.
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800403 * <p>
404 * This function is primarily offered for instrumentation and testing purposes.
405 * It may fail to map characters to key codes. In particular, the key character
406 * map for the {@link #BUILT_IN_KEYBOARD built-in keyboard} device id may be empty.
407 * Consider using the key character map associated with the
408 * {@link #VIRTUAL_KEYBOARD virtual keyboard} device id instead.
409 * </p><p>
410 * For robust text entry, do not use this function. Instead construct a
411 * {@link KeyEvent} with action code {@link KeyEvent#ACTION_MULTIPLE} that contains
412 * the desired string using {@link KeyEvent#KeyEvent(long, String, int, int)}.
413 * </p>
414 *
415 * @param chars The sequence of characters to generate.
416 * @return An array of {@link KeyEvent} objects, or null if the given char array
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * can not be generated using the current key character map.
418 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800419 public KeyEvent[] getEvents(char[] chars) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 if (chars == null) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800421 throw new IllegalArgumentException("chars must not be null.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800423 return nativeGetEvents(mPtr, mDeviceId, chars);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425
426 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800427 * Returns true if the specified key produces a glyph.
428 *
429 * @param keyCode The key code.
430 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800432 public boolean isPrintingKey(int keyCode) {
433 int type = Character.getType(nativeGetDisplayLabel(mPtr, keyCode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
435 switch (type)
436 {
437 case Character.SPACE_SEPARATOR:
438 case Character.LINE_SEPARATOR:
439 case Character.PARAGRAPH_SEPARATOR:
440 case Character.CONTROL:
441 case Character.FORMAT:
442 return false;
443 default:
444 return true;
445 }
446 }
447
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800448 /**
449 * Gets the keyboard type.
450 * Returns {@link #NUMERIC}, {@link #PREDICTIVE}, {@link #ALPHA} or {@link #FULL}.
451 * <p>
452 * Different keyboard types have different semantics. Refer to the documentation
453 * associated with the keyboard type constants for details.
454 * </p>
455 *
456 * @return The keyboard type.
457 */
458 public int getKeyboardType() {
459 return nativeGetKeyboardType(mPtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461
462 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800463 * Gets a constant that describes the behavior of this keyboard's modifier keys
464 * such as {@link KeyEvent#KEYCODE_SHIFT_LEFT}.
465 * <p>
466 * Currently there are two behaviors that may be combined:
467 * </p>
468 * <ul>
469 * <li>Chorded behavior: When the modifier key is pressed together with one or more
470 * character keys, the keyboard inserts the modified keys and
471 * then resets the modifier state when the modifier key is released.</li>
472 * <li>Toggled behavior: When the modifier key is pressed and released on its own
473 * it first toggles into a latched state. When latched, the modifier will apply
474 * to next character key that is pressed and will then reset itself to the initial state.
475 * If the modifier is already latched and the modifier key is pressed and release on
476 * its own again, then it toggles into a locked state. When locked, the modifier will
477 * apply to all subsequent character keys that are pressed until unlocked by pressing
478 * the modifier key on its own one more time to reset it to the initial state.
479 * Toggled behavior is useful for small profile keyboards designed for thumb typing.
480 * </ul>
481 * <p>
482 * This function currently returns {@link #MODIFIER_BEHAVIOR_CHORDED} when the
483 * {@link #getKeyboardType() keyboard type} is {@link #FULL} or {@link #SPECIAL_FUNCTION} and
484 * {@link #MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED} otherwise.
485 * In the future, the function may also take into account global keyboard
486 * accessibility settings, other user preferences, or new device capabilities.
487 * </p>
488 *
489 * @return The modifier behavior for this keyboard.
490 *
491 * @see {@link #MODIFIER_BEHAVIOR_CHORDED}
492 * @see {@link #MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800494 public int getModifierBehavior() {
495 switch (getKeyboardType()) {
496 case FULL:
497 case SPECIAL_FUNCTION:
498 return MODIFIER_BEHAVIOR_CHORDED;
499 default:
500 return MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED;
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
504 /**
505 * Queries the framework about whether any physical keys exist on the
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800506 * any keyboard attached to the device that are capable of producing the given key code.
507 *
508 * @param keyCode The key code to query.
509 * @return True if at least one attached keyboard supports the specified key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 */
511 public static boolean deviceHasKey(int keyCode) {
512 int[] codeArray = new int[1];
513 codeArray[0] = keyCode;
514 boolean[] ret = deviceHasKeys(codeArray);
515 return ret[0];
516 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800517
518 /**
519 * Queries the framework about whether any physical keys exist on the
520 * any keyboard attached to the device that are capable of producing the given
521 * array of key codes.
522 *
523 * @param keyCodes The array of key codes to query.
524 * @return A new array of the same size as the key codes array whose elements
525 * are set to true if at least one attached keyboard supports the corresponding key code
526 * at the same index in the key codes array.
527 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 public static boolean[] deviceHasKeys(int[] keyCodes) {
529 boolean[] ret = new boolean[keyCodes.length];
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700530 IWindowManager wm = Display.getWindowManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 try {
532 wm.hasKeys(keyCodes, ret);
533 } catch (RemoteException e) {
534 // no fallback; just return the empty array
535 }
536 return ret;
537 }
538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 /**
540 * Maps Unicode combining diacritical to display-form dead key
541 * (display character shifted left 16 bits).
542 */
543 private static SparseIntArray COMBINING = new SparseIntArray();
544
545 /**
546 * Maps combinations of (display-form) dead key and second character
547 * to combined output character.
548 */
549 private static SparseIntArray DEAD = new SparseIntArray();
550
551 /*
552 * TODO: Change the table format to support full 21-bit-wide
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800553 * accent characters and combined characters if ever necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 private static final int ACUTE = '\u00B4' << 16;
556 private static final int GRAVE = '`' << 16;
557 private static final int CIRCUMFLEX = '^' << 16;
558 private static final int TILDE = '~' << 16;
559 private static final int UMLAUT = '\u00A8' << 16;
560
561 /*
562 * This bit will be set in the return value of {@link #get(int, int)} if the
563 * key is a "dead key."
564 */
565 public static final int COMBINING_ACCENT = 0x80000000;
566 /**
567 * Mask the return value from {@link #get(int, int)} with this value to get
568 * a printable representation of the accent character of a "dead key."
569 */
570 public static final int COMBINING_ACCENT_MASK = 0x7FFFFFFF;
571
572 static {
573 COMBINING.put('\u0300', (GRAVE >> 16) | COMBINING_ACCENT);
574 COMBINING.put('\u0301', (ACUTE >> 16) | COMBINING_ACCENT);
575 COMBINING.put('\u0302', (CIRCUMFLEX >> 16) | COMBINING_ACCENT);
576 COMBINING.put('\u0303', (TILDE >> 16) | COMBINING_ACCENT);
577 COMBINING.put('\u0308', (UMLAUT >> 16) | COMBINING_ACCENT);
578
579 DEAD.put(ACUTE | 'A', '\u00C1');
580 DEAD.put(ACUTE | 'C', '\u0106');
581 DEAD.put(ACUTE | 'E', '\u00C9');
582 DEAD.put(ACUTE | 'G', '\u01F4');
583 DEAD.put(ACUTE | 'I', '\u00CD');
584 DEAD.put(ACUTE | 'K', '\u1E30');
585 DEAD.put(ACUTE | 'L', '\u0139');
586 DEAD.put(ACUTE | 'M', '\u1E3E');
587 DEAD.put(ACUTE | 'N', '\u0143');
588 DEAD.put(ACUTE | 'O', '\u00D3');
589 DEAD.put(ACUTE | 'P', '\u1E54');
590 DEAD.put(ACUTE | 'R', '\u0154');
591 DEAD.put(ACUTE | 'S', '\u015A');
592 DEAD.put(ACUTE | 'U', '\u00DA');
593 DEAD.put(ACUTE | 'W', '\u1E82');
594 DEAD.put(ACUTE | 'Y', '\u00DD');
595 DEAD.put(ACUTE | 'Z', '\u0179');
596 DEAD.put(ACUTE | 'a', '\u00E1');
597 DEAD.put(ACUTE | 'c', '\u0107');
598 DEAD.put(ACUTE | 'e', '\u00E9');
599 DEAD.put(ACUTE | 'g', '\u01F5');
600 DEAD.put(ACUTE | 'i', '\u00ED');
601 DEAD.put(ACUTE | 'k', '\u1E31');
602 DEAD.put(ACUTE | 'l', '\u013A');
603 DEAD.put(ACUTE | 'm', '\u1E3F');
604 DEAD.put(ACUTE | 'n', '\u0144');
605 DEAD.put(ACUTE | 'o', '\u00F3');
606 DEAD.put(ACUTE | 'p', '\u1E55');
607 DEAD.put(ACUTE | 'r', '\u0155');
608 DEAD.put(ACUTE | 's', '\u015B');
609 DEAD.put(ACUTE | 'u', '\u00FA');
610 DEAD.put(ACUTE | 'w', '\u1E83');
611 DEAD.put(ACUTE | 'y', '\u00FD');
612 DEAD.put(ACUTE | 'z', '\u017A');
613 DEAD.put(CIRCUMFLEX | 'A', '\u00C2');
614 DEAD.put(CIRCUMFLEX | 'C', '\u0108');
615 DEAD.put(CIRCUMFLEX | 'E', '\u00CA');
616 DEAD.put(CIRCUMFLEX | 'G', '\u011C');
617 DEAD.put(CIRCUMFLEX | 'H', '\u0124');
618 DEAD.put(CIRCUMFLEX | 'I', '\u00CE');
619 DEAD.put(CIRCUMFLEX | 'J', '\u0134');
620 DEAD.put(CIRCUMFLEX | 'O', '\u00D4');
621 DEAD.put(CIRCUMFLEX | 'S', '\u015C');
622 DEAD.put(CIRCUMFLEX | 'U', '\u00DB');
623 DEAD.put(CIRCUMFLEX | 'W', '\u0174');
624 DEAD.put(CIRCUMFLEX | 'Y', '\u0176');
625 DEAD.put(CIRCUMFLEX | 'Z', '\u1E90');
626 DEAD.put(CIRCUMFLEX | 'a', '\u00E2');
627 DEAD.put(CIRCUMFLEX | 'c', '\u0109');
628 DEAD.put(CIRCUMFLEX | 'e', '\u00EA');
629 DEAD.put(CIRCUMFLEX | 'g', '\u011D');
630 DEAD.put(CIRCUMFLEX | 'h', '\u0125');
631 DEAD.put(CIRCUMFLEX | 'i', '\u00EE');
632 DEAD.put(CIRCUMFLEX | 'j', '\u0135');
633 DEAD.put(CIRCUMFLEX | 'o', '\u00F4');
634 DEAD.put(CIRCUMFLEX | 's', '\u015D');
635 DEAD.put(CIRCUMFLEX | 'u', '\u00FB');
636 DEAD.put(CIRCUMFLEX | 'w', '\u0175');
637 DEAD.put(CIRCUMFLEX | 'y', '\u0177');
638 DEAD.put(CIRCUMFLEX | 'z', '\u1E91');
639 DEAD.put(GRAVE | 'A', '\u00C0');
640 DEAD.put(GRAVE | 'E', '\u00C8');
641 DEAD.put(GRAVE | 'I', '\u00CC');
642 DEAD.put(GRAVE | 'N', '\u01F8');
643 DEAD.put(GRAVE | 'O', '\u00D2');
644 DEAD.put(GRAVE | 'U', '\u00D9');
645 DEAD.put(GRAVE | 'W', '\u1E80');
646 DEAD.put(GRAVE | 'Y', '\u1EF2');
647 DEAD.put(GRAVE | 'a', '\u00E0');
648 DEAD.put(GRAVE | 'e', '\u00E8');
649 DEAD.put(GRAVE | 'i', '\u00EC');
650 DEAD.put(GRAVE | 'n', '\u01F9');
651 DEAD.put(GRAVE | 'o', '\u00F2');
652 DEAD.put(GRAVE | 'u', '\u00F9');
653 DEAD.put(GRAVE | 'w', '\u1E81');
654 DEAD.put(GRAVE | 'y', '\u1EF3');
655 DEAD.put(TILDE | 'A', '\u00C3');
656 DEAD.put(TILDE | 'E', '\u1EBC');
657 DEAD.put(TILDE | 'I', '\u0128');
658 DEAD.put(TILDE | 'N', '\u00D1');
659 DEAD.put(TILDE | 'O', '\u00D5');
660 DEAD.put(TILDE | 'U', '\u0168');
661 DEAD.put(TILDE | 'V', '\u1E7C');
662 DEAD.put(TILDE | 'Y', '\u1EF8');
663 DEAD.put(TILDE | 'a', '\u00E3');
664 DEAD.put(TILDE | 'e', '\u1EBD');
665 DEAD.put(TILDE | 'i', '\u0129');
666 DEAD.put(TILDE | 'n', '\u00F1');
667 DEAD.put(TILDE | 'o', '\u00F5');
668 DEAD.put(TILDE | 'u', '\u0169');
669 DEAD.put(TILDE | 'v', '\u1E7D');
670 DEAD.put(TILDE | 'y', '\u1EF9');
671 DEAD.put(UMLAUT | 'A', '\u00C4');
672 DEAD.put(UMLAUT | 'E', '\u00CB');
673 DEAD.put(UMLAUT | 'H', '\u1E26');
674 DEAD.put(UMLAUT | 'I', '\u00CF');
675 DEAD.put(UMLAUT | 'O', '\u00D6');
676 DEAD.put(UMLAUT | 'U', '\u00DC');
677 DEAD.put(UMLAUT | 'W', '\u1E84');
678 DEAD.put(UMLAUT | 'X', '\u1E8C');
679 DEAD.put(UMLAUT | 'Y', '\u0178');
680 DEAD.put(UMLAUT | 'a', '\u00E4');
681 DEAD.put(UMLAUT | 'e', '\u00EB');
682 DEAD.put(UMLAUT | 'h', '\u1E27');
683 DEAD.put(UMLAUT | 'i', '\u00EF');
684 DEAD.put(UMLAUT | 'o', '\u00F6');
685 DEAD.put(UMLAUT | 't', '\u1E97');
686 DEAD.put(UMLAUT | 'u', '\u00FC');
687 DEAD.put(UMLAUT | 'w', '\u1E85');
688 DEAD.put(UMLAUT | 'x', '\u1E8D');
689 DEAD.put(UMLAUT | 'y', '\u00FF');
690 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800691
692 /**
693 * Thrown by {@link KeyCharacterMap#load} when a key character map could not be loaded.
694 */
Jeff Brown7e1e21f2011-01-19 17:05:01 -0800695 public static class UnavailableException extends AndroidRuntimeException {
696 public UnavailableException(String msg) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800697 super(msg);
698 }
699 }
Jeff Brown49ed71d2010-12-06 17:13:33 -0800700
701 /**
702 * Specifies a substitute key code and meta state as a fallback action
703 * for an unhandled key.
704 * @hide
705 */
706 public static final class FallbackAction {
707 public int keyCode;
708 public int metaState;
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710}