I'm trying to disable Cmd+R or F5 for refreshing my electron app like so:
globalShortcut.register('CommandOrControl+R', () => false);
globalShortcut.register('F5', () => false);
But, unfortunately, it cause preventing refresh entirely for all frames, even for other browsers.
How can I register such shortcuts only for my created window?
ALTERNATIVE: I guess, we could use Mousetrap as an option for such operation, but I wonder - is there any kinda built in method for such operation?
I'm trying to disable Cmd+R or F5 for refreshing my electron app like so:
globalShortcut.register('CommandOrControl+R', () => false);
globalShortcut.register('F5', () => false);
But, unfortunately, it cause preventing refresh entirely for all frames, even for other browsers.
How can I register such shortcuts only for my created window?
ALTERNATIVE: I guess, we could use Mousetrap as an option for such operation, but I wonder - is there any kinda built in method for such operation?
Share Improve this question edited Jul 5, 2018 at 9:25 WebArtisan asked Jul 5, 2018 at 9:17 WebArtisanWebArtisan 4,23612 gold badges47 silver badges66 bronze badges 3- 2 have u seen this? electronjs/docs/tutorial/… (mousetrap is suggested there too) – pergy Commented Jul 5, 2018 at 10:36
- But mouse trap doesn't work inside a input fields isn't ? – Shahabaz Commented Jan 19, 2020 at 7:28
- There seems to be a built-in functionality of CMD+R in electron, as it's impossible to catch this event in the browser window and preventing it from propagation. The event is caught, but the page reloads even with stopPropagation(), preventDefault() or return false... Any ideas anyone? – Radko Commented Apr 1, 2020 at 20:53
2 Answers
Reset to default 3This is the most appropriate way to prevent Window refresh. The other approaches don't prevent window.reload()
calls.
The ev
object hold information about what triggered the unload event, it could be used to tailor the oute of the event in any way you wish.
window.addEventListener('beforeunload', (ev) => {
// Setting any value other than undefined here will prevent the window
// from closing or reloading
ev.returnValue = true;
});
I use this code and work only with keyboard
win.webContents.on('before-input-event', (event, input) => {
if (input.control && input.key.toLowerCase() === 'r') {
event.preventDefault()
}
})