Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event when machine has been locked/unlocked (Electron Windows)

So i have some tasks that i need to be notified when the user locks their machine. This is in order to pause those tasks. Equally i need to know when the machine has been successfully unlocked in order to resume said tasks. This is purely for a Windows only function so doesn't need to be cross-platform.

Ive looked through the Electron docs and found this about the powerMonitor API's however it only has events for suspend and resume. I assume they refer to the machine going to sleep as locking and un-locking does not trigger them on my machine (Win 10).

I know very little of how core Windows works, with its messages/event system. However i have found this which lists information about the WM_WTSSESSION_CHANGE which seems to be what i need. Notably the WTS_SESSION_LOCK (0x7) and WTS_SESSION_UNLOCK (0x8) status codes.

However i can't find a way either with electron or NodeJS directly to listen to Windows messages.

Any advice on the problem would be greatly appreciated. Thanks

like image 718
ste2425 Avatar asked Sep 07 '25 03:09

ste2425


1 Answers

Just in case someone ends up on this old question, powerMonitor now supports two events for this: lock-screen and unlock-screen, so you can just (in the main process):

const {powerMonitor} = require("electron");

powerMonitor.addListener('lock-screen', () => {
  // Screen is locked, do something
});

powerMonitor.addListener('unlock-screen', () => {
  // Screen is unlocked, do something else
});



like image 67
Simon Avatar answered Sep 10 '25 00:09

Simon