最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron.js Prevent refresh for created window - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

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 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()
    }
})
发布评论

评论列表(0)

  1. 暂无评论