Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: ui/events/keycodes/keyboard_code_conversion.cc

Issue 742103002: Ozone keyboard layout (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lite-code
Patch Set: reeeeebase Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/events/keycodes/keyboard_code_conversion.h" 5 #include "ui/events/keycodes/keyboard_code_conversion.h"
6 6
7 #include "ui/events/event_constants.h" 7 #include "ui/events/event_constants.h"
8 #include "ui/events/keycodes/dom3/dom_key.h"
8 9
9 namespace ui { 10 namespace ui {
10 11
11 uint16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) { 12 namespace {
13
14 // This table maps a subset of |KeyboardCode| (VKEYs) to DomKey and character.
15 // Only values not otherwise handled by GetMeaningFromKeyCode() are here.
16 const struct KeyboardCodeToMeaning {
17 KeyboardCode key_code;
18 DomKey key;
19 base::char16 plain_character;
20 base::char16 shift_character;
21 } kKeyboardCodeToMeaning[] = {
22 {VKEY_BACK, DomKey::BACKSPACE, '\b', 0},
23 {VKEY_TAB, DomKey::TAB, '\t', 0},
24 {VKEY_RETURN, DomKey::ENTER, '\r', 0},
25 {VKEY_ESCAPE, DomKey::ESCAPE, 0x1B, 0},
26 {VKEY_SPACE, DomKey::CHARACTER, ' ', 0},
27 {VKEY_MULTIPLY, DomKey::CHARACTER, '*', 0},
28 {VKEY_ADD, DomKey::CHARACTER, '+', 0},
29 {VKEY_SEPARATOR, DomKey::CHARACTER, ',', 0},
30 {VKEY_SUBTRACT, DomKey::CHARACTER, '-', 0},
31 {VKEY_DECIMAL, DomKey::CHARACTER, '.', 0},
32 {VKEY_DIVIDE, DomKey::CHARACTER, '/', 0},
33 {VKEY_OEM_1, DomKey::CHARACTER, ';', ':'},
34 {VKEY_OEM_PLUS, DomKey::CHARACTER, '=', '+'},
35 {VKEY_OEM_COMMA, DomKey::CHARACTER, ',', '<'},
36 {VKEY_OEM_MINUS, DomKey::CHARACTER, '-', '_'},
37 {VKEY_OEM_PERIOD, DomKey::CHARACTER, '.', '>'},
38 {VKEY_OEM_2, DomKey::CHARACTER, '/', '?'},
39 {VKEY_OEM_3, DomKey::CHARACTER, '`', '~'},
40 {VKEY_OEM_4, DomKey::CHARACTER, '[', '{'},
41 {VKEY_OEM_5, DomKey::CHARACTER, '\\', '|'},
42 {VKEY_OEM_6, DomKey::CHARACTER, ']', '}'},
43 {VKEY_OEM_7, DomKey::CHARACTER, '\'', '"'},
44 {VKEY_OEM_102, DomKey::CHARACTER, '<', '>'},
45 {VKEY_CLEAR, DomKey::CLEAR, 0, 0},
46 {VKEY_SHIFT, DomKey::SHIFT, 0, 0},
47 {VKEY_CONTROL, DomKey::CONTROL, 0, 0},
48 {VKEY_MENU, DomKey::ALT, 0, 0},
49 {VKEY_PAUSE, DomKey::PAUSE, 0, 0},
50 {VKEY_CAPITAL, DomKey::CAPS_LOCK, 0, 0},
51 // Windows conflates 'KanaMode' and 'HangulMode'.
52 {VKEY_KANA, DomKey::KANA_MODE, 0, 0},
53 {VKEY_JUNJA, DomKey::JUNJA_MODE, 0, 0},
54 {VKEY_FINAL, DomKey::FINAL_MODE, 0, 0},
55 // Windows conflates 'HanjaMode' and 'KanjiMode'.
56 {VKEY_HANJA, DomKey::HANJA_MODE, 0, 0},
57 {VKEY_CONVERT, DomKey::CONVERT, 0, 0},
58 {VKEY_NONCONVERT, DomKey::NON_CONVERT, 0, 0},
59 {VKEY_ACCEPT, DomKey::ACCEPT, 0, 0},
60 {VKEY_MODECHANGE, DomKey::MODE_CHANGE, 0, 0},
61 {VKEY_PRIOR, DomKey::PAGE_UP, 0, 0},
62 {VKEY_NEXT, DomKey::PAGE_DOWN, 0, 0},
63 {VKEY_END, DomKey::END, 0, 0},
64 {VKEY_HOME, DomKey::HOME, 0, 0},
65 {VKEY_LEFT, DomKey::ARROW_LEFT, 0, 0},
66 {VKEY_UP, DomKey::ARROW_UP, 0, 0},
67 {VKEY_RIGHT, DomKey::ARROW_RIGHT, 0, 0},
68 {VKEY_DOWN, DomKey::ARROW_DOWN, 0, 0},
69 {VKEY_SELECT, DomKey::SELECT, 0, 0},
70 {VKEY_PRINT, DomKey::PRINT, 0, 0},
71 {VKEY_EXECUTE, DomKey::EXECUTE, 0, 0},
72 {VKEY_SNAPSHOT, DomKey::PRINT_SCREEN, 0, 0},
73 {VKEY_INSERT, DomKey::INSERT, 0, 0},
74 {VKEY_DELETE, DomKey::DEL, 0, 0},
75 {VKEY_HELP, DomKey::HELP, 0, 0},
76 {VKEY_LWIN, DomKey::OS, 0, 0},
77 {VKEY_RWIN, DomKey::OS, 0, 0},
78 {VKEY_APPS, DomKey::MEDIA_APPS, 0, 0},
79 {VKEY_NUMLOCK, DomKey::NUM_LOCK, 0, 0},
80 {VKEY_SCROLL, DomKey::SCROLL_LOCK, 0, 0},
81 {VKEY_LSHIFT, DomKey::SHIFT, 0, 0},
82 {VKEY_RSHIFT, DomKey::SHIFT, 0, 0},
83 {VKEY_LCONTROL, DomKey::CONTROL, 0, 0},
84 {VKEY_RCONTROL, DomKey::CONTROL, 0, 0},
85 {VKEY_LMENU, DomKey::ALT, 0, 0},
86 {VKEY_RMENU, DomKey::ALT, 0, 0},
87 {VKEY_BROWSER_BACK, DomKey::BROWSER_BACK, 0, 0},
88 {VKEY_BROWSER_FORWARD, DomKey::BROWSER_FORWARD, 0, 0},
89 {VKEY_BROWSER_REFRESH, DomKey::BROWSER_REFRESH, 0, 0},
90 {VKEY_BROWSER_STOP, DomKey::BROWSER_STOP, 0, 0},
91 {VKEY_BROWSER_SEARCH, DomKey::BROWSER_SEARCH, 0, 0},
92 {VKEY_BROWSER_FAVORITES, DomKey::BROWSER_FAVORITES, 0, 0},
93 {VKEY_BROWSER_HOME, DomKey::BROWSER_HOME, 0, 0},
94 {VKEY_VOLUME_MUTE, DomKey::VOLUME_MUTE, 0, 0},
95 {VKEY_VOLUME_DOWN, DomKey::VOLUME_DOWN, 0, 0},
96 {VKEY_VOLUME_UP, DomKey::VOLUME_UP, 0, 0},
97 {VKEY_MEDIA_NEXT_TRACK, DomKey::MEDIA_TRACK_NEXT, 0, 0},
98 {VKEY_MEDIA_PREV_TRACK, DomKey::MEDIA_TRACK_PREVIOUS, 0, 0},
99 {VKEY_MEDIA_STOP, DomKey::MEDIA_STOP, 0, 0},
100 {VKEY_MEDIA_PLAY_PAUSE, DomKey::MEDIA_PLAY_PAUSE, 0, 0},
101 {VKEY_MEDIA_LAUNCH_MAIL, DomKey::LAUNCH_MAIL, 0, 0},
102 {VKEY_MEDIA_LAUNCH_MEDIA_SELECT, DomKey::LAUNCH_MEDIA_PLAYER, 0, 0},
103 {VKEY_MEDIA_LAUNCH_APP1, DomKey::LAUNCH_MY_COMPUTER, 0, 0},
104 {VKEY_MEDIA_LAUNCH_APP2, DomKey::LAUNCH_CALCULATOR, 0, 0},
105 {VKEY_OEM_8, DomKey::SUPER, 0, 0}, // ISO Level 5 Shift in ChromeOS
106 {VKEY_PROCESSKEY, DomKey::PROCESS, 0, 0},
107 {VKEY_DBE_SBCSCHAR, DomKey::HANKAKU, 0, 0},
108 {VKEY_DBE_DBCSCHAR, DomKey::ZENKAKU, 0, 0},
109 {VKEY_ATTN, DomKey::ATTN, 0, 0},
110 {VKEY_CRSEL, DomKey::CR_SEL, 0, 0},
111 {VKEY_EXSEL, DomKey::EX_SEL, 0, 0},
112 {VKEY_EREOF, DomKey::ERASE_EOF, 0, 0},
113 {VKEY_PLAY, DomKey::MEDIA_PLAY, 0, 0},
114 {VKEY_ZOOM, DomKey::ZOOM_TOGGLE, 0, 0},
115 {VKEY_OEM_CLEAR, DomKey::CLEAR, 0, 0},
116 {VKEY_ALTGR, DomKey::ALT_GRAPH, 0, 0},
117 #if defined(OS_POSIX)
118 {VKEY_POWER, DomKey::POWER, 0, 0},
119 {VKEY_BRIGHTNESS_DOWN, DomKey::BRIGHTNESS_DOWN, 0, 0},
120 {VKEY_BRIGHTNESS_UP, DomKey::BRIGHTNESS_UP, 0, 0},
121 {VKEY_COMPOSE, DomKey::COMPOSE, 0, 0},
122 {VKEY_OEM_103, DomKey::MEDIA_REWIND, 0, 0},
123 {VKEY_OEM_104, DomKey::MEDIA_FAST_FORWARD, 0, 0},
124 #endif
125 };
126
127 } // anonymous namespace
128
129 base::char16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) {
130 ui::DomKey dom_key;
131 base::char16 character;
132 if (GetMeaningFromKeyCode(key_code, flags, &dom_key, &character))
133 return character;
134 return 0;
135 }
136
137 bool GetMeaningFromKeyCode(KeyboardCode key_code,
138 int flags,
139 DomKey* dom_key,
140 base::char16* character) {
12 const bool ctrl = (flags & EF_CONTROL_DOWN) != 0; 141 const bool ctrl = (flags & EF_CONTROL_DOWN) != 0;
13 const bool shift = (flags & EF_SHIFT_DOWN) != 0; 142 const bool shift = (flags & EF_SHIFT_DOWN) != 0;
14 const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0); 143 const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0);
15 144
16 // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A. 145 // Control characters.
17 if (key_code >= VKEY_A && key_code <= VKEY_Z) {
18 return static_cast<uint16>
19 (key_code - VKEY_A + (ctrl ? 1 : (upper ? 'A' : 'a')));
20 }
21
22 // Other ctrl characters
23 if (ctrl) { 146 if (ctrl) {
147 // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A.
148 if (key_code >= VKEY_A && key_code <= VKEY_Z) {
149 *character = static_cast<uint16>(key_code - VKEY_A + 1);
150 switch (key_code) {
151 case VKEY_H:
152 *dom_key = DomKey::BACKSPACE;
153 break;
154 case VKEY_I:
155 *dom_key = DomKey::TAB;
156 break;
157 case VKEY_J:
158 case VKEY_M:
159 *dom_key = DomKey::ENTER;
160 break;
161 default:
162 *dom_key = DomKey::CHARACTER;
163 break;
164 }
165 return true;
166 }
167 // Other control characters.
24 if (shift) { 168 if (shift) {
25 // following graphics chars require shift key to input. 169 // The following graphics characters require the shift key to input.
26 switch (key_code) { 170 switch (key_code) {
27 // ctrl-@ maps to \x00 (Null byte) 171 // ctrl-@ maps to \x00 (Null byte)
28 case VKEY_2: 172 case VKEY_2:
29 return 0; 173 *dom_key = DomKey::CHARACTER;
174 *character = 0;
175 return true;
30 // ctrl-^ maps to \x1E (Record separator, Information separator two) 176 // ctrl-^ maps to \x1E (Record separator, Information separator two)
31 case VKEY_6: 177 case VKEY_6:
32 return 0x1E; 178 *dom_key = DomKey::CHARACTER;
179 *character = 0x1E;
180 return true;
33 // ctrl-_ maps to \x1F (Unit separator, Information separator one) 181 // ctrl-_ maps to \x1F (Unit separator, Information separator one)
34 case VKEY_OEM_MINUS: 182 case VKEY_OEM_MINUS:
35 return 0x1F; 183 *dom_key = DomKey::CHARACTER;
184 *character = 0x1F;
185 return true;
36 // Returns 0 for all other keys to avoid inputting unexpected chars. 186 // Returns 0 for all other keys to avoid inputting unexpected chars.
37 default: 187 default:
38 return 0; 188 *dom_key = DomKey::UNIDENTIFIED;
189 *character = 0;
190 return false;
39 } 191 }
40 } else { 192 } else {
41 switch (key_code) { 193 switch (key_code) {
42 // ctrl-[ maps to \x1B (Escape) 194 // ctrl-[ maps to \x1B (Escape)
43 case VKEY_OEM_4: 195 case VKEY_OEM_4:
44 return 0x1B; 196 *dom_key = DomKey::ESCAPE;
197 *character = 0x1B;
198 return true;
45 // ctrl-\ maps to \x1C (File separator, Information separator four) 199 // ctrl-\ maps to \x1C (File separator, Information separator four)
46 case VKEY_OEM_5: 200 case VKEY_OEM_5:
47 return 0x1C; 201 *dom_key = DomKey::CHARACTER;
202 *character = 0x1C;
203 return true;
48 // ctrl-] maps to \x1D (Group separator, Information separator three) 204 // ctrl-] maps to \x1D (Group separator, Information separator three)
49 case VKEY_OEM_6: 205 case VKEY_OEM_6:
50 return 0x1D; 206 *dom_key = DomKey::CHARACTER;
207 *character = 0x1D;
208 return true;
51 // ctrl-Enter maps to \x0A (Line feed) 209 // ctrl-Enter maps to \x0A (Line feed)
52 case VKEY_RETURN: 210 case VKEY_RETURN:
53 return 0x0A; 211 *dom_key = DomKey::CHARACTER;
212 *character = 0x0A;
213 return true;
54 // Returns 0 for all other keys to avoid inputting unexpected chars. 214 // Returns 0 for all other keys to avoid inputting unexpected chars.
55 default: 215 default:
56 return 0; 216 *dom_key = DomKey::UNIDENTIFIED;
217 *character = 0;
218 return false;
57 } 219 }
58 } 220 }
59 } 221 }
60 222
61 // For IME support. 223 // ASCII alphanumeric characters.
62 if (key_code == ui::VKEY_PROCESSKEY) 224 if (key_code >= VKEY_A && key_code <= VKEY_Z) {
63 return 0xE5; 225 *dom_key = DomKey::CHARACTER;
64 226 *character = static_cast<uint16>(key_code - VKEY_A + (upper ? 'A' : 'a'));
65 // Normal characters 227 return true;
228 }
66 if (key_code >= VKEY_0 && key_code <= VKEY_9) { 229 if (key_code >= VKEY_0 && key_code <= VKEY_9) {
67 return shift ? ")!@#$%^&*("[key_code - VKEY_0] : 230 *dom_key = DomKey::CHARACTER;
68 static_cast<uint16>(key_code); 231 *character =
69 } else if (key_code >= VKEY_NUMPAD0 && key_code <= VKEY_NUMPAD9) { 232 shift ? ")!@#$%^&*("[key_code - VKEY_0] : static_cast<uint16>(key_code);
70 return static_cast<uint16>(key_code - VKEY_NUMPAD0 + '0'); 233 return true;
71 } 234 }
72 235 if (key_code >= VKEY_NUMPAD0 && key_code <= VKEY_NUMPAD9) {
73 switch (key_code) { 236 *dom_key = DomKey::CHARACTER;
74 case VKEY_TAB: 237 *character = static_cast<uint16>(key_code - VKEY_NUMPAD0 + '0');
75 return '\t'; 238 return true;
76 case VKEY_RETURN: 239 }
77 return '\r'; 240
78 case VKEY_MULTIPLY: 241 // Function keys.
79 return '*'; 242 if (key_code >= VKEY_F1 && key_code <= VKEY_F24) {
80 case VKEY_ADD: 243 *dom_key =
81 return '+'; 244 static_cast<DomKey>(key_code - VKEY_F1 + static_cast<int>(DomKey::F1));
82 case VKEY_SUBTRACT: 245 *character = 0;
83 return '-'; 246 return true;
84 case VKEY_DECIMAL: 247 }
85 return '.'; 248
86 case VKEY_DIVIDE: 249 // Other keys.
87 return '/'; 250 for (size_t i = 0; i < arraysize(kKeyboardCodeToMeaning); ++i) {
88 case VKEY_SPACE: 251 if (kKeyboardCodeToMeaning[i].key_code == key_code) {
89 return ' '; 252 const KeyboardCodeToMeaning* p = &kKeyboardCodeToMeaning[i];
90 case VKEY_OEM_1: 253 *dom_key = p->key;
91 return shift ? ':' : ';'; 254 *character = (shift && p->shift_character) ? p->shift_character
92 case VKEY_OEM_PLUS: 255 : p->plain_character;
93 return shift ? '+' : '='; 256 return true;
94 case VKEY_OEM_COMMA: 257 }
95 return shift ? '<' : ','; 258 }
96 case VKEY_OEM_MINUS: 259 *dom_key = DomKey::UNIDENTIFIED;
97 return shift ? '_' : '-'; 260 *character = 0;
98 case VKEY_OEM_PERIOD: 261 return false;
99 return shift ? '>' : '.';
100 case VKEY_OEM_2:
101 return shift ? '?' : '/';
102 case VKEY_OEM_3:
103 return shift ? '~' : '`';
104 case VKEY_OEM_4:
105 return shift ? '{' : '[';
106 case VKEY_OEM_5:
107 return shift ? '|' : '\\';
108 case VKEY_OEM_6:
109 return shift ? '}' : ']';
110 case VKEY_OEM_7:
111 return shift ? '"' : '\'';
112 default:
113 return 0;
114 }
115 } 262 }
116 263
117 } // namespace ui 264 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/keycodes/keyboard_code_conversion.h ('k') | ui/events/keycodes/keyboard_codes_posix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698