Index: ui/events/keycodes/keyboard_lookup_win.cc |
diff --git a/ui/events/keycodes/keyboard_lookup_win.cc b/ui/events/keycodes/keyboard_lookup_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fe5d8b84a27394c61c3ac97ab596785153a31880 |
--- /dev/null |
+++ b/ui/events/keycodes/keyboard_lookup_win.cc |
@@ -0,0 +1,186 @@ |
+// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/events/keycodes/keyboard_lookup_win.h" |
+ |
+#include "base/logging.h" |
+#include "base/macros.h" |
+ |
+namespace ui { |
+ |
+namespace { |
+ |
+// List of flags we care about. |
+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...
|
+ 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
|
+ EF_SHIFT_DOWN, |
+ EF_CONTROL_DOWN, |
+ EF_ALT_DOWN, |
+ EF_COMMAND_DOWN, |
+ EF_ALTGR_DOWN, |
+ EF_NUM_LOCK_ON, |
+ EF_SCROLL_LOCK_ON |
+}; |
+ |
+size_t GetEventFlagsIndex(int event_flags) { |
+ size_t index = 0; |
+ for (size_t i = 0; i < arraysize(event_flags_index); ++i) { |
+ if (event_flags & event_flags_index[i]) |
+ index |= 1 << i; |
+ } |
+ return index; |
+} |
+ |
+int EventFlagsFromIndex(size_t index) { |
+ int event_flags = EF_NONE; |
+ for (size_t i = 0; i < arraysize(event_flags_index); ++i) { |
+ if (index & (1 << i)) |
+ event_flags |= event_flags_index[i]; |
+ } |
+ return event_flags; |
+} |
+ |
+void SetModifierState(BYTE *keyboard_state, int event_flags) { |
+ // 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
|
+ if (event_flags & EF_SHIFT_DOWN) { |
+ keyboard_state[VK_SHIFT] |= 0x80; |
+ } else { |
+ keyboard_state[VK_SHIFT] &= ~0x80; |
+ keyboard_state[VK_LSHIFT] &= ~0x80; |
+ 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...
|
+ } |
+ |
+ if (event_flags & EF_CONTROL_DOWN) { |
+ keyboard_state[VK_CONTROL] |= 0x80; |
+ } else { |
+ keyboard_state[VK_CONTROL] &= ~0x80; |
+ keyboard_state[VK_LCONTROL] &= ~0x80; |
+ keyboard_state[VK_RCONTROL] &= ~0x80; |
+ } |
+ |
+ if (event_flags & EF_ALT_DOWN) { |
+ keyboard_state[VK_MENU] |= 0x80; |
+ } else { |
+ keyboard_state[VK_MENU] &= ~0x80; |
+ keyboard_state[VK_LMENU] &= ~0x80; |
+ keyboard_state[VK_RMENU] &= ~0x80; |
+ } |
+ |
+ if (event_flags & EF_ALTGR_DOWN) { |
+ keyboard_state[VK_MENU] |= 0x80; |
+ keyboard_state[VK_CONTROL] |= 0x80; |
+ } else { |
+ // 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
|
+ } |
+ |
+ if (event_flags & EF_COMMAND_DOWN) { |
+ keyboard_state[VK_LWIN] |= 0x80; |
+ } else { |
+ keyboard_state[VK_LWIN] &= ~0x80; |
+ keyboard_state[VK_RWIN] &= ~0x80; |
+ } |
+ |
+ if (event_flags & EF_CAPS_LOCK_ON) { |
+ keyboard_state[VK_CAPITAL] |= 0x01; |
+ } else { |
+ keyboard_state[VK_CAPITAL] &= ~0x01; |
+ } |
+ |
+ if (event_flags & EF_NUM_LOCK_ON) { |
+ keyboard_state[VK_NUMLOCK] |= 0x01; |
+ } else { |
+ keyboard_state[VK_NUMLOCK] &= ~0x01; |
+ } |
+ |
+ if (event_flags & EF_SCROLL_LOCK_ON) { |
+ keyboard_state[VK_SCROLL] |= 0x01; |
+ } else { |
+ keyboard_state[VK_SCROLL] &= ~0x01; |
+ } |
+ |
+ // TODO(chongz): EF_EXTENDED |
Wez
2016/01/29 00:22:23
???
chongz
2016/01/29 20:25:49
Not necessary, will remove...
|
+} |
+ |
+} // anonymous namespace |
+ |
+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.
|
+ |
+WindowsKeyboardLookup::WindowsKeyboardLookup() |
+ : 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.
|
+ static_assert((1 << arraysize(event_flags_index)) |
+ <= 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
|
+} |
+ |
+WindowsKeyboardLookup::WindowsKeyboardLookup(HKL alayout) |
+ : WindowsKeyboardLookup() { |
+ CheckOrLoadLayout(alayout); |
+} |
+ |
+DomKey WindowsKeyboardLookup::VirtualKeyToCurrentLayoutDomKey( |
+ KeyboardCode vkcode, |
+ int event_flags) { |
+ 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
|
+ return s_instance.VirtualKeyToDomKey(vkcode, event_flags); |
+} |
+ |
+DomKey WindowsKeyboardLookup::VirtualKeyToDomKey(KeyboardCode vkcode, |
+ int event_flags) const { |
+ if (vkcode >= kMaxVirtualKeyCode) |
+ 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.
|
+ DomKey key = lookup_table_[vkcode][GetEventFlagsIndex(event_flags)]; |
+ if (key == DomKey::NONE) { |
+ static const int fallback_flags[] = { |
+ // 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)
|
+ EF_SHIFT_DOWN | EF_ALTGR_DOWN | EF_CAPS_LOCK_ON, |
+ EF_SHIFT_DOWN | EF_CAPS_LOCK_ON, |
+ EF_NONE, |
+ }; |
+ for (const auto &fallback : fallback_flags) { |
+ key = lookup_table_[vkcode][GetEventFlagsIndex(event_flags & fallback)]; |
+ if (key != DomKey::NONE) |
+ break; |
+ } |
+ } |
+ return key; |
+} |
+ |
+void WindowsKeyboardLookup::CheckOrLoadLayout(HKL alayout) { |
+ if (alayout == keyboard_layout_) |
+ return; |
+ keyboard_layout_ = alayout; |
+ |
+ BYTE keyboard_state[256]; |
+ 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.
|
+ |
+ memset(keyboard_state, 0, sizeof(keyboard_state)); |
+ ::GetKeyboardState(original_keyboard_state); |
+ |
+ for (size_t eindex = 0; eindex < kMaxKeyboardModifier; ++eindex) { |
+ SetModifierState(keyboard_state, EventFlagsFromIndex(eindex)); |
+ for (size_t vk = 0; vk < kMaxVirtualKeyCode; ++vk) { |
+ 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.
|
+ |
+ int rv = ::ToUnicodeEx(vk, 0, keyboard_state, unibuff, arraysize(unibuff), |
+ 0, keyboard_layout_); |
+ |
+ if (rv < 0) { |
+ // 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.
|
+ rv = ::ToUnicodeEx(vk, 0, keyboard_state, unibuff, arraysize(unibuff), |
+ 0, keyboard_layout_); |
+ // Must be a repeat of dead key character. |
+ DCHECK(rv == 2); |
+ lookup_table_[vk][eindex] = DomKey::DeadKeyFromCombiningCharacter( |
+ 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.
|
+ } else if (rv == 1 && unibuff[0] >= 0x20) { |
+ // 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...
|
+ lookup_table_[vk][eindex] = DomKey::FromCharacter(unibuff[0]); |
+ } else { |
+ lookup_table_[vk][eindex] = DomKey::NONE; |
+ } |
+ } |
+ } |
+ ::SetKeyboardState(original_keyboard_state); |
+} |
+ |
+} // namespace ui |