base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / events / keycodes / keyboard_code_conversion_x.cc
blob48025c436939994b5fe2ebf9186a973182966adb
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
7 #include <algorithm>
9 #define XK_3270 // for XK_3270_BackTab
10 #include <X11/XF86keysym.h>
11 #include <X11/Xlib.h>
12 #include <X11/Xutil.h>
13 #include <X11/extensions/XInput2.h>
14 #include <X11/keysym.h>
16 #include "base/basictypes.h"
17 #include "base/logging.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/sys_string_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "ui/events/keycodes/dom4/keycode_converter.h"
22 #include "ui/events/x/keysym_to_unicode.h"
24 #define VKEY_UNSUPPORTED VKEY_UNKNOWN
26 namespace ui {
28 namespace {
30 // MAP0 - MAP3:
31 // These are the generated VKEY code maps for all possible Latin keyboard
32 // layouts in Windows. And the maps are only for special letter keys excluding
33 // [a-z] & [0-9].
35 // ch0: the keysym without modifier states.
36 // ch1: the keysym with shift state.
37 // ch2: the keysym with altgr state.
38 // sc: the hardware keycode (in Windows, it's called scan code).
39 // vk: the VKEY code.
41 // MAP0: maps from ch0 to vk.
42 // MAP1: maps from ch0+sc to vk.
43 // MAP2: maps from ch0+ch1+sc to vk.
44 // MAP3: maps from ch0+ch1+ch2+sc to vk.
45 // MAP0 - MAP3 are all sorted, so that finding VK can be binary search.
47 // Reason for creating these maps is because a hard-coded mapping in
48 // KeyboardCodeFromXKeysym() doesn't support non-US keyboard layouts.
49 // e.g. in UK keyboard, the key between Quote and Enter keys has the VKEY code
50 // VKEY_OEM_5 instead of VKEY_3.
52 // The key symbols which are not [a-zA-Z0-9] and functional/extend keys (e.g.
53 // TAB, ENTER, BS, Arrow keys, modifier keys, F1-F12, media/app keys, etc.)
54 // should go through these maps for correct VKEY codes.
56 // Please refer to crbug.com/386066.
58 const struct MAP0 {
59 KeySym ch0;
60 uint8 vk;
61 bool operator()(const MAP0& m1, const MAP0& m2) const {
62 return m1.ch0 < m2.ch0;
64 } map0[] = {
65 {0x0025, 0x35}, // XK_percent: VKEY_5
66 {0x0026, 0x31}, // XK_ampersand: VKEY_1
67 {0x003C, 0xDC}, // XK_less: VKEY_OEM_5
68 {0x007B, 0xDE}, // XK_braceleft: VKEY_OEM_7
69 {0x007C, 0xDC}, // XK_bar: VKEY_OEM_5
70 {0x007D, 0xBF}, // XK_braceright: VKEY_OEM_2
71 {0x007E, 0xDC}, // XK_asciitilde: VKEY_OEM_5
72 {0x00A1, 0xDD}, // XK_exclamdown: VKEY_OEM_6
73 {0x00AD, 0xC0}, // XK_hyphen: VKEY_OEM_3
74 {0x00B2, 0xDE}, // XK_twosuperior: VKEY_OEM_7
75 {0x00B5, 0xDC}, // XK_mu: VKEY_OEM_5
76 {0x00BB, 0x39}, // XK_guillemotright: VKEY_9
77 {0x00BD, 0xDC}, // XK_onehalf: VKEY_OEM_5
78 {0x00BF, 0xDD}, // XK_questiondown: VKEY_OEM_6
79 {0x00DF, 0xDB}, // XK_ssharp: VKEY_OEM_4
80 {0x00E5, 0xDD}, // XK_aring: VKEY_OEM_6
81 {0x00EA, 0x33}, // XK_ecircumflex: VKEY_3
82 {0x00EB, 0xBA}, // XK_ediaeresis: VKEY_OEM_1
83 {0x00EC, 0xDD}, // XK_igrave: VKEY_OEM_6
84 {0x00EE, 0xDD}, // XK_icircumflex: VKEY_OEM_6
85 {0x00F1, 0xC0}, // XK_ntilde: VKEY_OEM_3
86 {0x00F2, 0xC0}, // XK_ograve: VKEY_OEM_3
87 {0x00F5, 0xDB}, // XK_otilde: VKEY_OEM_4
88 {0x00F7, 0xDD}, // XK_division: VKEY_OEM_6
89 {0x00FD, 0x37}, // XK_yacute: VKEY_7
90 {0x00FE, 0xBD}, // XK_thorn: VKEY_OEM_MINUS
91 {0x01A1, 0xDD}, // XK_ohorn: VKEY_OEM_6
92 {0x01B0, 0xDB}, // XK_uhorn: VKEY_OEM_4
93 {0x01B5, 0x32}, // XK_lcaron: VKEY_2
94 {0x01B6, 0xDD}, // XK_zstroke: VKEY_OEM_6
95 {0x01BB, 0x35}, // XK_tcaron: VKEY_5
96 {0x01E6, 0xDE}, // XK_cacute: VKEY_OEM_7
97 {0x01EC, 0x32}, // XK_ecaron: VKEY_2
98 {0x01F2, 0xDC}, // XK_ncaron: VKEY_OEM_5
99 {0x01F5, 0xDB}, // XK_odoubleacute: VKEY_OEM_4
100 {0x01F8, 0x35}, // XK_rcaron: VKEY_5
101 {0x01F9, 0xBA}, // XK_uring: VKEY_OEM_1
102 {0x01FB, 0xDC}, // XK_udoubleacute: VKEY_OEM_5
103 {0x01FE, 0xDE}, // XK_tcedilla: VKEY_OEM_7
104 {0x0259, 0xC0}, // XK_schwa: VKEY_OEM_3
105 {0x02B1, 0xDD}, // XK_hstroke: VKEY_OEM_6
106 {0x02B9, 0xBA}, // XK_idotless: VKEY_OEM_1
107 {0x02BB, 0xDD}, // XK_gbreve: VKEY_OEM_6
108 {0x02E5, 0xC0}, // XK_cabovedot: VKEY_OEM_3
109 {0x02F5, 0xDB}, // XK_gabovedot: VKEY_OEM_4
110 {0x03B6, 0xBF}, // XK_lcedilla: VKEY_OEM_2
111 {0x03BA, 0x57}, // XK_emacron: VKEY_W
112 {0x03E0, 0xDF}, // XK_amacron: VKEY_OEM_8
113 {0x03EF, 0xDD}, // XK_imacron: VKEY_OEM_6
114 {0x03F1, 0xDB}, // XK_ncedilla: VKEY_OEM_4
115 {0x03F3, 0xDC}, // XK_kcedilla: VKEY_OEM_5
118 const struct MAP1 {
119 KeySym ch0;
120 unsigned sc;
121 uint8 vk;
122 bool operator()(const MAP1& m1, const MAP1& m2) const {
123 if (m1.ch0 == m2.ch0)
124 return m1.sc < m2.sc;
125 return m1.ch0 < m2.ch0;
127 } map1[] = {
128 {0x0021, 0x0A, 0x31}, // XK_exclam+AE01: VKEY_1
129 {0x0021, 0x11, 0x38}, // XK_exclam+AE08: VKEY_8
130 {0x0021, 0x3D, 0xDF}, // XK_exclam+AB10: VKEY_OEM_8
131 {0x0022, 0x0B, 0x32}, // XK_quotedbl+AE02: VKEY_2
132 {0x0022, 0x0C, 0x33}, // XK_quotedbl+AE03: VKEY_3
133 {0x0023, 0x31, 0xDE}, // XK_numbersign+TLDE: VKEY_OEM_7
134 {0x0024, 0x23, 0xBA}, // XK_dollar+AD12: VKEY_OEM_1
135 {0x0024, 0x33, 0xDF}, // XK_dollar+BKSL: VKEY_OEM_8
136 {0x0027, 0x0D, 0x34}, // XK_quoteright+AE04: VKEY_4
137 {0x0027, 0x18, 0xDE}, // XK_quoteright+AD01: VKEY_OEM_7
138 {0x0027, 0x23, 0xBA}, // XK_quoteright+AD12: VKEY_OEM_1
139 {0x0027, 0x3D, 0xDE}, // XK_quoteright+AB10: VKEY_OEM_7
140 {0x0028, 0x0E, 0x35}, // XK_parenleft+AE05: VKEY_5
141 {0x0028, 0x12, 0x39}, // XK_parenleft+AE09: VKEY_9
142 {0x0028, 0x33, 0xDC}, // XK_parenleft+BKSL: VKEY_OEM_5
143 {0x0029, 0x13, 0x30}, // XK_parenright+AE10: VKEY_0
144 {0x0029, 0x14, 0xDB}, // XK_parenright+AE11: VKEY_OEM_4
145 {0x0029, 0x23, 0xDD}, // XK_parenright+AD12: VKEY_OEM_6
146 {0x002A, 0x23, 0xBA}, // XK_asterisk+AD12: VKEY_OEM_1
147 {0x002A, 0x33, 0xDC}, // XK_asterisk+BKSL: VKEY_OEM_5
148 {0x002B, 0x0A, 0x31}, // XK_plus+AE01: VKEY_1
149 {0x002B, 0x15, 0xBB}, // XK_plus+AE12: VKEY_OEM_PLUS
150 {0x002B, 0x22, 0xBB}, // XK_plus+AD11: VKEY_OEM_PLUS
151 {0x002B, 0x23, 0xBB}, // XK_plus+AD12: VKEY_OEM_PLUS
152 {0x002B, 0x2F, 0xBB}, // XK_plus+AC10: VKEY_OEM_PLUS
153 {0x002B, 0x33, 0xBF}, // XK_plus+BKSL: VKEY_OEM_2
154 {0x002C, 0x0C, 0x33}, // XK_comma+AE03: VKEY_3
155 {0x002C, 0x0E, 0x35}, // XK_comma+AE05: VKEY_5
156 {0x002C, 0x0F, 0x36}, // XK_comma+AE06: VKEY_6
157 {0x002C, 0x12, 0x39}, // XK_comma+AE09: VKEY_9
158 {0x002C, 0x19, 0xBC}, // XK_comma+AD02: VKEY_OEM_COMMA
159 {0x002C, 0x37, 0xBC}, // XK_comma+AB04: VKEY_OEM_COMMA
160 {0x002C, 0x3A, 0xBC}, // XK_comma+AB07: VKEY_OEM_COMMA
161 {0x002C, 0x3B, 0xBC}, // XK_comma+AB08: VKEY_OEM_COMMA
162 {0x002D, 0x0B, 0x32}, // XK_minus+AE02: VKEY_2
163 {0x002D, 0x0F, 0x36}, // XK_minus+AE06: VKEY_6
164 {0x002D, 0x14, 0xBD}, // XK_minus+AE11: VKEY_OEM_MINUS
165 {0x002D, 0x26, 0xBD}, // XK_minus+AC01: VKEY_OEM_MINUS
166 {0x002D, 0x30, 0xBD}, // XK_minus+AC11: VKEY_OEM_MINUS
167 {0x002E, 0x10, 0x37}, // XK_period+AE07: VKEY_7
168 {0x002E, 0x11, 0x38}, // XK_period+AE08: VKEY_8
169 {0x002E, 0x1A, 0xBE}, // XK_period+AD03: VKEY_OEM_PERIOD
170 {0x002E, 0x1B, 0xBE}, // XK_period+AD04: VKEY_OEM_PERIOD
171 {0x002E, 0x20, 0xBE}, // XK_period+AD09: VKEY_OEM_PERIOD
172 {0x002E, 0x30, 0xDE}, // XK_period+AC11: VKEY_OEM_7
173 {0x002E, 0x3C, 0xBE}, // XK_period+AB09: VKEY_OEM_PERIOD
174 {0x002E, 0x3D, 0xBF}, // XK_period+AB10: VKEY_OEM_2
175 {0x002F, 0x14, 0xDB}, // XK_slash+AE11: VKEY_OEM_4
176 {0x002F, 0x22, 0xBF}, // XK_slash+AD11: VKEY_OEM_2
177 {0x002F, 0x31, 0xDE}, // XK_slash+TLDE: VKEY_OEM_7
178 {0x002F, 0x33, 0xDC}, // XK_slash+BKSL: VKEY_OEM_5
179 {0x002F, 0x3D, 0xBF}, // XK_slash+AB10: VKEY_OEM_2
180 {0x003A, 0x0A, 0x31}, // XK_colon+AE01: VKEY_1
181 {0x003A, 0x0E, 0x35}, // XK_colon+AE05: VKEY_5
182 {0x003A, 0x0F, 0x36}, // XK_colon+AE06: VKEY_6
183 {0x003A, 0x3C, 0xBF}, // XK_colon+AB09: VKEY_OEM_2
184 {0x003B, 0x0D, 0x34}, // XK_semicolon+AE04: VKEY_4
185 {0x003B, 0x11, 0x38}, // XK_semicolon+AE08: VKEY_8
186 {0x003B, 0x18, 0xBA}, // XK_semicolon+AD01: VKEY_OEM_1
187 {0x003B, 0x22, 0xBA}, // XK_semicolon+AD11: VKEY_OEM_1
188 {0x003B, 0x23, 0xDD}, // XK_semicolon+AD12: VKEY_OEM_6
189 {0x003B, 0x2F, 0xBA}, // XK_semicolon+AC10: VKEY_OEM_1
190 {0x003B, 0x31, 0xC0}, // XK_semicolon+TLDE: VKEY_OEM_3
191 {0x003B, 0x34, 0xBA}, // XK_semicolon+AB01: VKEY_OEM_1
192 {0x003B, 0x3B, 0xBE}, // XK_semicolon+AB08: VKEY_OEM_PERIOD
193 {0x003B, 0x3D, 0xBF}, // XK_semicolon+AB10: VKEY_OEM_2
194 {0x003D, 0x11, 0x38}, // XK_equal+AE08: VKEY_8
195 {0x003D, 0x15, 0xBB}, // XK_equal+AE12: VKEY_OEM_PLUS
196 {0x003D, 0x23, 0xBB}, // XK_equal+AD12: VKEY_OEM_PLUS
197 {0x003F, 0x0B, 0x32}, // XK_question+AE02: VKEY_2
198 {0x003F, 0x10, 0x37}, // XK_question+AE07: VKEY_7
199 {0x003F, 0x11, 0x38}, // XK_question+AE08: VKEY_8
200 {0x003F, 0x14, 0xBB}, // XK_question+AE11: VKEY_OEM_PLUS
201 {0x0040, 0x23, 0xDD}, // XK_at+AD12: VKEY_OEM_6
202 {0x0040, 0x31, 0xDE}, // XK_at+TLDE: VKEY_OEM_7
203 {0x005B, 0x0A, 0xDB}, // XK_bracketleft+AE01: VKEY_OEM_4
204 {0x005B, 0x14, 0xDB}, // XK_bracketleft+AE11: VKEY_OEM_4
205 {0x005B, 0x22, 0xDB}, // XK_bracketleft+AD11: VKEY_OEM_4
206 {0x005B, 0x23, 0xDD}, // XK_bracketleft+AD12: VKEY_OEM_6
207 {0x005B, 0x30, 0xDE}, // XK_bracketleft+AC11: VKEY_OEM_7
208 {0x005C, 0x15, 0xDB}, // XK_backslash+AE12: VKEY_OEM_4
209 {0x005D, 0x0B, 0xDD}, // XK_bracketright+AE02: VKEY_OEM_6
210 {0x005D, 0x15, 0xDD}, // XK_bracketright+AE12: VKEY_OEM_6
211 {0x005D, 0x23, 0xDD}, // XK_bracketright+AD12: VKEY_OEM_6
212 {0x005D, 0x31, 0xC0}, // XK_bracketright+TLDE: VKEY_OEM_3
213 {0x005D, 0x33, 0xDC}, // XK_bracketright+BKSL: VKEY_OEM_5
214 {0x005F, 0x11, 0x38}, // XK_underscore+AE08: VKEY_8
215 {0x005F, 0x14, 0xBD}, // XK_underscore+AE11: VKEY_OEM_MINUS
216 {0x00A7, 0x0D, 0x34}, // XK_section+AE04: VKEY_4
217 {0x00A7, 0x0F, 0x36}, // XK_section+AE06: VKEY_6
218 {0x00A7, 0x30, 0xDE}, // XK_section+AC11: VKEY_OEM_7
219 {0x00AB, 0x11, 0x38}, // XK_guillemotleft+AE08: VKEY_8
220 {0x00AB, 0x15, 0xDD}, // XK_guillemotleft+AE12: VKEY_OEM_6
221 {0x00B0, 0x15, 0xBF}, // XK_degree+AE12: VKEY_OEM_2
222 {0x00B0, 0x31, 0xDE}, // XK_degree+TLDE: VKEY_OEM_7
223 {0x00BA, 0x30, 0xDE}, // XK_masculine+AC11: VKEY_OEM_7
224 {0x00BA, 0x31, 0xDC}, // XK_masculine+TLDE: VKEY_OEM_5
225 {0x00E0, 0x13, 0x30}, // XK_agrave+AE10: VKEY_0
226 {0x00E0, 0x33, 0xDC}, // XK_agrave+BKSL: VKEY_OEM_5
227 {0x00E1, 0x11, 0x38}, // XK_aacute+AE08: VKEY_8
228 {0x00E1, 0x30, 0xDE}, // XK_aacute+AC11: VKEY_OEM_7
229 {0x00E2, 0x0B, 0x32}, // XK_acircumflex+AE02: VKEY_2
230 {0x00E2, 0x33, 0xDC}, // XK_acircumflex+BKSL: VKEY_OEM_5
231 {0x00E4, 0x23, 0xDD}, // XK_adiaeresis+AD12: VKEY_OEM_6
232 {0x00E6, 0x2F, 0xC0}, // XK_ae+AC10: VKEY_OEM_3
233 {0x00E6, 0x30, 0xDE}, // XK_ae+AC11: VKEY_OEM_7
234 {0x00E7, 0x12, 0x39}, // XK_ccedilla+AE09: VKEY_9
235 {0x00E7, 0x22, 0xDB}, // XK_ccedilla+AD11: VKEY_OEM_4
236 {0x00E7, 0x23, 0xDD}, // XK_ccedilla+AD12: VKEY_OEM_6
237 {0x00E7, 0x30, 0xDE}, // XK_ccedilla+AC11: VKEY_OEM_7
238 {0x00E7, 0x33, 0xBF}, // XK_ccedilla+BKSL: VKEY_OEM_2
239 {0x00E7, 0x3B, 0xBC}, // XK_ccedilla+AB08: VKEY_OEM_COMMA
240 {0x00E8, 0x10, 0x37}, // XK_egrave+AE07: VKEY_7
241 {0x00E8, 0x22, 0xBA}, // XK_egrave+AD11: VKEY_OEM_1
242 {0x00E8, 0x30, 0xC0}, // XK_egrave+AC11: VKEY_OEM_3
243 {0x00E9, 0x0B, 0x32}, // XK_eacute+AE02: VKEY_2
244 {0x00E9, 0x13, 0x30}, // XK_eacute+AE10: VKEY_0
245 {0x00E9, 0x3D, 0xBF}, // XK_eacute+AB10: VKEY_OEM_2
246 {0x00ED, 0x12, 0x39}, // XK_iacute+AE09: VKEY_9
247 {0x00ED, 0x31, 0x30}, // XK_iacute+TLDE: VKEY_0
248 {0x00F0, 0x22, 0xDD}, // XK_eth+AD11: VKEY_OEM_6
249 {0x00F0, 0x23, 0xBA}, // XK_eth+AD12: VKEY_OEM_1
250 {0x00F3, 0x15, 0xBB}, // XK_oacute+AE12: VKEY_OEM_PLUS
251 {0x00F3, 0x33, 0xDC}, // XK_oacute+BKSL: VKEY_OEM_5
252 {0x00F4, 0x0D, 0x34}, // XK_ocircumflex+AE04: VKEY_4
253 {0x00F4, 0x2F, 0xBA}, // XK_ocircumflex+AC10: VKEY_OEM_1
254 {0x00F6, 0x13, 0xC0}, // XK_odiaeresis+AE10: VKEY_OEM_3
255 {0x00F6, 0x14, 0xBB}, // XK_odiaeresis+AE11: VKEY_OEM_PLUS
256 {0x00F6, 0x22, 0xDB}, // XK_odiaeresis+AD11: VKEY_OEM_4
257 {0x00F8, 0x2F, 0xC0}, // XK_oslash+AC10: VKEY_OEM_3
258 {0x00F8, 0x30, 0xDE}, // XK_oslash+AC11: VKEY_OEM_7
259 {0x00F9, 0x30, 0xC0}, // XK_ugrave+AC11: VKEY_OEM_3
260 {0x00F9, 0x33, 0xBF}, // XK_ugrave+BKSL: VKEY_OEM_2
261 {0x00FA, 0x22, 0xDB}, // XK_uacute+AD11: VKEY_OEM_4
262 {0x00FA, 0x23, 0xDD}, // XK_uacute+AD12: VKEY_OEM_6
263 {0x00FC, 0x19, 0x57}, // XK_udiaeresis+AD02: VKEY_W
264 {0x01B1, 0x0A, 0x31}, // XK_aogonek+AE01: VKEY_1
265 {0x01B1, 0x18, 0x51}, // XK_aogonek+AD01: VKEY_Q
266 {0x01B1, 0x30, 0xDE}, // XK_aogonek+AC11: VKEY_OEM_7
267 {0x01B3, 0x2F, 0xBA}, // XK_lstroke+AC10: VKEY_OEM_1
268 {0x01B3, 0x33, 0xBF}, // XK_lstroke+BKSL: VKEY_OEM_2
269 {0x01B9, 0x0C, 0x33}, // XK_scaron+AE03: VKEY_3
270 {0x01B9, 0x0F, 0x36}, // XK_scaron+AE06: VKEY_6
271 {0x01B9, 0x22, 0xDB}, // XK_scaron+AD11: VKEY_OEM_4
272 {0x01B9, 0x26, 0xBA}, // XK_scaron+AC01: VKEY_OEM_1
273 {0x01B9, 0x29, 0x46}, // XK_scaron+AC04: VKEY_F
274 {0x01B9, 0x3C, 0xBE}, // XK_scaron+AB09: VKEY_OEM_PERIOD
275 {0x01BA, 0x2F, 0xBA}, // XK_scedilla+AC10: VKEY_OEM_1
276 {0x01BA, 0x3C, 0xBE}, // XK_scedilla+AB09: VKEY_OEM_PERIOD
277 {0x01BE, 0x0F, 0x36}, // XK_zcaron+AE06: VKEY_6
278 {0x01BE, 0x15, 0xBB}, // XK_zcaron+AE12: VKEY_OEM_PLUS
279 {0x01BE, 0x19, 0x57}, // XK_zcaron+AD02: VKEY_W
280 {0x01BE, 0x22, 0x59}, // XK_zcaron+AD11: VKEY_Y
281 {0x01BE, 0x33, 0xDC}, // XK_zcaron+BKSL: VKEY_OEM_5
282 {0x01BF, 0x22, 0xDB}, // XK_zabovedot+AD11: VKEY_OEM_4
283 {0x01BF, 0x33, 0xDC}, // XK_zabovedot+BKSL: VKEY_OEM_5
284 {0x01E3, 0x0A, 0x31}, // XK_abreve+AE01: VKEY_1
285 {0x01E3, 0x22, 0xDB}, // XK_abreve+AD11: VKEY_OEM_4
286 {0x01E8, 0x0B, 0x32}, // XK_ccaron+AE02: VKEY_2
287 {0x01E8, 0x0D, 0x34}, // XK_ccaron+AE04: VKEY_4
288 {0x01E8, 0x21, 0x58}, // XK_ccaron+AD10: VKEY_X
289 {0x01E8, 0x2F, 0xBA}, // XK_ccaron+AC10: VKEY_OEM_1
290 {0x01E8, 0x3B, 0xBC}, // XK_ccaron+AB08: VKEY_OEM_COMMA
291 {0x01EA, 0x0C, 0x33}, // XK_eogonek+AE03: VKEY_3
292 {0x01F0, 0x13, 0x30}, // XK_dstroke+AE10: VKEY_0
293 {0x01F0, 0x23, 0xDD}, // XK_dstroke+AD12: VKEY_OEM_6
294 {0x03E7, 0x0E, 0x35}, // XK_iogonek+AE05: VKEY_5
295 {0x03EC, 0x0D, 0x34}, // XK_eabovedot+AE04: VKEY_4
296 {0x03EC, 0x30, 0xDE}, // XK_eabovedot+AC11: VKEY_OEM_7
297 {0x03F9, 0x10, 0x37}, // XK_uogonek+AE07: VKEY_7
298 {0x03FE, 0x11, 0x38}, // XK_umacron+AE08: VKEY_8
299 {0x03FE, 0x18, 0x51}, // XK_umacron+AD01: VKEY_Q
300 {0x03FE, 0x35, 0x58}, // XK_umacron+AB02: VKEY_X
303 const struct MAP2 {
304 KeySym ch0;
305 unsigned sc;
306 KeySym ch1;
307 uint8 vk;
308 bool operator()(const MAP2& m1, const MAP2& m2) const {
309 if (m1.ch0 == m2.ch0 && m1.sc == m2.sc)
310 return m1.ch1 < m2.ch1;
311 if (m1.ch0 == m2.ch0)
312 return m1.sc < m2.sc;
313 return m1.ch0 < m2.ch0;
315 } map2[] = {
316 {0x0023, 0x33, 0x0027,
317 0xBF}, // XK_numbersign+BKSL+XK_quoteright: VKEY_OEM_2
318 {0x0027, 0x30, 0x0022,
319 0xDE}, // XK_quoteright+AC11+XK_quotedbl: VKEY_OEM_7
320 {0x0027, 0x31, 0x0022,
321 0xC0}, // XK_quoteright+TLDE+XK_quotedbl: VKEY_OEM_3
322 {0x0027, 0x31, 0x00B7,
323 0xDC}, // XK_quoteright+TLDE+XK_periodcentered: VKEY_OEM_5
324 {0x0027, 0x33, 0x0000, 0xDC}, // XK_quoteright+BKSL+NoSymbol: VKEY_OEM_5
325 {0x002D, 0x3D, 0x003D, 0xBD}, // XK_minus+AB10+XK_equal: VKEY_OEM_MINUS
326 {0x002F, 0x0C, 0x0033, 0x33}, // XK_slash+AE03+XK_3: VKEY_3
327 {0x002F, 0x0C, 0x003F, 0xBF}, // XK_slash+AE03+XK_question: VKEY_OEM_2
328 {0x002F, 0x13, 0x0030, 0x30}, // XK_slash+AE10+XK_0: VKEY_0
329 {0x002F, 0x13, 0x003F, 0xBF}, // XK_slash+AE10+XK_question: VKEY_OEM_2
330 {0x003D, 0x3D, 0x0025, 0xDF}, // XK_equal+AB10+XK_percent: VKEY_OEM_8
331 {0x003D, 0x3D, 0x002B, 0xBB}, // XK_equal+AB10+XK_plus: VKEY_OEM_PLUS
332 {0x005C, 0x33, 0x002F, 0xDE}, // XK_backslash+BKSL+XK_slash: VKEY_OEM_7
333 {0x005C, 0x33, 0x007C, 0xDC}, // XK_backslash+BKSL+XK_bar: VKEY_OEM_5
334 {0x0060, 0x31, 0x0000, 0xC0}, // XK_quoteleft+TLDE+NoSymbol: VKEY_OEM_3
335 {0x0060, 0x31, 0x00AC, 0xDF}, // XK_quoteleft+TLDE+XK_notsign: VKEY_OEM_8
336 {0x00A7, 0x31, 0x00B0, 0xBF}, // XK_section+TLDE+XK_degree: VKEY_OEM_2
337 {0x00A7, 0x31, 0x00BD, 0xDC}, // XK_section+TLDE+XK_onehalf: VKEY_OEM_5
338 {0x00E0, 0x30, 0x00B0, 0xDE}, // XK_agrave+AC11+XK_degree: VKEY_OEM_7
339 {0x00E0, 0x30, 0x00E4, 0xDC}, // XK_agrave+AC11+XK_adiaeresis: VKEY_OEM_5
340 {0x00E4, 0x30, 0x00E0, 0xDC}, // XK_adiaeresis+AC11+XK_agrave: VKEY_OEM_5
341 {0x00E9, 0x2F, 0x00C9, 0xBA}, // XK_eacute+AC10+XK_Eacute: VKEY_OEM_1
342 {0x00E9, 0x2F, 0x00F6, 0xDE}, // XK_eacute+AC10+XK_odiaeresis: VKEY_OEM_7
343 {0x00F6, 0x2F, 0x00E9, 0xDE}, // XK_odiaeresis+AC10+XK_eacute: VKEY_OEM_7
344 {0x00FC, 0x22, 0x00E8, 0xBA}, // XK_udiaeresis+AD11+XK_egrave: VKEY_OEM_1
347 const struct MAP3 {
348 KeySym ch0;
349 unsigned sc;
350 KeySym ch1;
351 KeySym ch2;
352 uint8 vk;
353 bool operator()(const MAP3& m1, const MAP3& m2) const {
354 if (m1.ch0 == m2.ch0 && m1.sc == m2.sc && m1.ch1 == m2.ch1)
355 return m1.ch2 < m2.ch2;
356 if (m1.ch0 == m2.ch0 && m1.sc == m2.sc)
357 return m1.ch1 < m2.ch1;
358 if (m1.ch0 == m2.ch0)
359 return m1.sc < m2.sc;
360 return m1.ch0 < m2.ch0;
362 } map3[] = {
363 {0x0023, 0x33, 0x007E, 0x0000,
364 0xDE}, // XK_numbersign+BKSL+XK_asciitilde+NoSymbol: VKEY_OEM_7
365 {0x0027, 0x14, 0x003F, 0x0000,
366 0xDB}, // XK_quoteright+AE11+XK_question+NoSymbol: VKEY_OEM_4
367 {0x0027, 0x14, 0x003F, 0x00DD,
368 0xDB}, // XK_quoteright+AE11+XK_question+XK_Yacute: VKEY_OEM_4
369 {0x0027, 0x15, 0x002A, 0x0000,
370 0xBB}, // XK_quoteright+AE12+XK_asterisk+NoSymbol: VKEY_OEM_PLUS
371 {0x0027, 0x30, 0x0040, 0x0000,
372 0xC0}, // XK_quoteright+AC11+XK_at+NoSymbol: VKEY_OEM_3
373 {0x0027, 0x33, 0x002A, 0x0000,
374 0xBF}, // XK_quoteright+BKSL+XK_asterisk+NoSymbol: VKEY_OEM_2
375 {0x0027, 0x33, 0x002A, 0x00BD,
376 0xDC}, // XK_quoteright+BKSL+XK_asterisk+XK_onehalf: VKEY_OEM_5
377 {0x0027, 0x33, 0x002A, 0x01A3,
378 0xBF}, // XK_quoteright+BKSL+XK_asterisk+XK_Lstroke: VKEY_OEM_2
379 {0x0027, 0x34, 0x0022, 0x0000,
380 0x5A}, // XK_quoteright+AB01+XK_quotedbl+NoSymbol: VKEY_Z
381 {0x0027, 0x34, 0x0022, 0x01D8,
382 0xDE}, // XK_quoteright+AB01+XK_quotedbl+XK_Rcaron: VKEY_OEM_7
383 {0x002B, 0x14, 0x003F, 0x0000,
384 0xBB}, // XK_plus+AE11+XK_question+NoSymbol: VKEY_OEM_PLUS
385 {0x002B, 0x14, 0x003F, 0x005C,
386 0xBD}, // XK_plus+AE11+XK_question+XK_backslash: VKEY_OEM_MINUS
387 {0x002B, 0x14, 0x003F, 0x01F5,
388 0xBB}, // XK_plus+AE11+XK_question+XK_odoubleacute: VKEY_OEM_PLUS
389 {0x002D, 0x15, 0x005F, 0x0000,
390 0xBD}, // XK_minus+AE12+XK_underscore+NoSymbol: VKEY_OEM_MINUS
391 {0x002D, 0x15, 0x005F, 0x03B3,
392 0xDB}, // XK_minus+AE12+XK_underscore+XK_rcedilla: VKEY_OEM_4
393 {0x002D, 0x3D, 0x005F, 0x0000,
394 0xBD}, // XK_minus+AB10+XK_underscore+NoSymbol: VKEY_OEM_MINUS
395 {0x002D, 0x3D, 0x005F, 0x002A,
396 0xBD}, // XK_minus+AB10+XK_underscore+XK_asterisk: VKEY_OEM_MINUS
397 {0x002D, 0x3D, 0x005F, 0x002F,
398 0xBF}, // XK_minus+AB10+XK_underscore+XK_slash: VKEY_OEM_2
399 {0x002D, 0x3D, 0x005F, 0x006E,
400 0xBD}, // XK_minus+AB10+XK_underscore+XK_n: VKEY_OEM_MINUS
401 {0x003D, 0x14, 0x0025, 0x0000,
402 0xBB}, // XK_equal+AE11+XK_percent+NoSymbol: VKEY_OEM_PLUS
403 {0x003D, 0x14, 0x0025, 0x002D,
404 0xBD}, // XK_equal+AE11+XK_percent+XK_minus: VKEY_OEM_MINUS
405 {0x005C, 0x31, 0x007C, 0x0031,
406 0xDC}, // XK_backslash+TLDE+XK_bar+XK_1: VKEY_OEM_5
407 {0x005C, 0x31, 0x007C, 0x03D1,
408 0xC0}, // XK_backslash+TLDE+XK_bar+XK_Ncedilla: VKEY_OEM_3
409 {0x0060, 0x31, 0x007E, 0x0000,
410 0xC0}, // XK_quoteleft+TLDE+XK_asciitilde+NoSymbol: VKEY_OEM_3
411 {0x0060, 0x31, 0x007E, 0x0031,
412 0xC0}, // XK_quoteleft+TLDE+XK_asciitilde+XK_1: VKEY_OEM_3
413 {0x0060, 0x31, 0x007E, 0x003B,
414 0xC0}, // XK_quoteleft+TLDE+XK_asciitilde+XK_semicolon: VKEY_OEM_3
415 {0x0060, 0x31, 0x007E, 0x0060,
416 0xC0}, // XK_quoteleft+TLDE+XK_asciitilde+XK_quoteleft: VKEY_OEM_3
417 {0x0060, 0x31, 0x007E, 0x00BF,
418 0xC0}, // XK_quoteleft+TLDE+XK_asciitilde+XK_questiondown: VKEY_OEM_3
419 {0x0060, 0x31, 0x007E, 0x01F5,
420 0xC0}, // XK_quoteleft+TLDE+XK_asciitilde+XK_odoubleacute: VKEY_OEM_3
421 {0x00E4, 0x30, 0x00C4, 0x0000,
422 0xDE}, // XK_adiaeresis+AC11+XK_Adiaeresis+NoSymbol: VKEY_OEM_7
423 {0x00E4, 0x30, 0x00C4, 0x01A6,
424 0xDE}, // XK_adiaeresis+AC11+XK_Adiaeresis+XK_Sacute: VKEY_OEM_7
425 {0x00E4, 0x30, 0x00C4, 0x01F8,
426 0xDE}, // XK_adiaeresis+AC11+XK_Adiaeresis+XK_rcaron: VKEY_OEM_7
427 {0x00E7, 0x2F, 0x00C7, 0x0000,
428 0xBA}, // XK_ccedilla+AC10+XK_Ccedilla+NoSymbol: VKEY_OEM_1
429 {0x00E7, 0x2F, 0x00C7, 0x00DE,
430 0xC0}, // XK_ccedilla+AC10+XK_Ccedilla+XK_Thorn: VKEY_OEM_3
431 {0x00F6, 0x2F, 0x00D6, 0x0000,
432 0xC0}, // XK_odiaeresis+AC10+XK_Odiaeresis+NoSymbol: VKEY_OEM_3
433 {0x00F6, 0x2F, 0x00D6, 0x01DE,
434 0xC0}, // XK_odiaeresis+AC10+XK_Odiaeresis+XK_Tcedilla: VKEY_OEM_3
435 {0x00FC, 0x14, 0x00DC, 0x0000,
436 0xBF}, // XK_udiaeresis+AE11+XK_Udiaeresis+NoSymbol: VKEY_OEM_2
437 {0x00FC, 0x22, 0x00DC, 0x0000,
438 0xBA}, // XK_udiaeresis+AD11+XK_Udiaeresis+NoSymbol: VKEY_OEM_1
439 {0x00FC, 0x22, 0x00DC, 0x01A3,
440 0xC0}, // XK_udiaeresis+AD11+XK_Udiaeresis+XK_Lstroke: VKEY_OEM_3
441 {0x01EA, 0x3D, 0x01CA, 0x0000,
442 0xBD}, // XK_eogonek+AB10+XK_Eogonek+NoSymbol: VKEY_OEM_MINUS
443 {0x01EA, 0x3D, 0x01CA, 0x006E,
444 0xBF}, // XK_eogonek+AB10+XK_Eogonek+XK_n: VKEY_OEM_2
445 {0x03E7, 0x22, 0x03C7, 0x0000,
446 0xDB}, // XK_iogonek+AD11+XK_Iogonek+NoSymbol: VKEY_OEM_4
447 {0x03F9, 0x2F, 0x03D9, 0x0000,
448 0xC0}, // XK_uogonek+AC10+XK_Uogonek+NoSymbol: VKEY_OEM_3
449 {0x03F9, 0x2F, 0x03D9, 0x01DE,
450 0xBA}, // XK_uogonek+AC10+XK_Uogonek+XK_Tcedilla: VKEY_OEM_1
453 template <class T_MAP>
454 KeyboardCode FindVK(const T_MAP& key, const T_MAP* map, size_t size) {
455 T_MAP comp = {0};
456 const T_MAP* p = std::lower_bound(map, map + size, key, comp);
457 if (p != map + size && !comp(*p, key) && !comp(key, *p))
458 return static_cast<KeyboardCode>(p->vk);
459 return VKEY_UNKNOWN;
462 // Check for TTY function keys or space key which should always be mapped
463 // based on KeySym, and never fall back to MAP0~MAP3, since some layouts
464 // generate them by applying the Control/AltGr modifier to some other key.
465 // e.g. in de(neo), AltGr+V generates XK_Enter.
466 bool IsTtyFunctionOrSpaceKey(KeySym keysym) {
467 KeySym keysyms[] = {
468 XK_BackSpace,
469 XK_Tab,
470 XK_Linefeed,
471 XK_Clear,
472 XK_Return,
473 XK_Pause,
474 XK_Scroll_Lock,
475 XK_Sys_Req,
476 XK_Escape,
477 XK_Delete,
478 XK_space
481 for (size_t i = 0; i < arraysize(keysyms); ++i) {
482 if (keysyms[i] == keysym)
483 return true;
485 return false;
488 } // namespace
490 // Get an ui::KeyboardCode from an X keyevent
491 KeyboardCode KeyboardCodeFromXKeyEvent(const XEvent* xev) {
492 // Gets correct VKEY code from XEvent is performed as the following steps:
493 // 1. Gets the keysym without modifier states.
494 // 2. For [a-z] & [0-9] cases, returns the VKEY code accordingly.
495 // 3. Find keysym in map0.
496 // 4. If not found, fallback to find keysym + hardware_code in map1.
497 // 5. If not found, fallback to find keysym + keysym_shift + hardware_code
498 // in map2.
499 // 6. If not found, fallback to find keysym + keysym_shift + keysym_altgr +
500 // hardware_code in map3.
501 // 7. If not found, fallback to find in KeyboardCodeFromXKeysym(), which
502 // mainly for non-letter keys.
503 // 8. If not found, fallback to find with the hardware code in US layout.
505 KeySym keysym = NoSymbol;
506 XEvent xkeyevent = {0};
507 if (xev->type == GenericEvent) {
508 // Convert the XI2 key event into a core key event so that we can
509 // continue to use XLookupString() until crbug.com/367732 is complete.
510 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent);
511 } else {
512 xkeyevent.xkey = xev->xkey;
514 KeyboardCode keycode = VKEY_UNKNOWN;
515 XKeyEvent* xkey = &xkeyevent.xkey;
516 // XLookupKeysym does not take into consideration the state of the lock/shift
517 // etc. keys. So it is necessary to use XLookupString instead.
518 XLookupString(xkey, NULL, 0, &keysym, NULL);
519 if (IsKeypadKey(keysym) || IsPrivateKeypadKey(keysym) ||
520 IsCursorKey(keysym) || IsPFKey(keysym) || IsFunctionKey(keysym) ||
521 IsModifierKey(keysym) || IsTtyFunctionOrSpaceKey(keysym)) {
522 return KeyboardCodeFromXKeysym(keysym);
525 // If |xkey| has modifiers set, other than NumLock, then determine the
526 // un-modified KeySym and use that to map, so that e.g. Ctrl+D correctly
527 // generates VKEY_D.
528 if (xkey->state & 0xFF & ~Mod2Mask) {
529 xkey->state &= (~0xFF | Mod2Mask);
530 XLookupString(xkey, NULL, 0, &keysym, NULL);
533 // [a-z] cases.
534 if (keysym >= XK_a && keysym <= XK_z)
535 return static_cast<KeyboardCode>(VKEY_A + keysym - XK_a);
537 // [0-9] cases.
538 if (keysym >= XK_0 && keysym <= XK_9)
539 return static_cast<KeyboardCode>(VKEY_0 + keysym - XK_0);
541 if (!IsKeypadKey(keysym) && !IsPrivateKeypadKey(keysym) &&
542 !IsCursorKey(keysym) && !IsPFKey(keysym) && !IsFunctionKey(keysym) &&
543 !IsModifierKey(keysym)) {
544 MAP0 key0 = {keysym & 0xFFFF, 0};
545 keycode = FindVK(key0, map0, arraysize(map0));
546 if (keycode != VKEY_UNKNOWN)
547 return keycode;
549 MAP1 key1 = {keysym & 0xFFFF, xkey->keycode, 0};
550 keycode = FindVK(key1, map1, arraysize(map1));
551 if (keycode != VKEY_UNKNOWN)
552 return keycode;
554 KeySym keysym_shift = NoSymbol;
555 xkey->state |= ShiftMask;
556 XLookupString(xkey, NULL, 0, &keysym_shift, NULL);
557 MAP2 key2 = {keysym & 0xFFFF, xkey->keycode, keysym_shift & 0xFFFF, 0};
558 keycode = FindVK(key2, map2, arraysize(map2));
559 if (keycode != VKEY_UNKNOWN)
560 return keycode;
562 KeySym keysym_altgr = NoSymbol;
563 xkey->state &= ~ShiftMask;
564 xkey->state |= Mod1Mask;
565 XLookupString(xkey, NULL, 0, &keysym_altgr, NULL);
566 MAP3 key3 = {keysym & 0xFFFF, xkey->keycode, keysym_shift & 0xFFFF,
567 keysym_altgr & 0xFFFF, 0};
568 keycode = FindVK(key3, map3, arraysize(map3));
569 if (keycode != VKEY_UNKNOWN)
570 return keycode;
572 // On Linux some keys has AltGr char but not on Windows.
573 // So if cannot find VKEY with (ch0+sc+ch1+ch2) in map3, tries to fallback
574 // to just find VKEY with (ch0+sc+ch1). This is the best we could do.
575 MAP3 key4 = {keysym & 0xFFFF, xkey->keycode, keysym_shift & 0xFFFF, 0,
577 const MAP3* p =
578 std::lower_bound(map3, map3 + arraysize(map3), key4, MAP3());
579 if (p != map3 + arraysize(map3) && p->ch0 == key4.ch0 && p->sc == key4.sc &&
580 p->ch1 == key4.ch1)
581 return static_cast<KeyboardCode>(p->vk);
584 keycode = KeyboardCodeFromXKeysym(keysym);
585 if (keycode == VKEY_UNKNOWN && !IsModifierKey(keysym)) {
586 // Modifier keys should not fall back to the hardware-keycode-based US
587 // layout. See crbug.com/402320
588 keycode = DefaultKeyboardCodeFromHardwareKeycode(xkey->keycode);
591 return keycode;
594 KeyboardCode KeyboardCodeFromXKeysym(unsigned int keysym) {
595 // TODO(sad): Have |keysym| go through the X map list?
597 switch (keysym) {
598 case XK_BackSpace:
599 return VKEY_BACK;
600 case XK_Delete:
601 case XK_KP_Delete:
602 return VKEY_DELETE;
603 case XK_Tab:
604 case XK_KP_Tab:
605 case XK_ISO_Left_Tab:
606 case XK_3270_BackTab:
607 return VKEY_TAB;
608 case XK_Linefeed:
609 case XK_Return:
610 case XK_KP_Enter:
611 case XK_ISO_Enter:
612 return VKEY_RETURN;
613 case XK_Clear:
614 case XK_KP_Begin: // NumPad 5 without Num Lock, for crosbug.com/29169.
615 return VKEY_CLEAR;
616 case XK_KP_Space:
617 case XK_space:
618 return VKEY_SPACE;
619 case XK_Home:
620 case XK_KP_Home:
621 return VKEY_HOME;
622 case XK_End:
623 case XK_KP_End:
624 return VKEY_END;
625 case XK_Page_Up:
626 case XK_KP_Page_Up: // aka XK_KP_Prior
627 return VKEY_PRIOR;
628 case XK_Page_Down:
629 case XK_KP_Page_Down: // aka XK_KP_Next
630 return VKEY_NEXT;
631 case XK_Left:
632 case XK_KP_Left:
633 return VKEY_LEFT;
634 case XK_Right:
635 case XK_KP_Right:
636 return VKEY_RIGHT;
637 case XK_Down:
638 case XK_KP_Down:
639 return VKEY_DOWN;
640 case XK_Up:
641 case XK_KP_Up:
642 return VKEY_UP;
643 case XK_Escape:
644 return VKEY_ESCAPE;
645 case XK_Kana_Lock:
646 case XK_Kana_Shift:
647 return VKEY_KANA;
648 case XK_Hangul:
649 return VKEY_HANGUL;
650 case XK_Hangul_Hanja:
651 return VKEY_HANJA;
652 case XK_Kanji:
653 return VKEY_KANJI;
654 case XK_Henkan:
655 return VKEY_CONVERT;
656 case XK_Muhenkan:
657 return VKEY_NONCONVERT;
658 case XK_Zenkaku_Hankaku:
659 return VKEY_DBE_DBCSCHAR;
661 case XK_KP_0:
662 case XK_KP_1:
663 case XK_KP_2:
664 case XK_KP_3:
665 case XK_KP_4:
666 case XK_KP_5:
667 case XK_KP_6:
668 case XK_KP_7:
669 case XK_KP_8:
670 case XK_KP_9:
671 return static_cast<KeyboardCode>(VKEY_NUMPAD0 + (keysym - XK_KP_0));
673 case XK_multiply:
674 case XK_KP_Multiply:
675 return VKEY_MULTIPLY;
676 case XK_KP_Add:
677 return VKEY_ADD;
678 case XK_KP_Separator:
679 return VKEY_SEPARATOR;
680 case XK_KP_Subtract:
681 return VKEY_SUBTRACT;
682 case XK_KP_Decimal:
683 return VKEY_DECIMAL;
684 case XK_KP_Divide:
685 return VKEY_DIVIDE;
686 case XK_KP_Equal:
687 case XK_equal:
688 case XK_plus:
689 return VKEY_OEM_PLUS;
690 case XK_comma:
691 case XK_less:
692 return VKEY_OEM_COMMA;
693 case XK_minus:
694 case XK_underscore:
695 return VKEY_OEM_MINUS;
696 case XK_greater:
697 case XK_period:
698 return VKEY_OEM_PERIOD;
699 case XK_colon:
700 case XK_semicolon:
701 return VKEY_OEM_1;
702 case XK_question:
703 case XK_slash:
704 return VKEY_OEM_2;
705 case XK_asciitilde:
706 case XK_quoteleft:
707 return VKEY_OEM_3;
708 case XK_bracketleft:
709 case XK_braceleft:
710 return VKEY_OEM_4;
711 case XK_backslash:
712 case XK_bar:
713 return VKEY_OEM_5;
714 case XK_bracketright:
715 case XK_braceright:
716 return VKEY_OEM_6;
717 case XK_quoteright:
718 case XK_quotedbl:
719 return VKEY_OEM_7;
720 case XK_ISO_Level5_Shift:
721 return VKEY_OEM_8;
722 case XK_Shift_L:
723 case XK_Shift_R:
724 return VKEY_SHIFT;
725 case XK_Control_L:
726 case XK_Control_R:
727 return VKEY_CONTROL;
728 case XK_Meta_L:
729 case XK_Meta_R:
730 case XK_Alt_L:
731 case XK_Alt_R:
732 return VKEY_MENU;
733 case XK_ISO_Level3_Shift:
734 case XK_Mode_switch:
735 return VKEY_ALTGR;
736 case XK_Multi_key:
737 return VKEY_COMPOSE;
738 case XK_Pause:
739 return VKEY_PAUSE;
740 case XK_Caps_Lock:
741 return VKEY_CAPITAL;
742 case XK_Num_Lock:
743 return VKEY_NUMLOCK;
744 case XK_Scroll_Lock:
745 return VKEY_SCROLL;
746 case XK_Select:
747 return VKEY_SELECT;
748 case XK_Print:
749 return VKEY_PRINT;
750 case XK_Execute:
751 return VKEY_EXECUTE;
752 case XK_Insert:
753 case XK_KP_Insert:
754 return VKEY_INSERT;
755 case XK_Help:
756 return VKEY_HELP;
757 case XK_Super_L:
758 return VKEY_LWIN;
759 case XK_Super_R:
760 return VKEY_RWIN;
761 case XK_Menu:
762 return VKEY_APPS;
763 case XK_F1:
764 case XK_F2:
765 case XK_F3:
766 case XK_F4:
767 case XK_F5:
768 case XK_F6:
769 case XK_F7:
770 case XK_F8:
771 case XK_F9:
772 case XK_F10:
773 case XK_F11:
774 case XK_F12:
775 case XK_F13:
776 case XK_F14:
777 case XK_F15:
778 case XK_F16:
779 case XK_F17:
780 case XK_F18:
781 case XK_F19:
782 case XK_F20:
783 case XK_F21:
784 case XK_F22:
785 case XK_F23:
786 case XK_F24:
787 return static_cast<KeyboardCode>(VKEY_F1 + (keysym - XK_F1));
788 case XK_KP_F1:
789 case XK_KP_F2:
790 case XK_KP_F3:
791 case XK_KP_F4:
792 return static_cast<KeyboardCode>(VKEY_F1 + (keysym - XK_KP_F1));
794 case XK_guillemotleft:
795 case XK_guillemotright:
796 case XK_degree:
797 // In the case of canadian multilingual keyboard layout, VKEY_OEM_102 is
798 // assigned to ugrave key.
799 case XK_ugrave:
800 case XK_Ugrave:
801 case XK_brokenbar:
802 return VKEY_OEM_102; // international backslash key in 102 keyboard.
804 // When evdev is in use, /usr/share/X11/xkb/symbols/inet maps F13-18 keys
805 // to the special XF86XK symbols to support Microsoft Ergonomic keyboards:
806 // https://bugs.freedesktop.org/show_bug.cgi?id=5783
807 // In Chrome, we map these X key symbols back to F13-18 since we don't have
808 // VKEYs for these XF86XK symbols.
809 case XF86XK_Tools:
810 return VKEY_F13;
811 case XF86XK_Launch5:
812 return VKEY_F14;
813 case XF86XK_Launch6:
814 return VKEY_F15;
815 case XF86XK_Launch7:
816 return VKEY_F16;
817 case XF86XK_Launch8:
818 return VKEY_F17;
819 case XF86XK_Launch9:
820 return VKEY_F18;
822 // For supporting multimedia buttons on a USB keyboard.
823 case XF86XK_Back:
824 return VKEY_BROWSER_BACK;
825 case XF86XK_Forward:
826 return VKEY_BROWSER_FORWARD;
827 case XF86XK_Reload:
828 return VKEY_BROWSER_REFRESH;
829 case XF86XK_Stop:
830 return VKEY_BROWSER_STOP;
831 case XF86XK_Search:
832 return VKEY_BROWSER_SEARCH;
833 case XF86XK_Favorites:
834 return VKEY_BROWSER_FAVORITES;
835 case XF86XK_HomePage:
836 return VKEY_BROWSER_HOME;
837 case XF86XK_AudioMute:
838 return VKEY_VOLUME_MUTE;
839 case XF86XK_AudioLowerVolume:
840 return VKEY_VOLUME_DOWN;
841 case XF86XK_AudioRaiseVolume:
842 return VKEY_VOLUME_UP;
843 case XF86XK_AudioNext:
844 return VKEY_MEDIA_NEXT_TRACK;
845 case XF86XK_AudioPrev:
846 return VKEY_MEDIA_PREV_TRACK;
847 case XF86XK_AudioStop:
848 return VKEY_MEDIA_STOP;
849 case XF86XK_AudioPlay:
850 return VKEY_MEDIA_PLAY_PAUSE;
851 case XF86XK_Mail:
852 return VKEY_MEDIA_LAUNCH_MAIL;
853 case XF86XK_LaunchA: // F3 on an Apple keyboard.
854 return VKEY_MEDIA_LAUNCH_APP1;
855 case XF86XK_LaunchB: // F4 on an Apple keyboard.
856 case XF86XK_Calculator:
857 return VKEY_MEDIA_LAUNCH_APP2;
858 case XF86XK_WLAN:
859 return VKEY_WLAN;
860 case XF86XK_PowerOff:
861 return VKEY_POWER;
862 case XF86XK_Sleep:
863 return VKEY_SLEEP;
864 case XF86XK_MonBrightnessDown:
865 return VKEY_BRIGHTNESS_DOWN;
866 case XF86XK_MonBrightnessUp:
867 return VKEY_BRIGHTNESS_UP;
868 case XF86XK_KbdBrightnessDown:
869 return VKEY_KBD_BRIGHTNESS_DOWN;
870 case XF86XK_KbdBrightnessUp:
871 return VKEY_KBD_BRIGHTNESS_UP;
873 // TODO(sad): some keycodes are still missing.
875 DVLOG(1) << "Unknown keysym: " << base::StringPrintf("0x%x", keysym);
876 return VKEY_UNKNOWN;
879 DomCode CodeFromXEvent(const XEvent* xev) {
880 int keycode = (xev->type == GenericEvent)
881 ? static_cast<XIDeviceEvent*>(xev->xcookie.data)->detail
882 : xev->xkey.keycode;
883 return ui::KeycodeConverter::NativeKeycodeToDomCode(keycode);
886 uint16 GetCharacterFromXEvent(const XEvent* xev) {
887 XEvent xkeyevent = {0};
888 const XKeyEvent* xkey = NULL;
889 if (xev->type == GenericEvent) {
890 // Convert the XI2 key event into a core key event so that we can
891 // continue to use XLookupString() until crbug.com/367732 is complete.
892 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent);
893 xkey = &xkeyevent.xkey;
894 } else {
895 xkey = &xev->xkey;
897 KeySym keysym = XK_VoidSymbol;
898 XLookupString(const_cast<XKeyEvent*>(xkey), NULL, 0, &keysym, NULL);
899 return GetUnicodeCharacterFromXKeySym(keysym);
902 KeyboardCode DefaultKeyboardCodeFromHardwareKeycode(
903 unsigned int hardware_code) {
904 // This function assumes that X11 is using evdev-based keycodes.
905 static const KeyboardCode kHardwareKeycodeMap[] = {
906 // Please refer to below links for the table content:
907 // http://www.w3.org/TR/DOM-Level-3-Events-code/#keyboard-101
908 // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode
909 // http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
910 VKEY_UNKNOWN, // 0x00:
911 VKEY_UNKNOWN, // 0x01:
912 VKEY_UNKNOWN, // 0x02:
913 VKEY_UNKNOWN, // 0x03:
914 VKEY_UNKNOWN, // 0x04:
915 VKEY_UNKNOWN, // 0x05:
916 VKEY_UNKNOWN, // 0x06:
917 VKEY_UNKNOWN, // XKB evdev (XKB - 8) X KeySym
918 VKEY_UNKNOWN, // === =============== ======
919 VKEY_ESCAPE, // 0x09: KEY_ESC Escape
920 VKEY_1, // 0x0A: KEY_1 1
921 VKEY_2, // 0x0B: KEY_2 2
922 VKEY_3, // 0x0C: KEY_3 3
923 VKEY_4, // 0x0D: KEY_4 4
924 VKEY_5, // 0x0E: KEY_5 5
925 VKEY_6, // 0x0F: KEY_6 6
926 VKEY_7, // 0x10: KEY_7 7
927 VKEY_8, // 0x11: KEY_8 8
928 VKEY_9, // 0x12: KEY_9 9
929 VKEY_0, // 0x13: KEY_0 0
930 VKEY_OEM_MINUS, // 0x14: KEY_MINUS minus
931 VKEY_OEM_PLUS, // 0x15: KEY_EQUAL equal
932 VKEY_BACK, // 0x16: KEY_BACKSPACE BackSpace
933 VKEY_TAB, // 0x17: KEY_TAB Tab
934 VKEY_Q, // 0x18: KEY_Q q
935 VKEY_W, // 0x19: KEY_W w
936 VKEY_E, // 0x1A: KEY_E e
937 VKEY_R, // 0x1B: KEY_R r
938 VKEY_T, // 0x1C: KEY_T t
939 VKEY_Y, // 0x1D: KEY_Y y
940 VKEY_U, // 0x1E: KEY_U u
941 VKEY_I, // 0x1F: KEY_I i
942 VKEY_O, // 0x20: KEY_O o
943 VKEY_P, // 0x21: KEY_P p
944 VKEY_OEM_4, // 0x22: KEY_LEFTBRACE bracketleft
945 VKEY_OEM_6, // 0x23: KEY_RIGHTBRACE bracketright
946 VKEY_RETURN, // 0x24: KEY_ENTER Return
947 VKEY_LCONTROL, // 0x25: KEY_LEFTCTRL Control_L
948 VKEY_A, // 0x26: KEY_A a
949 VKEY_S, // 0x27: KEY_S s
950 VKEY_D, // 0x28: KEY_D d
951 VKEY_F, // 0x29: KEY_F f
952 VKEY_G, // 0x2A: KEY_G g
953 VKEY_H, // 0x2B: KEY_H h
954 VKEY_J, // 0x2C: KEY_J j
955 VKEY_K, // 0x2D: KEY_K k
956 VKEY_L, // 0x2E: KEY_L l
957 VKEY_OEM_1, // 0x2F: KEY_SEMICOLON semicolon
958 VKEY_OEM_7, // 0x30: KEY_APOSTROPHE apostrophe
959 VKEY_OEM_3, // 0x31: KEY_GRAVE grave
960 VKEY_LSHIFT, // 0x32: KEY_LEFTSHIFT Shift_L
961 VKEY_OEM_5, // 0x33: KEY_BACKSLASH backslash
962 VKEY_Z, // 0x34: KEY_Z z
963 VKEY_X, // 0x35: KEY_X x
964 VKEY_C, // 0x36: KEY_C c
965 VKEY_V, // 0x37: KEY_V v
966 VKEY_B, // 0x38: KEY_B b
967 VKEY_N, // 0x39: KEY_N n
968 VKEY_M, // 0x3A: KEY_M m
969 VKEY_OEM_COMMA, // 0x3B: KEY_COMMA comma
970 VKEY_OEM_PERIOD, // 0x3C: KEY_DOT period
971 VKEY_OEM_2, // 0x3D: KEY_SLASH slash
972 VKEY_RSHIFT, // 0x3E: KEY_RIGHTSHIFT Shift_R
973 VKEY_MULTIPLY, // 0x3F: KEY_KPASTERISK KP_Multiply
974 VKEY_LMENU, // 0x40: KEY_LEFTALT Alt_L
975 VKEY_SPACE, // 0x41: KEY_SPACE space
976 VKEY_CAPITAL, // 0x42: KEY_CAPSLOCK Caps_Lock
977 VKEY_F1, // 0x43: KEY_F1 F1
978 VKEY_F2, // 0x44: KEY_F2 F2
979 VKEY_F3, // 0x45: KEY_F3 F3
980 VKEY_F4, // 0x46: KEY_F4 F4
981 VKEY_F5, // 0x47: KEY_F5 F5
982 VKEY_F6, // 0x48: KEY_F6 F6
983 VKEY_F7, // 0x49: KEY_F7 F7
984 VKEY_F8, // 0x4A: KEY_F8 F8
985 VKEY_F9, // 0x4B: KEY_F9 F9
986 VKEY_F10, // 0x4C: KEY_F10 F10
987 VKEY_NUMLOCK, // 0x4D: KEY_NUMLOCK Num_Lock
988 VKEY_SCROLL, // 0x4E: KEY_SCROLLLOCK Scroll_Lock
989 VKEY_NUMPAD7, // 0x4F: KEY_KP7 KP_7
990 VKEY_NUMPAD8, // 0x50: KEY_KP8 KP_8
991 VKEY_NUMPAD9, // 0x51: KEY_KP9 KP_9
992 VKEY_SUBTRACT, // 0x52: KEY_KPMINUS KP_Subtract
993 VKEY_NUMPAD4, // 0x53: KEY_KP4 KP_4
994 VKEY_NUMPAD5, // 0x54: KEY_KP5 KP_5
995 VKEY_NUMPAD6, // 0x55: KEY_KP6 KP_6
996 VKEY_ADD, // 0x56: KEY_KPPLUS KP_Add
997 VKEY_NUMPAD1, // 0x57: KEY_KP1 KP_1
998 VKEY_NUMPAD2, // 0x58: KEY_KP2 KP_2
999 VKEY_NUMPAD3, // 0x59: KEY_KP3 KP_3
1000 VKEY_NUMPAD0, // 0x5A: KEY_KP0 KP_0
1001 VKEY_DECIMAL, // 0x5B: KEY_KPDOT KP_Decimal
1002 VKEY_UNKNOWN, // 0x5C:
1003 VKEY_DBE_DBCSCHAR, // 0x5D: KEY_ZENKAKUHANKAKU Zenkaku_Hankaku
1004 VKEY_OEM_5, // 0x5E: KEY_102ND backslash
1005 VKEY_F11, // 0x5F: KEY_F11 F11
1006 VKEY_F12, // 0x60: KEY_F12 F12
1007 VKEY_OEM_102, // 0x61: KEY_RO Romaji
1008 VKEY_UNSUPPORTED, // 0x62: KEY_KATAKANA Katakana
1009 VKEY_UNSUPPORTED, // 0x63: KEY_HIRAGANA Hiragana
1010 VKEY_CONVERT, // 0x64: KEY_HENKAN Henkan
1011 VKEY_UNSUPPORTED, // 0x65: KEY_KATAKANAHIRAGANA Hiragana_Katakana
1012 VKEY_NONCONVERT, // 0x66: KEY_MUHENKAN Muhenkan
1013 VKEY_SEPARATOR, // 0x67: KEY_KPJPCOMMA KP_Separator
1014 VKEY_RETURN, // 0x68: KEY_KPENTER KP_Enter
1015 VKEY_RCONTROL, // 0x69: KEY_RIGHTCTRL Control_R
1016 VKEY_DIVIDE, // 0x6A: KEY_KPSLASH KP_Divide
1017 VKEY_PRINT, // 0x6B: KEY_SYSRQ Print
1018 VKEY_RMENU, // 0x6C: KEY_RIGHTALT Alt_R
1019 VKEY_RETURN, // 0x6D: KEY_LINEFEED Linefeed
1020 VKEY_HOME, // 0x6E: KEY_HOME Home
1021 VKEY_UP, // 0x6F: KEY_UP Up
1022 VKEY_PRIOR, // 0x70: KEY_PAGEUP Page_Up
1023 VKEY_LEFT, // 0x71: KEY_LEFT Left
1024 VKEY_RIGHT, // 0x72: KEY_RIGHT Right
1025 VKEY_END, // 0x73: KEY_END End
1026 VKEY_DOWN, // 0x74: KEY_DOWN Down
1027 VKEY_NEXT, // 0x75: KEY_PAGEDOWN Page_Down
1028 VKEY_INSERT, // 0x76: KEY_INSERT Insert
1029 VKEY_DELETE, // 0x77: KEY_DELETE Delete
1030 VKEY_UNSUPPORTED, // 0x78: KEY_MACRO
1031 VKEY_VOLUME_MUTE, // 0x79: KEY_MUTE XF86AudioMute
1032 VKEY_VOLUME_DOWN, // 0x7A: KEY_VOLUMEDOWN XF86AudioLowerVolume
1033 VKEY_VOLUME_UP, // 0x7B: KEY_VOLUMEUP XF86AudioRaiseVolume
1034 VKEY_POWER, // 0x7C: KEY_POWER XF86PowerOff
1035 VKEY_OEM_PLUS, // 0x7D: KEY_KPEQUAL KP_Equal
1036 VKEY_UNSUPPORTED, // 0x7E: KEY_KPPLUSMINUS plusminus
1037 VKEY_PAUSE, // 0x7F: KEY_PAUSE Pause
1038 VKEY_MEDIA_LAUNCH_APP1, // 0x80: KEY_SCALE XF86LaunchA
1039 VKEY_DECIMAL, // 0x81: KEY_KPCOMMA KP_Decimal
1040 VKEY_HANGUL, // 0x82: KEY_HANGUEL Hangul
1041 VKEY_HANJA, // 0x83: KEY_HANJA Hangul_Hanja
1042 VKEY_OEM_5, // 0x84: KEY_YEN yen
1043 VKEY_LWIN, // 0x85: KEY_LEFTMETA Super_L
1044 VKEY_RWIN, // 0x86: KEY_RIGHTMETA Super_R
1045 VKEY_COMPOSE, // 0x87: KEY_COMPOSE Menu
1048 if (hardware_code >= arraysize(kHardwareKeycodeMap)) {
1049 // Additional keycodes used by the Chrome OS top row special function keys.
1050 switch (hardware_code) {
1051 case 0xA6: // KEY_BACK
1052 return VKEY_BACK;
1053 case 0xA7: // KEY_FORWARD
1054 return VKEY_BROWSER_FORWARD;
1055 case 0xB5: // KEY_REFRESH
1056 return VKEY_BROWSER_REFRESH;
1057 case 0xD4: // KEY_DASHBOARD
1058 return VKEY_MEDIA_LAUNCH_APP2;
1059 case 0xE8: // KEY_BRIGHTNESSDOWN
1060 return VKEY_BRIGHTNESS_DOWN;
1061 case 0xE9: // KEY_BRIGHTNESSUP
1062 return VKEY_BRIGHTNESS_UP;
1064 return VKEY_UNKNOWN;
1066 return kHardwareKeycodeMap[hardware_code];
1069 // TODO(jcampan): this method might be incomplete.
1070 int XKeysymForWindowsKeyCode(KeyboardCode keycode, bool shift) {
1071 switch (keycode) {
1072 case VKEY_NUMPAD0:
1073 return XK_KP_0;
1074 case VKEY_NUMPAD1:
1075 return XK_KP_1;
1076 case VKEY_NUMPAD2:
1077 return XK_KP_2;
1078 case VKEY_NUMPAD3:
1079 return XK_KP_3;
1080 case VKEY_NUMPAD4:
1081 return XK_KP_4;
1082 case VKEY_NUMPAD5:
1083 return XK_KP_5;
1084 case VKEY_NUMPAD6:
1085 return XK_KP_6;
1086 case VKEY_NUMPAD7:
1087 return XK_KP_7;
1088 case VKEY_NUMPAD8:
1089 return XK_KP_8;
1090 case VKEY_NUMPAD9:
1091 return XK_KP_9;
1092 case VKEY_MULTIPLY:
1093 return XK_KP_Multiply;
1094 case VKEY_ADD:
1095 return XK_KP_Add;
1096 case VKEY_SUBTRACT:
1097 return XK_KP_Subtract;
1098 case VKEY_DECIMAL:
1099 return XK_KP_Decimal;
1100 case VKEY_DIVIDE:
1101 return XK_KP_Divide;
1103 case VKEY_BACK:
1104 return XK_BackSpace;
1105 case VKEY_TAB:
1106 return shift ? XK_ISO_Left_Tab : XK_Tab;
1107 case VKEY_CLEAR:
1108 return XK_Clear;
1109 case VKEY_RETURN:
1110 return XK_Return;
1111 case VKEY_SHIFT:
1112 return XK_Shift_L;
1113 case VKEY_CONTROL:
1114 return XK_Control_L;
1115 case VKEY_MENU:
1116 return XK_Alt_L;
1117 case VKEY_APPS:
1118 return XK_Menu;
1119 case VKEY_ALTGR:
1120 return XK_ISO_Level3_Shift;
1121 case VKEY_COMPOSE:
1122 return XK_Multi_key;
1124 case VKEY_PAUSE:
1125 return XK_Pause;
1126 case VKEY_CAPITAL:
1127 return XK_Caps_Lock;
1128 case VKEY_KANA:
1129 return XK_Kana_Lock;
1130 case VKEY_HANJA:
1131 return XK_Hangul_Hanja;
1132 case VKEY_CONVERT:
1133 return XK_Henkan;
1134 case VKEY_NONCONVERT:
1135 return XK_Muhenkan;
1136 case VKEY_DBE_SBCSCHAR:
1137 return XK_Zenkaku_Hankaku;
1138 case VKEY_DBE_DBCSCHAR:
1139 return XK_Zenkaku_Hankaku;
1140 case VKEY_ESCAPE:
1141 return XK_Escape;
1142 case VKEY_SPACE:
1143 return XK_space;
1144 case VKEY_PRIOR:
1145 return XK_Page_Up;
1146 case VKEY_NEXT:
1147 return XK_Page_Down;
1148 case VKEY_END:
1149 return XK_End;
1150 case VKEY_HOME:
1151 return XK_Home;
1152 case VKEY_LEFT:
1153 return XK_Left;
1154 case VKEY_UP:
1155 return XK_Up;
1156 case VKEY_RIGHT:
1157 return XK_Right;
1158 case VKEY_DOWN:
1159 return XK_Down;
1160 case VKEY_SELECT:
1161 return XK_Select;
1162 case VKEY_PRINT:
1163 return XK_Print;
1164 case VKEY_EXECUTE:
1165 return XK_Execute;
1166 case VKEY_INSERT:
1167 return XK_Insert;
1168 case VKEY_DELETE:
1169 return XK_Delete;
1170 case VKEY_HELP:
1171 return XK_Help;
1172 case VKEY_0:
1173 return shift ? XK_parenright : XK_0;
1174 case VKEY_1:
1175 return shift ? XK_exclam : XK_1;
1176 case VKEY_2:
1177 return shift ? XK_at : XK_2;
1178 case VKEY_3:
1179 return shift ? XK_numbersign : XK_3;
1180 case VKEY_4:
1181 return shift ? XK_dollar : XK_4;
1182 case VKEY_5:
1183 return shift ? XK_percent : XK_5;
1184 case VKEY_6:
1185 return shift ? XK_asciicircum : XK_6;
1186 case VKEY_7:
1187 return shift ? XK_ampersand : XK_7;
1188 case VKEY_8:
1189 return shift ? XK_asterisk : XK_8;
1190 case VKEY_9:
1191 return shift ? XK_parenleft : XK_9;
1193 case VKEY_A:
1194 case VKEY_B:
1195 case VKEY_C:
1196 case VKEY_D:
1197 case VKEY_E:
1198 case VKEY_F:
1199 case VKEY_G:
1200 case VKEY_H:
1201 case VKEY_I:
1202 case VKEY_J:
1203 case VKEY_K:
1204 case VKEY_L:
1205 case VKEY_M:
1206 case VKEY_N:
1207 case VKEY_O:
1208 case VKEY_P:
1209 case VKEY_Q:
1210 case VKEY_R:
1211 case VKEY_S:
1212 case VKEY_T:
1213 case VKEY_U:
1214 case VKEY_V:
1215 case VKEY_W:
1216 case VKEY_X:
1217 case VKEY_Y:
1218 case VKEY_Z:
1219 return (shift ? XK_A : XK_a) + (keycode - VKEY_A);
1221 case VKEY_LWIN:
1222 return XK_Super_L;
1223 case VKEY_RWIN:
1224 return XK_Super_R;
1226 case VKEY_NUMLOCK:
1227 return XK_Num_Lock;
1229 case VKEY_SCROLL:
1230 return XK_Scroll_Lock;
1232 case VKEY_OEM_1:
1233 return shift ? XK_colon : XK_semicolon;
1234 case VKEY_OEM_PLUS:
1235 return shift ? XK_plus : XK_equal;
1236 case VKEY_OEM_COMMA:
1237 return shift ? XK_less : XK_comma;
1238 case VKEY_OEM_MINUS:
1239 return shift ? XK_underscore : XK_minus;
1240 case VKEY_OEM_PERIOD:
1241 return shift ? XK_greater : XK_period;
1242 case VKEY_OEM_2:
1243 return shift ? XK_question : XK_slash;
1244 case VKEY_OEM_3:
1245 return shift ? XK_asciitilde : XK_quoteleft;
1246 case VKEY_OEM_4:
1247 return shift ? XK_braceleft : XK_bracketleft;
1248 case VKEY_OEM_5:
1249 return shift ? XK_bar : XK_backslash;
1250 case VKEY_OEM_6:
1251 return shift ? XK_braceright : XK_bracketright;
1252 case VKEY_OEM_7:
1253 return shift ? XK_quotedbl : XK_quoteright;
1254 case VKEY_OEM_8:
1255 return XK_ISO_Level5_Shift;
1256 case VKEY_OEM_102:
1257 return shift ? XK_guillemotleft : XK_guillemotright;
1259 case VKEY_F1:
1260 case VKEY_F2:
1261 case VKEY_F3:
1262 case VKEY_F4:
1263 case VKEY_F5:
1264 case VKEY_F6:
1265 case VKEY_F7:
1266 case VKEY_F8:
1267 case VKEY_F9:
1268 case VKEY_F10:
1269 case VKEY_F11:
1270 case VKEY_F12:
1271 case VKEY_F13:
1272 case VKEY_F14:
1273 case VKEY_F15:
1274 case VKEY_F16:
1275 case VKEY_F17:
1276 case VKEY_F18:
1277 case VKEY_F19:
1278 case VKEY_F20:
1279 case VKEY_F21:
1280 case VKEY_F22:
1281 case VKEY_F23:
1282 case VKEY_F24:
1283 return XK_F1 + (keycode - VKEY_F1);
1285 case VKEY_BROWSER_BACK:
1286 return XF86XK_Back;
1287 case VKEY_BROWSER_FORWARD:
1288 return XF86XK_Forward;
1289 case VKEY_BROWSER_REFRESH:
1290 return XF86XK_Reload;
1291 case VKEY_BROWSER_STOP:
1292 return XF86XK_Stop;
1293 case VKEY_BROWSER_SEARCH:
1294 return XF86XK_Search;
1295 case VKEY_BROWSER_FAVORITES:
1296 return XF86XK_Favorites;
1297 case VKEY_BROWSER_HOME:
1298 return XF86XK_HomePage;
1299 case VKEY_VOLUME_MUTE:
1300 return XF86XK_AudioMute;
1301 case VKEY_VOLUME_DOWN:
1302 return XF86XK_AudioLowerVolume;
1303 case VKEY_VOLUME_UP:
1304 return XF86XK_AudioRaiseVolume;
1305 case VKEY_MEDIA_NEXT_TRACK:
1306 return XF86XK_AudioNext;
1307 case VKEY_MEDIA_PREV_TRACK:
1308 return XF86XK_AudioPrev;
1309 case VKEY_MEDIA_STOP:
1310 return XF86XK_AudioStop;
1311 case VKEY_MEDIA_PLAY_PAUSE:
1312 return XF86XK_AudioPlay;
1313 case VKEY_MEDIA_LAUNCH_MAIL:
1314 return XF86XK_Mail;
1315 case VKEY_MEDIA_LAUNCH_APP1:
1316 return XF86XK_LaunchA;
1317 case VKEY_MEDIA_LAUNCH_APP2:
1318 return XF86XK_LaunchB;
1319 case VKEY_WLAN:
1320 return XF86XK_WLAN;
1321 case VKEY_POWER:
1322 return XF86XK_PowerOff;
1323 case VKEY_BRIGHTNESS_DOWN:
1324 return XF86XK_MonBrightnessDown;
1325 case VKEY_BRIGHTNESS_UP:
1326 return XF86XK_MonBrightnessUp;
1327 case VKEY_KBD_BRIGHTNESS_DOWN:
1328 return XF86XK_KbdBrightnessDown;
1329 case VKEY_KBD_BRIGHTNESS_UP:
1330 return XF86XK_KbdBrightnessUp;
1332 default:
1333 LOG(WARNING) << "Unknown keycode:" << keycode;
1334 return 0;
1338 void InitXKeyEventFromXIDeviceEvent(const XEvent& src, XEvent* xkeyevent) {
1339 DCHECK(src.type == GenericEvent);
1340 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(src.xcookie.data);
1341 switch (xievent->evtype) {
1342 case XI_KeyPress:
1343 xkeyevent->type = KeyPress;
1344 break;
1345 case XI_KeyRelease:
1346 xkeyevent->type = KeyRelease;
1347 break;
1348 default:
1349 NOTREACHED();
1351 xkeyevent->xkey.serial = xievent->serial;
1352 xkeyevent->xkey.send_event = xievent->send_event;
1353 xkeyevent->xkey.display = xievent->display;
1354 xkeyevent->xkey.window = xievent->event;
1355 xkeyevent->xkey.root = xievent->root;
1356 xkeyevent->xkey.subwindow = xievent->child;
1357 xkeyevent->xkey.time = xievent->time;
1358 xkeyevent->xkey.x = xievent->event_x;
1359 xkeyevent->xkey.y = xievent->event_y;
1360 xkeyevent->xkey.x_root = xievent->root_x;
1361 xkeyevent->xkey.y_root = xievent->root_y;
1362 xkeyevent->xkey.state = xievent->mods.effective;
1363 xkeyevent->xkey.keycode = xievent->detail;
1364 xkeyevent->xkey.same_screen = 1;
1367 unsigned int XKeyCodeForWindowsKeyCode(ui::KeyboardCode key_code,
1368 int flags,
1369 XDisplay* display) {
1370 // SHIFT state is ignored in the call to XKeysymForWindowsKeyCode() here
1371 // because we map the XKeysym back to a keycode, i.e. a physical key position.
1372 // Using a SHIFT-modified XKeysym would sometimes yield X keycodes that,
1373 // while technically valid, may be surprising in that they do not match
1374 // the keycode of the original press, and conflict with assumptions in
1375 // other code.
1377 // For example, in a US layout, Shift-9 has the interpretation XK_parenleft,
1378 // but the keycode KEY_9 alone does not map to XK_parenleft; instead,
1379 // XKeysymToKeycode() returns KEY_KPLEFTPAREN (keypad left parenthesis)
1380 // which does map to XK_parenleft -- notwithstanding that keyboards with
1381 // dedicated number pad parenthesis keys are currently uncommon.
1383 // Similarly, Shift-Comma has the interpretation XK_less, but KEY_COMMA
1384 // alone does not map to XK_less; XKeysymToKeycode() returns KEY_102ND
1385 // (the '<>' key between Shift and Z on 105-key keyboards) which does.
1387 // crbug.com/386066 and crbug.com/390263 are examples of problems
1388 // associated with this.
1390 return XKeysymToKeycode(display, XKeysymForWindowsKeyCode(key_code, false));
1393 } // namespace ui