electron - Electronjs如何检测监视器/屏幕是否关闭
问题描述
我们如何检测监视器是否关闭。Electron 提供了electron.screen API,我们可以在其中检索所有显示器electron.screen.getAllDisplays()
,但此处提供的信息不包含有关显示器电源状态的信息:
[{
id: 2528732444,
bounds: { x: 0, y: 0, width: 1920, height: 1080 },
workArea: { x: 0, y: 0, width: 1920, height: 1040 },
accelerometerSupport: 'unknown',
monochrome: false,
colorDepth: 24,
colorSpace: '{primaries:BT709, transfer:IEC61966_2_1, matrix:RGB, range:FULL}',
depthPerComponent: 8,
size: { width: 1920, height: 1080 },
workAreaSize: { width: 1920, height: 1040 },
scaleFactor: 1,
rotation: 0,
internal: false,
touchSupport: 'unknown'
},
...
]
如果显示器未插入 PC,则不会列出它。但是,如果显示器已插入 PC 并关闭,它会将其列出,electron.screen.getAllDisplays()
但不提供其状态。
解决方案
这是不可能的,即使显示器关闭(这就是它显示在 getAllDisplays() 中的原因),DVI 或 HDMI 之类的东西仍会报告屏幕已连接。
我能想到的唯一选择是运行一个访问 Win32 API 的 shell 脚本,但这将不再基于 Electron。
您将使用Windows 的电源设置 GUID。
GUID_CONSOLE_DISPLAY_STATE
6FE69556-704A-47A0-8F24-C28D936FDA47
The current monitor's display state has changed.
Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This notification is available starting with Windows 8 and Windows Server 2012.
The Data member is a DWORD with one of the following values.
0x0 - The display is off.
0x1 - The display is on.
0x2 - The display is dimmed.
推荐阅读
- python - 获取时间序列自相关函数的原始数据 (plot_acf)
- javascript - 赛普拉斯:如何从字符串复制/粘贴单个文本词以稍后粘贴
- splunk - Splunk:从搜索中返回 One 或 True,在另一个搜索中使用该结果
- java - 场景的类设计:继承与枚举
- simulink - Simulink:在停止时引用掩码块
- opengl - 大纲(OpenGL 2.0)
- javascript - 使用 react 根据后端响应更新复选框值
- c++ - 病态模板偏特化
- git - 错误:pathspec 'tut51.cpp' 与 git 已知的任何文件都不匹配
- c# - 加载卫星程序集时的并发问题?