You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ubuntu 22.04(Wayland)的Electron应用中执行xset命令关闭显示器?

Wayland下Electron应用实现关闭显示器的方案

xset -dpms force off无法在Wayland环境下生效,因为xset是X11窗口系统专属工具,Wayland与X11底层机制不兼容,直接执行该命令不会产生任何效果。

以下是Wayland环境下可行的替代方案:

方案1:使用wl-gammactl模拟关闭显示器

这是通用的Wayland工具,通过调节屏幕gamma值实现黑屏效果,等效于关闭显示器:

  1. 先安装工具:
    sudo apt install wl-gammactl
    
  2. 在Electron中调用命令控制显示器:
    const { exec } = require('child_process');
    
    // 关闭显示器
    exec('wl-gammactl set --all 0 0 0', (err) => {
      if (err) console.error('关闭失败:', err);
    });
    
    // 恢复显示器
    exec('wl-gammactl set --all 1 1 1', (err) => {
      if (err) console.error('恢复失败:', err);
    });
    

方案2:调用systemd-logind或 compositor 原生接口

针对不同Wayland桌面环境,可使用系统原生接口触发关闭逻辑:

  • 通用systemd-logind方式(需系统已开启空闲关屏设置,触发系统空闲状态实现关屏):
    busctl call org.freedesktop.login1 /org/freedesktop/login1/session/self org.freedesktop.login1.Session SetIdleHint b true
    
  • GNOME Shell专属方式:
    关闭显示器:
    gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval 'global.screen.get_display().set_power_save_mode(true);'
    
    恢复显示器:
    gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval 'global.screen.get_display().set_power_save_mode(false);'
    

注意事项

  • 不同Wayland compositor(如GNOME、KDE、Sway)的命令存在差异,需根据目标环境适配。
  • Electron应用执行系统命令时,需确保用户已安装对应工具,可在应用首次运行时检测并提示安装。

内容的提问来源于stack exchange,提问作者selenagomez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 03:47:39