2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _UI_KEY_CHARACTER_MAP_H
18 #define _UI_KEY_CHARACTER_MAP_H
23 #include <utils/Errors.h>
24 #include <utils/KeyedVector.h>
25 #include <utils/Tokenizer.h>
26 #include <utils/String8.h>
27 #include <utils/Unicode.h>
32 * Describes a mapping from Android key codes to characters.
33 * Also specifies other functions of the keyboard such as the keyboard type
34 * and key modifier semantics.
36 class KeyCharacterMap {
39 KEYBOARD_TYPE_UNKNOWN = 0,
40 KEYBOARD_TYPE_NUMERIC = 1,
41 KEYBOARD_TYPE_PREDICTIVE = 2,
42 KEYBOARD_TYPE_ALPHA = 3,
43 KEYBOARD_TYPE_FULL = 4,
44 KEYBOARD_TYPE_SPECIAL_FUNCTION = 5,
47 // Substitute key code and meta state for fallback action.
48 struct FallbackAction {
55 static status_t load(const String8& filename, KeyCharacterMap** outMap);
56 static status_t loadByDeviceId(int32_t deviceId, KeyCharacterMap** outMap);
58 /* Gets the keyboard type. */
59 int32_t getKeyboardType() const;
61 /* Gets the primary character for this key as in the label physically printed on it.
62 * Returns 0 if none (eg. for non-printing keys). */
63 char16_t getDisplayLabel(int32_t keyCode) const;
65 /* Gets the Unicode character for the number or symbol generated by the key
66 * when the keyboard is used as a dialing pad.
67 * Returns 0 if no number or symbol is generated.
69 char16_t getNumber(int32_t keyCode) const;
71 /* Gets the Unicode character generated by the key and meta key modifiers.
72 * Returns 0 if no character is generated.
74 char16_t getCharacter(int32_t keyCode, int32_t metaState) const;
76 /* Gets the fallback action to use by default if the application does not
77 * handle the specified key.
78 * Returns true if an action was available, false if none.
80 bool getFallbackAction(int32_t keyCode, int32_t metaState,
81 FallbackAction* outFallbackAction) const;
83 /* Gets the first matching Unicode character that can be generated by the key,
84 * preferring the one with the specified meta key modifiers.
85 * Returns 0 if no matching character is generated.
87 char16_t getMatch(int32_t keyCode, const char16_t* chars,
88 size_t numChars, int32_t metaState) const;
90 /* Gets a sequence of key events that could plausibly generate the specified
91 * character sequence. Returns false if some of the characters cannot be generated.
93 bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars,
94 Vector<KeyEvent>& outEvents) const;
100 /* The next behavior in the list, or NULL if none. */
103 /* The meta key modifiers for this behavior. */
106 /* The character to insert. */
109 /* The fallback keycode if the key is not handled. */
110 int32_t fallbackKeyCode;
117 /* The single character label printed on the key, or 0 if none. */
120 /* The number or symbol character generated by the key, or 0 if none. */
123 /* The list of key behaviors sorted from most specific to least specific
124 * meta key binding. */
125 Behavior* firstBehavior;
141 inline Property(int32_t property = 0, int32_t metaState = 0) :
142 property(property), metaState(metaState) { }
148 KeyCharacterMap* mMap;
149 Tokenizer* mTokenizer;
154 Parser(KeyCharacterMap* map, Tokenizer* tokenizer);
159 status_t parseType();
161 status_t parseKeyProperty();
162 status_t parseModifier(const String8& token, int32_t* outMetaState);
163 status_t parseCharacterLiteral(char16_t* outCharacter);
166 KeyedVector<int32_t, Key*> mKeys;
171 bool getKey(int32_t keyCode, const Key** outKey) const;
172 bool getKeyBehavior(int32_t keyCode, int32_t metaState,
173 const Key** outKey, const Behavior** outBehavior) const;
175 bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;
177 static void addKey(Vector<KeyEvent>& outEvents,
178 int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
179 static void addMetaKeys(Vector<KeyEvent>& outEvents,
180 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
181 int32_t* currentMetaState);
182 static bool addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
183 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
184 int32_t keyCode, int32_t keyMetaState,
185 int32_t* currentMetaState);
186 static void addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents,
187 int32_t deviceId, int32_t metaState, bool down, nsecs_t time,
188 int32_t leftKeyCode, int32_t leftKeyMetaState,
189 int32_t rightKeyCode, int32_t rightKeyMetaState,
190 int32_t eitherKeyMetaState,
191 int32_t* currentMetaState);
192 static void addLockedMetaKey(Vector<KeyEvent>& outEvents,
193 int32_t deviceId, int32_t metaState, nsecs_t time,
194 int32_t keyCode, int32_t keyMetaState,
195 int32_t* currentMetaState);
198 } // namespace android
200 #endif // _UI_KEY_CHARACTER_MAP_H