Skip to content

Commit 11af4b6

Browse files
committed
Merge pull request electron#2918 from brenca/master
Option to specify button on a MouseEvent and text on a KeyboardEvent when using sendInputEvent
2 parents c18fa63 + 44ee74a commit 11af4b6

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

atom/common/native_mate_converters/blink_converter.cc

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "atom/common/keyboad_util.h"
1111
#include "base/strings/string_util.h"
12+
#include "base/strings/utf_string_conversions.h"
1213
#include "content/public/browser/native_web_keyboard_event.h"
1314
#include "native_mate/dictionary.h"
1415
#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h"
@@ -29,10 +30,10 @@ int VectorToBitArray(const std::vector<T>& vec) {
2930
namespace mate {
3031

3132
template<>
32-
struct Converter<char> {
33+
struct Converter<base::char16> {
3334
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
34-
char* out) {
35-
std::string code = base::StringToLowerASCII(V8ToString(val));
35+
base::char16* out) {
36+
base::string16 code = base::UTF8ToUTF16(V8ToString(val));
3637
if (code.length() != 1)
3738
return false;
3839
*out = code[0];
@@ -77,6 +78,21 @@ struct Converter<blink::WebInputEvent::Type> {
7778
}
7879
};
7980

81+
template<>
82+
struct Converter<blink::WebMouseEvent::Button> {
83+
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
84+
blink::WebMouseEvent::Button* out) {
85+
std::string button = base::StringToLowerASCII(V8ToString(val));
86+
if (button == "left")
87+
*out = blink::WebMouseEvent::Button::ButtonLeft;
88+
else if (button == "middle")
89+
*out = blink::WebMouseEvent::Button::ButtonMiddle;
90+
else if (button == "right")
91+
*out = blink::WebMouseEvent::Button::ButtonRight;
92+
return true;
93+
}
94+
};
95+
8096
template<>
8197
struct Converter<blink::WebInputEvent::Modifiers> {
8298
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
@@ -142,16 +158,19 @@ bool Converter<blink::WebKeyboardEvent>::FromV8(
142158
return false;
143159
if (!ConvertFromV8(isolate, val, static_cast<blink::WebInputEvent*>(out)))
144160
return false;
145-
char code;
161+
base::char16 code;
146162
if (!dict.Get("keyCode", &code))
147163
return false;
148164
bool shifted = false;
149165
out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted);
150-
if (out->windowsKeyCode == ui::VKEY_UNKNOWN)
151-
return false;
152166
if (shifted)
153167
out->modifiers |= blink::WebInputEvent::ShiftKey;
154168
out->setKeyIdentifierFromWindowsKeyCode();
169+
if (out->type == blink::WebInputEvent::Char
170+
|| out->type == blink::WebInputEvent::RawKeyDown) {
171+
out->text[0] = code;
172+
out->unmodifiedText[0] = code;
173+
}
155174
return true;
156175
}
157176

@@ -176,6 +195,7 @@ bool Converter<blink::WebMouseEvent>::FromV8(
176195
return false;
177196
if (!dict.Get("x", &out->x) || !dict.Get("y", &out->y))
178197
return false;
198+
dict.Get("button", &out->button);
179199
dict.Get("globalX", &out->globalX);
180200
dict.Get("globalY", &out->globalY);
181201
dict.Get("movementX", &out->movementX);

docs/api/web-contents.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,13 @@ Sends an input `event` to the page.
534534
For keyboard events, the `event` object also have following properties:
535535

536536
* `keyCode` String (**required**) - A single character that will be sent as
537-
keyboard event. Can be any ASCII character on the keyboard, like `a`, `1`
538-
and `=`.
537+
keyboard event. Can be any UTF-8 character.
539538

540539
For mouse events, the `event` object also have following properties:
541540

542541
* `x` Integer (**required**)
543542
* `y` Integer (**required**)
543+
* `button` String - The button pressed, can be `left`, `middle`, `right`
544544
* `globalX` Integer
545545
* `globalY` Integer
546546
* `movementX` Integer

0 commit comments

Comments
 (0)