Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electronjs detect on window change

I'm trying to get a detect event whenever I switch to another active window. I know there are modules on npm that tells you the active window, but is there an Electron native way to detect a change in the active window(that is not my electron app itself, but some other completely unrelated app.

Thanks!

like image 493
ima_prog Avatar asked May 03 '26 14:05

ima_prog


1 Answers

BrowserWindow has a 'blur' event which is triggered when the window looses focus, and the app has a 'browser-window-blur' event which is called when any of the created windows looses focus.

const {app} = require('electron')

app.on('browser-window-blur', () => {
  // Your code
})
  • Electron API Docs - App (event-browser-window-blur)
  • Electron API Docs - BrowserWindow (event-blur)

If you talk about external Windows you need to go via a native module to solve that, electron and nodejs have no build in function for that. Native modules are extensions to normal JavaScript and they are written in diffrent languages for example C/C++ and exported/compiled to be used with nodeJS.

For talking to the Windows OS API you could use node-winapi if you want to do it yourself. Otherwise i would encourage you to use a library which does this already. Just be sure that it works with the nodeJS version that Electron uses currently nodeJS v8.9.3

like image 166
Hans Koch Avatar answered May 05 '26 04:05

Hans Koch