admin管理员组文章数量:1415673
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()
}
})
本文标签: javascriptElectronjs Prevent refresh for created windowStack Overflow
版权声明:本文标题:javascript - Electron.js Prevent refresh for created window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745219805a2648340.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论