From 13c241f3b96de64157c5eadafcac933dd120f072 Mon Sep 17 00:00:00 2001 From: Jeena Date: Tue, 30 Jan 2018 22:55:16 +0100 Subject: [PATCH] Fix not responding next/previous when focus in webview For some reason in the latest Qt versions the webview took over the focus from the keyboard, once clicked on the webview the arrow keys wouldn't register up and thus you couldn't navigate with them anymore. This patch fixes this problem by using window.location.href and checkinf for those special urls. This is way easier to use than WebChannels. --- html/content.js | 12 ++++++++++++ qml/Content.qml | 9 +++++++-- qml/main.qml | 8 ++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/html/content.js b/html/content.js index cbc29b2..2d88c4e 100644 --- a/html/content.js +++ b/html/content.js @@ -71,3 +71,15 @@ function setNightmode(nightmode) { if(nightmode) document.body.className = "nightmode"; else document.body.className = ""; } + +document.onkeydown = checkKey; + +function checkKey(e) { + e = e || window.event; + if (e.keyCode == '37') { + window.location.href = "feedthemonkey:previous"; + } + else if (e.keyCode == '39') { + window.location.href = "feedthemonkey:next"; + } +} diff --git a/qml/Content.qml b/qml/Content.qml index b00764a..086dbef 100644 --- a/qml/Content.qml +++ b/qml/Content.qml @@ -85,9 +85,14 @@ Item { webView.runJavaScript("if(typeof setNightmode == \"function\") setNightmode(" + (content.nightmode ? "true" : "false") + ")") } - onNavigationRequested: { - if (request.navigationType != WebEngineView.LinkClickedNavigation) { + if (request.url == "feedthemonkey:previous") { + request.action = WebEngineView.IgnoreRequest; + app.showPreviousPost(); + } else if (request.url == "feedthemonkey:next") { + request.action = WebEngineView.IgnoreRequest; + app.showNextPost(); + } else if (request.navigationType != WebEngineView.LinkClickedNavigation) { request.action = WebEngineView.AcceptRequest; } else { request.action = WebEngineView.IgnoreRequest; diff --git a/qml/main.qml b/qml/main.qml index e08f3d9..7746367 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -114,6 +114,14 @@ ApplicationWindow { return forEscapingHTML.getText(0, forEscapingHTML.length) } + function showNextPost() { + sidebar.next() + } + + function showPreviousPost() { + sidebar.previous() + } + function keyPressed(event) { switch (event.key) { case Qt.Key_Right: