OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 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. | |
4 | |
5 #include "ui/events/keycodes/keyboard_lookup_win.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 | |
10 namespace ui { | |
11 | |
12 namespace { | |
13 | |
14 // List of flags we care about. | |
15 const EventFlags event_flags_index[] = { | |
Wez
2016/01/29 00:22:24
nit: Use a compile-time check to ensure that this
chongz
2016/01/29 20:25:49
Tried here but kMaxKeyboardModifier is private...
| |
16 EF_CAPS_LOCK_ON, | |
Wez
2016/01/29 00:22:23
nit: Why have you moved CAPS_LOCK_ON to appear fir
chongz
2016/01/29 20:25:49
Sorry I didn't notice that the order was changed o
| |
17 EF_SHIFT_DOWN, | |
18 EF_CONTROL_DOWN, | |
19 EF_ALT_DOWN, | |
20 EF_COMMAND_DOWN, | |
21 EF_ALTGR_DOWN, | |
22 EF_NUM_LOCK_ON, | |
23 EF_SCROLL_LOCK_ON | |
24 }; | |
25 | |
26 size_t GetEventFlagsIndex(int event_flags) { | |
27 size_t index = 0; | |
28 for (size_t i = 0; i < arraysize(event_flags_index); ++i) { | |
29 if (event_flags & event_flags_index[i]) | |
30 index |= 1 << i; | |
31 } | |
32 return index; | |
33 } | |
34 | |
35 int EventFlagsFromIndex(size_t index) { | |
36 int event_flags = EF_NONE; | |
37 for (size_t i = 0; i < arraysize(event_flags_index); ++i) { | |
38 if (index & (1 << i)) | |
39 event_flags |= event_flags_index[i]; | |
40 } | |
41 return event_flags; | |
42 } | |
43 | |
44 void SetModifierState(BYTE *keyboard_state, int event_flags) { | |
45 // Trying to match system_event_state_lookup.cc. | |
Wez
2016/01/29 00:22:23
This comment should be more specific about the beh
chongz
2016/01/29 20:25:49
Maybe
// Trying to recover keyboard_state using t
| |
46 if (event_flags & EF_SHIFT_DOWN) { | |
47 keyboard_state[VK_SHIFT] |= 0x80; | |
48 } else { | |
49 keyboard_state[VK_SHIFT] &= ~0x80; | |
50 keyboard_state[VK_LSHIFT] &= ~0x80; | |
51 keyboard_state[VK_RSHIFT] &= ~0x80; | |
Wez
2016/01/29 00:22:23
Why do we set the non-located key state, but clear
chongz
2016/01/29 20:25:49
Will do memset() to reset state...
| |
52 } | |
53 | |
54 if (event_flags & EF_CONTROL_DOWN) { | |
55 keyboard_state[VK_CONTROL] |= 0x80; | |
56 } else { | |
57 keyboard_state[VK_CONTROL] &= ~0x80; | |
58 keyboard_state[VK_LCONTROL] &= ~0x80; | |
59 keyboard_state[VK_RCONTROL] &= ~0x80; | |
60 } | |
61 | |
62 if (event_flags & EF_ALT_DOWN) { | |
63 keyboard_state[VK_MENU] |= 0x80; | |
64 } else { | |
65 keyboard_state[VK_MENU] &= ~0x80; | |
66 keyboard_state[VK_LMENU] &= ~0x80; | |
67 keyboard_state[VK_RMENU] &= ~0x80; | |
68 } | |
69 | |
70 if (event_flags & EF_ALTGR_DOWN) { | |
71 keyboard_state[VK_MENU] |= 0x80; | |
72 keyboard_state[VK_CONTROL] |= 0x80; | |
73 } else { | |
74 // Windows does not have a specific key code for AltGr. | |
Wez
2016/01/29 00:22:23
This comment seems misplaced.. looks like it belon
chongz
2016/01/29 20:25:49
Sorry I didn't know that before, will change to Ri
chongz
2016/02/02 21:14:04
I'm not sure why but only the non-located Alt+Cont
| |
75 } | |
76 | |
77 if (event_flags & EF_COMMAND_DOWN) { | |
78 keyboard_state[VK_LWIN] |= 0x80; | |
79 } else { | |
80 keyboard_state[VK_LWIN] &= ~0x80; | |
81 keyboard_state[VK_RWIN] &= ~0x80; | |
82 } | |
83 | |
84 if (event_flags & EF_CAPS_LOCK_ON) { | |
85 keyboard_state[VK_CAPITAL] |= 0x01; | |
86 } else { | |
87 keyboard_state[VK_CAPITAL] &= ~0x01; | |
88 } | |
89 | |
90 if (event_flags & EF_NUM_LOCK_ON) { | |
91 keyboard_state[VK_NUMLOCK] |= 0x01; | |
92 } else { | |
93 keyboard_state[VK_NUMLOCK] &= ~0x01; | |
94 } | |
95 | |
96 if (event_flags & EF_SCROLL_LOCK_ON) { | |
97 keyboard_state[VK_SCROLL] |= 0x01; | |
98 } else { | |
99 keyboard_state[VK_SCROLL] &= ~0x01; | |
100 } | |
101 | |
102 // TODO(chongz): EF_EXTENDED | |
Wez
2016/01/29 00:22:23
???
chongz
2016/01/29 20:25:49
Not necessary, will remove...
| |
103 } | |
104 | |
105 } // anonymous namespace | |
106 | |
107 WindowsKeyboardLookup WindowsKeyboardLookup::s_instance; | |
Wez
2016/01/29 00:22:23
I think this will cause a static initializer, so u
chongz
2016/01/29 20:25:49
Acknowledged.
| |
108 | |
109 WindowsKeyboardLookup::WindowsKeyboardLookup() | |
110 : keyboard_layout_(nullptr) { | |
Wez
2016/01/29 00:22:23
nit: HKL is a typedef of HANDLE, for which INVALID
chongz
2016/01/29 20:25:49
Acknowledged.
| |
111 static_assert((1 << arraysize(event_flags_index)) | |
112 <= WindowsKeyboardLookup::kMaxKeyboardModifier, "Need larger table"); | |
Wez
2016/01/29 00:22:23
nit: Change this to an equality check; there's no
chongz
2016/01/29 20:25:49
Maybe make kMaxKeyboardModifier public and move to
| |
113 } | |
114 | |
115 WindowsKeyboardLookup::WindowsKeyboardLookup(HKL alayout) | |
116 : WindowsKeyboardLookup() { | |
117 CheckOrLoadLayout(alayout); | |
118 } | |
119 | |
120 DomKey WindowsKeyboardLookup::VirtualKeyToCurrentLayoutDomKey( | |
121 KeyboardCode vkcode, | |
122 int event_flags) { | |
123 s_instance.CheckOrLoadLayout(::GetKeyboardLayout(0)); | |
Wez
2016/01/29 00:22:23
What happens if this gets called by code from diff
chongz
2016/01/29 20:25:49
Should I add a lock or only allow it to be used by
chongz
2016/02/02 21:14:04
Still, should I add a lock or only allow it to be
| |
124 return s_instance.VirtualKeyToDomKey(vkcode, event_flags); | |
125 } | |
126 | |
127 DomKey WindowsKeyboardLookup::VirtualKeyToDomKey(KeyboardCode vkcode, | |
128 int event_flags) const { | |
129 if (vkcode >= kMaxVirtualKeyCode) | |
130 return DomKey::NONE; | |
Wez
2016/01/29 00:22:24
This if() will be optimized out by the compiler, s
chongz
2016/01/29 20:25:49
Will assume the input is valid.
| |
131 DomKey key = lookup_table_[vkcode][GetEventFlagsIndex(event_flags)]; | |
132 if (key == DomKey::NONE) { | |
133 static const int fallback_flags[] = { | |
134 // Trying to match Firefox's behavior. | |
Wez
2016/01/29 00:22:23
This doesn't look right; if I pressed a key w/out
chongz
2016/01/29 20:25:49
If there is no modifiers (event_flags == EF_NONE)
| |
135 EF_SHIFT_DOWN | EF_ALTGR_DOWN | EF_CAPS_LOCK_ON, | |
136 EF_SHIFT_DOWN | EF_CAPS_LOCK_ON, | |
137 EF_NONE, | |
138 }; | |
139 for (const auto &fallback : fallback_flags) { | |
140 key = lookup_table_[vkcode][GetEventFlagsIndex(event_flags & fallback)]; | |
141 if (key != DomKey::NONE) | |
142 break; | |
143 } | |
144 } | |
145 return key; | |
146 } | |
147 | |
148 void WindowsKeyboardLookup::CheckOrLoadLayout(HKL alayout) { | |
149 if (alayout == keyboard_layout_) | |
150 return; | |
151 keyboard_layout_ = alayout; | |
152 | |
153 BYTE keyboard_state[256]; | |
154 BYTE original_keyboard_state[256]; | |
Wez
2016/01/29 00:22:23
nit: Style-guide prefers that variables are declar
chongz
2016/01/29 20:25:49
Acknowledged.
| |
155 | |
156 memset(keyboard_state, 0, sizeof(keyboard_state)); | |
157 ::GetKeyboardState(original_keyboard_state); | |
158 | |
159 for (size_t eindex = 0; eindex < kMaxKeyboardModifier; ++eindex) { | |
160 SetModifierState(keyboard_state, EventFlagsFromIndex(eindex)); | |
161 for (size_t vk = 0; vk < kMaxVirtualKeyCode; ++vk) { | |
162 wchar_t unibuff[5]; | |
Wez
2016/01/29 00:22:23
nit: Please given this a descriptive name, as per
chongz
2016/01/29 20:25:49
Acknowledged.
| |
163 | |
164 int rv = ::ToUnicodeEx(vk, 0, keyboard_state, unibuff, arraysize(unibuff), | |
165 0, keyboard_layout_); | |
166 | |
167 if (rv < 0) { | |
168 // Dead key, repeat twice to get character representation. | |
Wez
2016/01/29 00:22:23
nit: IIRC you can always get a dead-key to generat
chongz
2016/01/29 20:25:49
Acknowledged.
| |
169 rv = ::ToUnicodeEx(vk, 0, keyboard_state, unibuff, arraysize(unibuff), | |
170 0, keyboard_layout_); | |
171 // Must be a repeat of dead key character. | |
172 DCHECK(rv == 2); | |
173 lookup_table_[vk][eindex] = DomKey::DeadKeyFromCombiningCharacter( | |
174 unibuff[0]); | |
Wez
2016/01/29 00:22:23
nit: Indentation looks off; run git cl format on t
chongz
2016/01/29 20:25:49
Acknowledged.
| |
175 } else if (rv == 1 && unibuff[0] >= 0x20) { | |
176 // Ignores legacy non-printalbe control characters. | |
Wez
2016/01/29 00:22:23
typo
This comment is confusing; IIUC what you're
chongz
2016/01/29 20:25:49
Yes we don't want those control characters, will r
chongz
2016/02/02 21:14:04
Added TODO to handle multiple characters later...
| |
177 lookup_table_[vk][eindex] = DomKey::FromCharacter(unibuff[0]); | |
178 } else { | |
179 lookup_table_[vk][eindex] = DomKey::NONE; | |
180 } | |
181 } | |
182 } | |
183 ::SetKeyboardState(original_keyboard_state); | |
184 } | |
185 | |
186 } // namespace ui | |
OLD | NEW |