Browse Source

Set key and code on keyboard events

Kevin Sawicki 8 years ago
parent
commit
6fac14ad3f

+ 31 - 0
atom/common/keyboard_util.cc

@@ -7,6 +7,8 @@
 #include "atom/common/keyboard_util.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_util.h"
+#include "third_party/WebKit/public/web/WebInputEvent.h"
+#include "ui/events/event_constants.h"
 
 namespace atom {
 
@@ -174,4 +176,33 @@ ui::KeyboardCode KeyboardCodeFromStr(const std::string& str, bool* shifted) {
     return KeyboardCodeFromKeyIdentifier(str, shifted);
 }
 
+int WebEventModifiersToEventFlags(int modifiers) {
+  int flags = 0;
+
+  if (modifiers & blink::WebInputEvent::ShiftKey)
+    flags |= ui::EF_SHIFT_DOWN;
+  if (modifiers & blink::WebInputEvent::ControlKey)
+    flags |= ui::EF_CONTROL_DOWN;
+  if (modifiers & blink::WebInputEvent::AltKey)
+    flags |= ui::EF_ALT_DOWN;
+  if (modifiers & blink::WebInputEvent::MetaKey)
+    flags |= ui::EF_COMMAND_DOWN;
+  if (modifiers & blink::WebInputEvent::CapsLockOn)
+    flags |= ui::EF_CAPS_LOCK_ON;
+  if (modifiers & blink::WebInputEvent::NumLockOn)
+    flags |= ui::EF_NUM_LOCK_ON;
+  if (modifiers & blink::WebInputEvent::ScrollLockOn)
+    flags |= ui::EF_SCROLL_LOCK_ON;
+  if (modifiers & blink::WebInputEvent::LeftButtonDown)
+    flags |= ui::EF_LEFT_MOUSE_BUTTON;
+  if (modifiers & blink::WebInputEvent::MiddleButtonDown)
+    flags |= ui::EF_MIDDLE_MOUSE_BUTTON;
+  if (modifiers & blink::WebInputEvent::RightButtonDown)
+    flags |= ui::EF_RIGHT_MOUSE_BUTTON;
+  if (modifiers & blink::WebInputEvent::IsAutoRepeat)
+    flags |= ui::EF_IS_REPEAT;
+
+  return flags;
+}
+
 }  // namespace atom

+ 2 - 0
atom/common/keyboard_util.h

@@ -15,6 +15,8 @@ namespace atom {
 // pressed.
 ui::KeyboardCode KeyboardCodeFromStr(const std::string& str, bool* shifted);
 
+int WebEventModifiersToEventFlags(int modifiers);
+
 }  // namespace atom
 
 #endif  // ATOM_COMMON_KEYBOARD_UTIL_H_

+ 15 - 4
atom/common/native_mate_converters/blink_converter.cc

@@ -18,6 +18,7 @@
 #include "third_party/WebKit/public/web/WebFindOptions.h"
 #include "third_party/WebKit/public/web/WebInputEvent.h"
 #include "ui/base/clipboard/clipboard.h"
+#include "ui/events/keycodes/keyboard_code_conversion.h"
 
 namespace {
 
@@ -166,15 +167,25 @@ bool Converter<blink::WebKeyboardEvent>::FromV8(
     return false;
 
   std::string str;
-  bool shifted = false;
-  if (dict.Get("keyCode", &str))
-    out->windowsKeyCode = atom::KeyboardCodeFromStr(str, &shifted);
-  else
+  if (!dict.Get("keyCode", &str))
     return false;
 
+  bool shifted = false;
+  ui::KeyboardCode keyCode = atom::KeyboardCodeFromStr(str, &shifted);
+  out->windowsKeyCode = keyCode;
   if (shifted)
     out->modifiers |= blink::WebInputEvent::ShiftKey;
   out->setKeyIdentifierFromWindowsKeyCode();
+
+  ui::DomCode domCode = ui::UsLayoutKeyboardCodeToDomCode(keyCode);
+  out->domCode = static_cast<int>(domCode);
+
+  ui::DomKey domKey;
+  ui::KeyboardCode dummy_code;
+  int flags = atom::WebEventModifiersToEventFlags(out->modifiers);
+  if (ui::DomCodeToUsLayoutDomKey(domCode, flags, &domKey, &dummy_code))
+    out->domKey = static_cast<int>(domKey);
+
   if ((out->type == blink::WebInputEvent::Char ||
        out->type == blink::WebInputEvent::RawKeyDown)) {
     // Make sure to not read beyond the buffer in case some bad code doesn't