From bb6d7d14989142f3639b6c51f6f91b61bb9d5386 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Mon, 16 Nov 2015 04:15:13 +0100 Subject: [PATCH 1/3] Added the option to `KeyboardEvent` to specify the sent character by it's name (if it can't be sent via the `keyCode` parameter) --- atom/common/keyboad_util.cc | 27 ++++++++++++++++++- atom/common/keyboad_util.h | 7 ++++- .../native_mate_converters/blink_converter.cc | 13 ++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/atom/common/keyboad_util.cc b/atom/common/keyboad_util.cc index 29d1a800c859..942637786198 100644 --- a/atom/common/keyboad_util.cc +++ b/atom/common/keyboad_util.cc @@ -2,12 +2,13 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#include #include "atom/common/keyboad_util.h" namespace atom { // Return key code of the char. -ui::KeyboardCode KeyboardCodeFromCharCode(char c, bool* shifted) { +ui::KeyboardCode KeyboardCodeFromCharCode(base::char16 c, bool* shifted) { *shifted = false; switch (c) { case 0x08: return ui::VKEY_BACK; @@ -71,4 +72,28 @@ ui::KeyboardCode KeyboardCodeFromCharCode(char c, bool* shifted) { } } +// Return key code of the char. +ui::KeyboardCode KeyboardCodeFromKeyIdentifier(std::string chr) { + if (chr == "enter") return ui::VKEY_RETURN; + if (chr == "backspace") return ui::VKEY_BACK; + if (chr == "delete") return ui::VKEY_DELETE; + if (chr == "tab") return ui::VKEY_TAB; + if (chr == "escape") return ui::VKEY_ESCAPE; + if (chr == "control") return ui::VKEY_CONTROL; + if (chr == "alt") return ui::VKEY_MENU; + if (chr == "shift") return ui::VKEY_SHIFT; + if (chr == "end") return ui::VKEY_END; + if (chr == "home") return ui::VKEY_HOME; + if (chr == "insert") return ui::VKEY_INSERT; + if (chr == "left") return ui::VKEY_LEFT; + if (chr == "up") return ui::VKEY_UP; + if (chr == "right") return ui::VKEY_RIGHT; + if (chr == "down") return ui::VKEY_DOWN; + if (chr == "pageup") return ui::VKEY_PRIOR; + if (chr == "pagedown") return ui::VKEY_NEXT; + if (chr == "printscreen") return ui::VKEY_SNAPSHOT; + + return ui::VKEY_UNKNOWN; +} + } // namespace atom diff --git a/atom/common/keyboad_util.h b/atom/common/keyboad_util.h index 0496886e40bd..78e8021547d9 100644 --- a/atom/common/keyboad_util.h +++ b/atom/common/keyboad_util.h @@ -5,13 +5,18 @@ #ifndef ATOM_COMMON_KEYBOAD_UTIL_H_ #define ATOM_COMMON_KEYBOAD_UTIL_H_ +#include #include "ui/events/keycodes/keyboard_codes.h" +#include "base/strings/string_util.h" namespace atom { // Return key code of the char, and also determine whether the SHIFT key is // pressed. -ui::KeyboardCode KeyboardCodeFromCharCode(char c, bool* shifted); +ui::KeyboardCode KeyboardCodeFromCharCode(base::char16 c, bool* shifted); + +// Return key code of the char from a string representation of the char +ui::KeyboardCode KeyboardCodeFromKeyIdentifier(std::string chr); } // namespace atom diff --git a/atom/common/native_mate_converters/blink_converter.cc b/atom/common/native_mate_converters/blink_converter.cc index fcfc8905b3e2..e82b656e26ea 100644 --- a/atom/common/native_mate_converters/blink_converter.cc +++ b/atom/common/native_mate_converters/blink_converter.cc @@ -159,10 +159,17 @@ bool Converter::FromV8( if (!ConvertFromV8(isolate, val, static_cast(out))) return false; base::char16 code; - if (!dict.Get("keyCode", &code)) - return false; + std::string identifier; bool shifted = false; - out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted); + + if (dict.Get("keyCode", &code)) + out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted); + else if (dict.Get("keyIdentifier", &identifier)) + out->windowsKeyCode = atom::KeyboardCodeFromKeyIdentifier( + base::StringToLowerASCII(identifier)); + else + return false; + if (shifted) out->modifiers |= blink::WebInputEvent::ShiftKey; out->setKeyIdentifierFromWindowsKeyCode(); From 5fff06d2c29c9b1ecf3b169033c472895e03de45 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Mon, 16 Nov 2015 04:22:37 +0100 Subject: [PATCH 2/3] Added documentation for the changes in KeyboardEvent and added missing mouseMove event type --- docs/api/web-contents.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 917e0670590c..50d39ac91784 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -584,7 +584,7 @@ Disable device emulation enabled by `webContents.enableDeviceEmulation`. * `event` Object * `type` String (**required**) - The type of the event, can be `mouseDown`, `mouseUp`, `mouseEnter`, `mouseLeave`, `contextMenu`, `mouseWheel`, - `keyDown`, `keyUp`, `char`. + `mouseMove`, `keyDown`, `keyUp`, `char`. * `modifiers` Array - An array of modifiers of the event, can include `shift`, `control`, `alt`, `meta`, `isKeypad`, `isAutoRepeat`, `leftButtonDown`, `middleButtonDown`, `rightButtonDown`, `capsLock`, @@ -593,9 +593,14 @@ Disable device emulation enabled by `webContents.enableDeviceEmulation`. Sends an input `event` to the page. For keyboard events, the `event` object also have following properties: +(only one of `keyCode` and `keyIdentifier` is required) -* `keyCode` String (**required**) - A single character that will be sent as +* `keyCode` Char (**required**) - A single character that will be sent as keyboard event. Can be any UTF-8 character. +* `keyIdentifier` String (**required**) - A text representation of the character + that will be sent as keyboard event, can be `Enter`, `Backspace`, `Delete`, + `Tab`, `Escape`, `Control`, `Alt`, `Shift`, `End`, `Home`, `Insert`, `Left`, + `Up`, `Right`, `Down`, `PageUp`, `PageDown`, `PrintScreen` For mouse events, the `event` object also have following properties: From 14740eeb8a82ace268bc105e84d25f53e1b73d52 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Mon, 16 Nov 2015 14:51:26 +0100 Subject: [PATCH 3/3] Merging keyCode and keyIdentifier and adjusting the docs accordingly --- atom/common/keyboad_util.cc | 2 +- atom/common/keyboad_util.h | 2 +- .../common/native_mate_converters/blink_converter.cc | 2 +- docs/api/web-contents.md | 12 +++++------- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/atom/common/keyboad_util.cc b/atom/common/keyboad_util.cc index 942637786198..7d7c5d99fabe 100644 --- a/atom/common/keyboad_util.cc +++ b/atom/common/keyboad_util.cc @@ -73,7 +73,7 @@ ui::KeyboardCode KeyboardCodeFromCharCode(base::char16 c, bool* shifted) { } // Return key code of the char. -ui::KeyboardCode KeyboardCodeFromKeyIdentifier(std::string chr) { +ui::KeyboardCode KeyboardCodeFromKeyIdentifier(const std::string& chr) { if (chr == "enter") return ui::VKEY_RETURN; if (chr == "backspace") return ui::VKEY_BACK; if (chr == "delete") return ui::VKEY_DELETE; diff --git a/atom/common/keyboad_util.h b/atom/common/keyboad_util.h index 78e8021547d9..4a85c190635d 100644 --- a/atom/common/keyboad_util.h +++ b/atom/common/keyboad_util.h @@ -16,7 +16,7 @@ namespace atom { ui::KeyboardCode KeyboardCodeFromCharCode(base::char16 c, bool* shifted); // Return key code of the char from a string representation of the char -ui::KeyboardCode KeyboardCodeFromKeyIdentifier(std::string chr); +ui::KeyboardCode KeyboardCodeFromKeyIdentifier(const std::string& chr); } // namespace atom diff --git a/atom/common/native_mate_converters/blink_converter.cc b/atom/common/native_mate_converters/blink_converter.cc index e82b656e26ea..2c871276ba26 100644 --- a/atom/common/native_mate_converters/blink_converter.cc +++ b/atom/common/native_mate_converters/blink_converter.cc @@ -164,7 +164,7 @@ bool Converter::FromV8( if (dict.Get("keyCode", &code)) out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted); - else if (dict.Get("keyIdentifier", &identifier)) + else if (dict.Get("keyCode", &identifier)) out->windowsKeyCode = atom::KeyboardCodeFromKeyIdentifier( base::StringToLowerASCII(identifier)); else diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 50d39ac91784..6acfae5c03be 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -593,14 +593,12 @@ Disable device emulation enabled by `webContents.enableDeviceEmulation`. Sends an input `event` to the page. For keyboard events, the `event` object also have following properties: -(only one of `keyCode` and `keyIdentifier` is required) -* `keyCode` Char (**required**) - A single character that will be sent as - keyboard event. Can be any UTF-8 character. -* `keyIdentifier` String (**required**) - A text representation of the character - that will be sent as keyboard event, can be `Enter`, `Backspace`, `Delete`, - `Tab`, `Escape`, `Control`, `Alt`, `Shift`, `End`, `Home`, `Insert`, `Left`, - `Up`, `Right`, `Down`, `PageUp`, `PageDown`, `PrintScreen` +* `keyCode` Char or String (**required**) - The character that will be sent + as the keyboard event. Can be a single UTF-8 character, or the name of the + key that generates the event. Accepted key names are `enter`, `backspace`, + `delete`, `tab`, `escape`, `control`, `alt`, `shift`, `end`, `home`, `insert`, + `left`, `up`, `right`, `down`, `pageUp`, `pageDown`, `printScreen` For mouse events, the `event` object also have following properties: