electron: Windows 10 / Event: 'app-command' not fired after `loadURL`

  • Electron Version (output of node_modules/.bin/electron --version):
    • 4.0.5
  • Operating System (Platform and Version):
    • Windows 10 (1803)
  • Last known working Electron version (if applicable):
    • 3.1.4

Expected Behavior

Event: ‘app-command’ fired when back / forward button is pushed after loadURL ( use five button mouse, cmd -> browser-backward, browser-forward )

https://electronjs.org/docs/api/browser-window#event-app-command-windows

Actual behavior

Event: ‘app-command’ not fired after loadURL

To Reproduce

const electron = require('electron')

const { app, BrowserWindow } = electron

async function createWindow () {
  let mainWindow = new BrowserWindow()

  mainWindow.on('app-command', function (e, cmd) {
    console.log(cmd) // after 5sec, v4 not fired, v3 fired.
  })

  mainWindow.on('closed', function () {
    mainWindow = null
  })

  console.log('not loaded yet.')
  await (ms => new Promise(resolve => setTimeout(resolve, ms)))(5000)

  mainWindow.loadURL('https://electronjs.org/')
  console.log('loaded!')
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

Screenshots

nothing

Additional Information

nothing

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Guys just use the following to avoid electron here

window.addEventListener('mousedown', event => {
  switch (event.button) {
    case 4: history.back(); break;
    case 5: history.forward(); break;
  }
})

VSCode is seeing the same regression. Can this get some attention?

Still a thing in v11.0.3 unfortunately. Here is a dirty JS hack to disable the mouse buttons from navigating (does not work cross browser):

window.addEventListener('mouseup', function(e){
if (e.button === 3 || e.button === 4) e.preventDefault()
})

@sandeep1995 btw with my mouse on Windows and Linux the event.button is 3 and 4, not 4 and 5?

I’ve found that it works even after loadURL/loadFile but you have to click ‘back/forward’ mouse button exactly on ‘drag/title bar’ of window. When you click on body, it does not work. Does it mean it is a problem with chromium itself?

I can confirm that the event is fired when invoking it on the native title!