Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / ime / chromeos / ime_keymap.cc
blobd21432122a08ebe049db97a2ef742809645d7d44
1 // Copyright 2014 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/base/ime/chromeos/ime_keymap.h"
7 #include <map>
9 #include "base/lazy_instance.h"
11 namespace ui {
13 namespace {
15 const struct KeyCodeTable {
16 KeyboardCode keyboard_code;
17 const char* dom_code;
18 } kKeyCodeTable[] = {
19 {VKEY_BACK, "Backspace"},
20 {VKEY_TAB, "Tab"},
21 {VKEY_RETURN, "Enter"},
22 {VKEY_MENU, "ContextMenu"},
23 {VKEY_PAUSE, "Pause"},
24 {VKEY_CAPITAL, "CapsLock"},
25 {VKEY_KANA, "KanaMode"},
26 {VKEY_HANGUL, "HangulMode"},
27 {VKEY_HANJA, "Hanja"},
28 {VKEY_KANJI, "Kanji"},
29 {VKEY_ESCAPE, "Escape"},
30 {VKEY_CONVERT, "Convert"},
31 {VKEY_NONCONVERT, "NoConvert"},
32 {VKEY_SPACE, "Space"},
33 {VKEY_END, "End"},
34 {VKEY_HOME, "Home"},
35 {VKEY_LEFT, "ArrowLeft"},
36 {VKEY_UP, "ArrowUp"},
37 {VKEY_RIGHT, "ArrowRight"},
38 {VKEY_DOWN, "ArrowDown"},
39 {VKEY_PRINT, "PrintScreen"},
40 {VKEY_INSERT, "Insert"},
41 {VKEY_DELETE, "Delete"},
42 {VKEY_HELP, "Help"},
43 {VKEY_0, "Digit0"},
44 {VKEY_1, "Digit1"},
45 {VKEY_2, "Digit2"},
46 {VKEY_3, "Digit3"},
47 {VKEY_4, "Digit4"},
48 {VKEY_5, "Digit5"},
49 {VKEY_6, "Digit6"},
50 {VKEY_7, "Digit7"},
51 {VKEY_8, "Digit8"},
52 {VKEY_9, "Digit9"},
53 {VKEY_A, "KeyA"},
54 {VKEY_B, "KeyB"},
55 {VKEY_C, "KeyC"},
56 {VKEY_D, "KeyD"},
57 {VKEY_E, "KeyE"},
58 {VKEY_F, "KeyF"},
59 {VKEY_G, "KeyG"},
60 {VKEY_H, "KeyH"},
61 {VKEY_I, "KeyI"},
62 {VKEY_J, "KeyJ"},
63 {VKEY_K, "KeyK"},
64 {VKEY_L, "KeyL"},
65 {VKEY_M, "KeyM"},
66 {VKEY_N, "KeyN"},
67 {VKEY_O, "KeyO"},
68 {VKEY_P, "KeyP"},
69 {VKEY_Q, "KeyQ"},
70 {VKEY_R, "KeyR"},
71 {VKEY_S, "KeyS"},
72 {VKEY_T, "KeyT"},
73 {VKEY_U, "KeyU"},
74 {VKEY_V, "KeyV"},
75 {VKEY_W, "KeyW"},
76 {VKEY_X, "KeyX"},
77 {VKEY_Y, "KeyY"},
78 {VKEY_Z, "KeyZ"},
79 {VKEY_LWIN, "OSLeft"},
80 {VKEY_RWIN, "OSRight"},
81 {VKEY_NUMPAD0, "Numpad0"},
82 {VKEY_NUMPAD1, "Numpad1"},
83 {VKEY_NUMPAD2, "Numpad2"},
84 {VKEY_NUMPAD3, "Numpad3"},
85 {VKEY_NUMPAD4, "Numpad4"},
86 {VKEY_NUMPAD5, "Numpad5"},
87 {VKEY_NUMPAD6, "Numpad6"},
88 {VKEY_NUMPAD7, "Numpad7"},
89 {VKEY_NUMPAD8, "Numpad8"},
90 {VKEY_NUMPAD9, "Numpad9"},
91 {VKEY_MULTIPLY, "NumpadMultiply"},
92 {VKEY_ADD, "NumpadAdd"},
93 {VKEY_SUBTRACT, "NumpadSubtract"},
94 {VKEY_DECIMAL, "NumpadDecimal"},
95 {VKEY_DIVIDE, "NumpadDivide"},
96 {VKEY_F1, "F1"},
97 {VKEY_F2, "F2"},
98 {VKEY_F3, "F3"},
99 {VKEY_F4, "F4"},
100 {VKEY_F5, "F5"},
101 {VKEY_F6, "F6"},
102 {VKEY_F7, "F7"},
103 {VKEY_F8, "F8"},
104 {VKEY_F9, "F9"},
105 {VKEY_F10, "F10"},
106 {VKEY_F11, "F11"},
107 {VKEY_F12, "F12"},
108 {VKEY_F13, "F13"},
109 {VKEY_F14, "F14"},
110 {VKEY_F15, "F15"},
111 {VKEY_F16, "F16"},
112 {VKEY_F17, "F17"},
113 {VKEY_F18, "F18"},
114 {VKEY_F19, "F19"},
115 {VKEY_F20, "F20"},
116 {VKEY_F21, "F21"},
117 {VKEY_F22, "F22"},
118 {VKEY_F23, "F23"},
119 {VKEY_F24, "F24"},
120 {VKEY_NUMLOCK, "NumLock"},
121 {VKEY_SCROLL, "ScrollLock"},
122 {VKEY_LSHIFT, "ShiftLeft"},
123 {VKEY_RSHIFT, "ShiftRight"},
124 {VKEY_LCONTROL, "ControlLeft"},
125 {VKEY_RCONTROL, "ControlRight"},
126 {VKEY_LMENU, "AltLeft"},
127 {VKEY_RMENU, "AltRight"},
128 {VKEY_BROWSER_BACK, "BrowserBack"},
129 {VKEY_BROWSER_FORWARD, "BrowserForward"},
130 {VKEY_BROWSER_REFRESH, "BrowserRefresh"},
131 {VKEY_BROWSER_STOP, "BrowserStop"},
132 {VKEY_BROWSER_SEARCH, "BrowserSearch"},
133 {VKEY_BROWSER_HOME, "BrowserHome"},
134 {VKEY_VOLUME_MUTE, "VolumeMute"},
135 {VKEY_VOLUME_DOWN, "VolumeDown"},
136 {VKEY_VOLUME_UP, "VolumeUp"},
137 {VKEY_BRIGHTNESS_DOWN, "BrightnessDown"},
138 {VKEY_BRIGHTNESS_UP, "BrightnessUp"},
139 {VKEY_MEDIA_LAUNCH_APP1, "ChromeOSSwitchWindow"},
140 {VKEY_MEDIA_LAUNCH_APP2, "ChromeOSFullscreen"},
141 {VKEY_MEDIA_NEXT_TRACK, "MediaTrackNext"},
142 {VKEY_MEDIA_PREV_TRACK, "MediaTrackPrevious"},
143 {VKEY_MEDIA_STOP, "MediaStop"},
144 {VKEY_MEDIA_PLAY_PAUSE, "MediaPlayPause"},
145 {VKEY_MEDIA_LAUNCH_MAIL, "LaunchMail"},
146 {VKEY_OEM_1, "Semicolon"},
147 {VKEY_OEM_PLUS, "Equal"},
148 {VKEY_OEM_COMMA, "Comma"},
149 {VKEY_OEM_MINUS, "Minus"},
150 {VKEY_OEM_PERIOD, "Period"},
151 {VKEY_OEM_2, "Slash"},
152 {VKEY_OEM_3, "Backquote"},
153 {VKEY_OEM_4, "BracketLeft"},
154 {VKEY_OEM_5, "Backslash"},
155 {VKEY_OEM_6, "BracketRight"},
156 {VKEY_OEM_7, "Quote"}};
158 class KeyCodeMap {
159 public:
160 KeyCodeMap() {
161 for (size_t i = 0; i < arraysize(kKeyCodeTable); ++i) {
162 map_dom_key_[kKeyCodeTable[i].dom_code] = kKeyCodeTable[i].keyboard_code;
163 map_key_dom_[kKeyCodeTable[i].keyboard_code] = kKeyCodeTable[i].dom_code;
167 KeyboardCode GetKeyboardCode(const std::string& dom_code) const {
168 std::map<std::string, KeyboardCode>::const_iterator it =
169 map_dom_key_.find(dom_code);
170 return (it == map_dom_key_.end()) ? VKEY_UNKNOWN : it->second;
173 std::string GetDomKeycode(KeyboardCode key_code) const {
174 std::map<KeyboardCode, std::string>::const_iterator it =
175 map_key_dom_.find(key_code);
176 return (it == map_key_dom_.end()) ? "" : it->second;
179 private:
180 std::map<std::string, KeyboardCode> map_dom_key_;
181 std::map<KeyboardCode, std::string> map_key_dom_;
184 base::LazyInstance<KeyCodeMap>::Leaky g_keycode_map =
185 LAZY_INSTANCE_INITIALIZER;
187 } // namespace
189 KeyboardCode DomKeycodeToKeyboardCode(const std::string& code) {
190 return g_keycode_map.Get().GetKeyboardCode(code);
193 std::string KeyboardCodeToDomKeycode(KeyboardCode code) {
194 return g_keycode_map.Get().GetDomKeycode(code);
197 } // namespace ui