Merge pull request #2918 from brenca/master

Option to specify button on a MouseEvent and text on a KeyboardEvent when using sendInputEvent
This commit is contained in:
Cheng Zhao 2015-09-28 12:47:22 +08:00
commit 11af4b63ac
2 changed files with 28 additions and 8 deletions

View file

@ -9,6 +9,7 @@
#include "atom/common/keyboad_util.h" #include "atom/common/keyboad_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/native_web_keyboard_event.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h" #include "third_party/WebKit/public/web/WebDeviceEmulationParams.h"
@ -29,10 +30,10 @@ int VectorToBitArray(const std::vector<T>& vec) {
namespace mate { namespace mate {
template<> template<>
struct Converter<char> { struct Converter<base::char16> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
char* out) { base::char16* out) {
std::string code = base::StringToLowerASCII(V8ToString(val)); base::string16 code = base::UTF8ToUTF16(V8ToString(val));
if (code.length() != 1) if (code.length() != 1)
return false; return false;
*out = code[0]; *out = code[0];
@ -77,6 +78,21 @@ struct Converter<blink::WebInputEvent::Type> {
} }
}; };
template<>
struct Converter<blink::WebMouseEvent::Button> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
blink::WebMouseEvent::Button* out) {
std::string button = base::StringToLowerASCII(V8ToString(val));
if (button == "left")
*out = blink::WebMouseEvent::Button::ButtonLeft;
else if (button == "middle")
*out = blink::WebMouseEvent::Button::ButtonMiddle;
else if (button == "right")
*out = blink::WebMouseEvent::Button::ButtonRight;
return true;
}
};
template<> template<>
struct Converter<blink::WebInputEvent::Modifiers> { struct Converter<blink::WebInputEvent::Modifiers> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
@ -142,16 +158,19 @@ bool Converter<blink::WebKeyboardEvent>::FromV8(
return false; return false;
if (!ConvertFromV8(isolate, val, static_cast<blink::WebInputEvent*>(out))) if (!ConvertFromV8(isolate, val, static_cast<blink::WebInputEvent*>(out)))
return false; return false;
char code; base::char16 code;
if (!dict.Get("keyCode", &code)) if (!dict.Get("keyCode", &code))
return false; return false;
bool shifted = false; bool shifted = false;
out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted); out->windowsKeyCode = atom::KeyboardCodeFromCharCode(code, &shifted);
if (out->windowsKeyCode == ui::VKEY_UNKNOWN)
return false;
if (shifted) if (shifted)
out->modifiers |= blink::WebInputEvent::ShiftKey; out->modifiers |= blink::WebInputEvent::ShiftKey;
out->setKeyIdentifierFromWindowsKeyCode(); out->setKeyIdentifierFromWindowsKeyCode();
if (out->type == blink::WebInputEvent::Char
|| out->type == blink::WebInputEvent::RawKeyDown) {
out->text[0] = code;
out->unmodifiedText[0] = code;
}
return true; return true;
} }
@ -176,6 +195,7 @@ bool Converter<blink::WebMouseEvent>::FromV8(
return false; return false;
if (!dict.Get("x", &out->x) || !dict.Get("y", &out->y)) if (!dict.Get("x", &out->x) || !dict.Get("y", &out->y))
return false; return false;
dict.Get("button", &out->button);
dict.Get("globalX", &out->globalX); dict.Get("globalX", &out->globalX);
dict.Get("globalY", &out->globalY); dict.Get("globalY", &out->globalY);
dict.Get("movementX", &out->movementX); dict.Get("movementX", &out->movementX);

View file

@ -534,13 +534,13 @@ Sends an input `event` to the page.
For keyboard events, the `event` object also have following properties: For keyboard events, the `event` object also have following properties:
* `keyCode` String (**required**) - A single character that will be sent as * `keyCode` String (**required**) - A single character that will be sent as
keyboard event. Can be any ASCII character on the keyboard, like `a`, `1` keyboard event. Can be any UTF-8 character.
and `=`.
For mouse events, the `event` object also have following properties: For mouse events, the `event` object also have following properties:
* `x` Integer (**required**) * `x` Integer (**required**)
* `y` Integer (**required**) * `y` Integer (**required**)
* `button` String - The button pressed, can be `left`, `middle`, `right`
* `globalX` Integer * `globalX` Integer
* `globalY` Integer * `globalY` Integer
* `movementX` Integer * `movementX` Integer