electron: focus() doesn't focus web view for window at start

This problem is similar to issue#382, but it doesn’t solve the problem.

Environment

OSX: 10.0.9 Electron: 0.26.0

When I first start my app, focus() and focusOnWebView() doesn’t really focus on the web view of the window. And keyboard press event listening doesn’t work until I click the web page. But If I then switch to other app and switch back, it works.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 21 (6 by maintainers)

Most upvoted comments

Oh snap. I just discovered a fix.

const webview = document.getElementById('my-webview')

webview.addEventListener('dom-ready', () => {
  webview.focus()
})

This worked for me

const webview = document.getElementById('my-webview')

webview.addEventListener('dom-ready', () => {
  this.focus();
  window.blur();
  window.focus();
});