Skip to content

Commit

Permalink
Fix not responding next/previous when focus in webview
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeena committed Jan 30, 2018
1 parent f025ad4 commit 13c241f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 12 additions & 0 deletions html/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
9 changes: 7 additions & 2 deletions qml/Content.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 13c241f

Please sign in to comment.