From 868c44b23949d7d1f677e8e7d6e4832ca2f0510e Mon Sep 17 00:00:00 2001 From: Leon Krause Date: Wed, 9 May 2018 15:57:10 +0200 Subject: [PATCH] Fix keyboard focus lock-out with HTML5 canvas in iframe (cherry picked from commit 9080e96bc81328e658a03e2cd248c06ddbf9f2ec) --- misc/dist/html/default.html | 2 +- platform/javascript/engine.js | 4 ++++ platform/javascript/os_javascript.cpp | 10 +++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/misc/dist/html/default.html b/misc/dist/html/default.html index a1a4e89d020..3ad29d149cb 100644 --- a/misc/dist/html/default.html +++ b/misc/dist/html/default.html @@ -195,7 +195,7 @@ $GODOT_HEAD_INCLUDE
- + HTML5 canvas appears to be unsupported in the current browser.
Please try updating or use a different browser.
diff --git a/platform/javascript/engine.js b/platform/javascript/engine.js index 06ff96bc307..329a4fc1894 100644 --- a/platform/javascript/engine.js +++ b/platform/javascript/engine.js @@ -173,6 +173,10 @@ actualCanvas.style.padding = 0; actualCanvas.style.borderWidth = 0; actualCanvas.style.borderStyle = 'none'; + // disable right-click context menu + actualCanvas.addEventListener('contextmenu', function(ev) { + ev.preventDefault(); + }, false); // until context restoration is implemented actualCanvas.addEventListener('webglcontextlost', function(ev) { alert("WebGL context lost, please reload the page"); diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 017922ba1d7..e878cf3725c 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -159,10 +159,9 @@ static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent int mask = _input->get_mouse_button_mask(); int button_flag = 1 << (ev->get_button_index() - 1); if (ev->is_pressed()) { - // since the event is consumed, focus manually - if (!is_canvas_focused()) { - focus_canvas(); - } + // Since the event is consumed, focus manually. The containing iframe, + // if used, may not have focus yet, so focus even if already focused. + focus_canvas(); mask |= button_flag; } else if (mask & button_flag) { mask &= ~button_flag; @@ -173,7 +172,8 @@ static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent ev->set_button_mask(mask); _input->parse_input_event(ev); - // prevent selection dragging + // Prevent multi-click text selection and wheel-click scrolling anchor. + // Context menu is prevented through contextmenu event. return true; }