Add before-input-event event for webContents (fixes #7586)
Embedding arbitrary web content is problematic when it comes to keyboard shortcuts because: * Web content can steal app shortcuts (see e.g. brave/browser-laptop#4408) * Blocked web content (e.g. a focused <webview> performing expensive computation) will also prevent app shortcuts from firing immediately The new before-input-event event can be used to overcome these issues by always handle certain keyboard events in the main process. Note that this requires electron/brightray#261 to compile.
This commit is contained in:
parent
3290c6b335
commit
a3b65ad481
4 changed files with 170 additions and 0 deletions
|
@ -70,6 +70,7 @@
|
|||
#include "third_party/WebKit/public/web/WebFindOptions.h"
|
||||
#include "third_party/WebKit/public/web/WebInputEvent.h"
|
||||
#include "ui/display/screen.h"
|
||||
#include "ui/events/keycodes/dom/keycode_converter.h"
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
#include "ui/aura/window.h"
|
||||
|
@ -488,6 +489,34 @@ void WebContents::HandleKeyboardEvent(
|
|||
}
|
||||
}
|
||||
|
||||
bool WebContents::PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
const content::NativeWebKeyboardEvent& event,
|
||||
bool* is_keyboard_shortcut) {
|
||||
const char* type =
|
||||
event.type == blink::WebInputEvent::Type::RawKeyDown ? "keyDown" :
|
||||
event.type == blink::WebInputEvent::Type::KeyUp ? "keyUp" :
|
||||
nullptr;
|
||||
if (!type) {
|
||||
// This should never happen.
|
||||
assert(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate());
|
||||
dict.Set("type", type);
|
||||
dict.Set("key", ui::KeycodeConverter::DomKeyToKeyString(event.domKey));
|
||||
|
||||
using Modifiers = blink::WebInputEvent::Modifiers;
|
||||
dict.Set("isAutoRepeat", (event.modifiers & Modifiers::IsAutoRepeat) != 0);
|
||||
dict.Set("shift", (event.modifiers & Modifiers::ShiftKey) != 0);
|
||||
dict.Set("control", (event.modifiers & Modifiers::ControlKey) != 0);
|
||||
dict.Set("alt", (event.modifiers & Modifiers::AltKey) != 0);
|
||||
dict.Set("meta", (event.modifiers & Modifiers::MetaKey) != 0);
|
||||
|
||||
return Emit("before-input-event", dict);
|
||||
}
|
||||
|
||||
void WebContents::EnterFullscreenModeForTab(content::WebContents* source,
|
||||
const GURL& origin) {
|
||||
auto permission_helper =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue