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?
This 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 outcome 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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With