Set key and code on keyboard events

This commit is contained in:
Kevin Sawicki 2016-10-07 12:46:33 -07:00
parent d69367aa9b
commit 6fac14ad3f
3 changed files with 48 additions and 4 deletions

View file

@ -7,6 +7,8 @@
#include "atom/common/keyboard_util.h" #include "atom/common/keyboard_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/events/event_constants.h"
namespace atom { namespace atom {
@ -174,4 +176,33 @@ ui::KeyboardCode KeyboardCodeFromStr(const std::string& str, bool* shifted) {
return KeyboardCodeFromKeyIdentifier(str, 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 } // namespace atom

View file

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

View file

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