How to prevent F keys and other browser shortcuts?
See original GitHub issueSpecification
- pywebview version: 3.6.3
- operating system: Windows 10
- web renderer: Edge
Description
Hi everyone, I am building basic python desktop applications, I use various servers, flask, pywebio, etc. to build my GUI. I want my apps to provide the full desktop experience. On the pywebview window, when I click on F3 or use a shortcut like CTRL+F, edge reveals itself with a search bar. I don’t want it to reveal that it runs in a browser, so I used JavaScript code below but there is a problem, when my page loads slowly and if my server crashes, this JavaScript code won’t work and all the shortcuts and F keys will be active again. Is there another way to prevent browser shortcuts? This method that i use is really sketchy and doesn’t feel good.
let FKeys = [
112, // F1
113,
114, // F3
115,
116, // F5 - Refresh
117,
118,
119,
120,
121, // F10 - DevTools
122, // F11 - Fullscreen
123, // F12 - Dev Tools
];
let ForbiddenShortcutLetters = [
"c", // Copy
"v", // Paste
"x", // Cut
"s", // Save
"p", // Print
"w", // Close
"u", // View Source
"a", // Select All
"z", // Undo
];
let ForbiddenShotcutKeyCodes = [
73, // I
69, // E
74, // J
70, // F
];
document.addEventListener("contextmenu", function (event) {
event.preventDefault();
});
document.onkeydown = function (event) {
console.log(event);
if (FKeys.includes(event.keyCode)) {
console.log("F key pressed");
event.preventDefault();
event.stopPropagation();
return false;
} else if (
(event.ctrlKey || event.shiftKey || event.altKey) &&
ForbiddenShortcutLetters.indexOf(event.key) !== -1
) {
event.preventDefault();
event.stopPropagation();
return false;
} else if (
(event.ctrlKey || event.shiftKey || event.altKey) &&
ForbiddenShotcutKeyCodes.includes(event.keyCode)
) {
event.preventDefault();
event.stopPropagation();
return false;
} else if (event.ctrlKey) {
// Disable Ctrl
event.preventDefault();
event.stopPropagation();
return false;
} else if (event.shiftKey) {
// Disable Shift
event.preventDefault();
event.stopPropagation();
return false;
} else if (event.altKey) {
// Disable Alt
event.preventDefault();
event.stopPropagation();
return false;
}
};
Practicalities
-
YES I am willing to work on this issue myself.
-
NO I am prepared to support this issue financially.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Yes, this issue must be definitely addressed. Here is a discussion on the matter https://github.com/MicrosoftEdge/WebView2Feedback/issues/288
@r0x0r You are the best, thanks a lot.