electron/docs/api/power-monitor.md
2018-09-14 14:57:01 +10:00

78 lines
1.9 KiB
Markdown

# powerMonitor
> Monitor power state changes.
Process: [Main](../glossary.md#main-process)
You cannot require or use this module until the `ready` event of the `app`
module is emitted.
For example:
```javascript
const electron = require('electron')
const { app } = electron
app.on('ready', () => {
electron.powerMonitor.on('suspend', () => {
console.log('The system is going to sleep')
})
})
```
## Events
The `powerMonitor` module emits the following events:
### Event: 'suspend'
Emitted when the system is suspending.
### Event: 'resume'
Emitted when system is resuming.
### Event: 'on-ac' _Windows_
Emitted when the system changes to AC power.
### Event: 'on-battery' _Windows_
Emitted when system changes to battery power.
### Event: 'shutdown' _Linux_ _macOS_
Emitted when the system is about to reboot or shut down. If the event handler
invokes `e.preventDefault()`, Electron will attempt to delay system shutdown in
order for the app to exit cleanly. If `e.preventDefault()` is called, the app
should exit as soon as possible by calling something like `app.quit()`.
### Event: 'lock-screen' _macOS_ _Windows_
Emitted when the system is about to lock the screen.
### Event: 'unlock-screen' _macOS_ _Windows_
Emitted as soon as the systems screen is unlocked.
## Methods
The `powerMonitor` module has the following methods:
#### `powerMonitor.querySystemIdleState(idleThreshold, callback)`
* `idleThreshold` Integer
* `callback` Function
* `idleState` String - Can be `active`, `idle`, `locked` or `unknown`
Calculate the system idle state. `idleThreshold` is the amount of time (in seconds)
before considered idle. `callback` will be called synchronously on some systems
and with an `idleState` argument that describes the system's state. `locked` is
available on supported systems only.
#### `powerMonitor.querySystemIdleTime(callback)`
* `callback` Function
* `idleTime` Integer - Idle time in seconds
Calculate system idle time in seconds.