【问题标题】:Is there any way to get system idle time on all platforms有没有办法在所有平台上获得系统空闲时间
【发布时间】:2019-06-10 05:21:08
【问题描述】:

我正在制作一个电子应用程序来检查系统空闲时间。我想在winodws、mac和linux上使用一个通用模块来获取空闲时间。

我正在使用此命令在 mac 上获取空闲时间,但它在 linux 中不起作用。 我还尝试了不同的 npm 插件来获得空闲时间,但它们都没有按预期工作

function idleTime(callback) {
  var command = `ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'`;
  child_process.exec(command, function (err, stdout, stderr) {
    return err ? callback(err) : callback(null, stdout.trim());
  });

我的预期结果是检查用户是否正在使用系统。我想计算系统在所有平台(mac、linux、windows)上的空闲时间。

【问题讨论】:

标签: node.js electron


【解决方案1】:

https://electronjs.org/docs/api/power-monitor#powermonitorquerysystemidlestateidlethreshold-callback

powerMonitor.querySystemIdleTime(callback)
callback Function

idleTime Integer - Idle time in seconds
Calculate system idle time in seconds.

Electron 内置支持检查空闲时间(和状态)。

【讨论】:

  • 我正在尝试这个,但它给出了“无法读取未定义的属性'querySystemIdleTime'”这个错误。你能解释一下我是电子新手吗
  • 你可以看看提供的link开头的代码示例,了解如何获取powerMonitor变量,并遵守必须从主进程,在app模块的ready事件发出后。
【解决方案2】:

为时已晚,但对于新手来说看看Electron Power monitor API's

const {powerMonitor} = require('electron'); 
const idle = powerMonitor.getSystemIdleTime() // it returns in seconds when I am writing this
console.log('Current System Idle Time - ', idle); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多