如何让键盘背光在晚上自动打开,在白天自动关闭?

如何让键盘背光在晚上自动打开,在白天自动关闭?

有没有什么方法(也许使用运行适当命令的 systemd 服务)可以在早上自动关闭键盘背光并在晚上自动打开?

我在 Dell Latitude 5400 中使用 Kubuntu 22.04。

没有光传感器。我想手动设置时间。

另一个选项是从 redshift/KDE 夜间模式获取时间。


为手动模式启用 systemd 计时器后,我看到以下输出。

$ systemctl --user enable --now keyboard-backlight-evening-askubuntu-1503945-1004020.timer
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.

$ systemctl status keyboard-backlight-evening-askubuntu-1503945-1004020.timer
Unit keyboard-backlight-evening-askubuntu-1503945-1004020.timer could not be found.

答案1

新解决方案:KDE 夜间色彩同步

您可以聆听夜色激活的声音,并让背光跟随它。

  1. 设置如下:

    sudo apt install brightnessctl
    sudo usermod -aG input ${USER}
    sudo usermod -aG video ${USER}
    reboot
    
  2. 注意输出brightnessctl --list

  3. 粘贴~/.config/systemd/user/keyboard-backlight-kde-night-color-sync-askubuntu-1503945-1004020.service

    [Unit]
    Description=Sync keyboard backlight to night color
    
    [Service]
    Exec=%h/.local/share/keyboard-backlight-kde-night-color-sync-askubuntu-1503945-1004020.sh
    
    [Install]
    WantedBy=plasma-kwin_wayland.service
    
  4. ~/.local/share/keyboard-backlight-kde-night-color-sync-askubuntu-1503945-1004020.sh

    #!/bin/bash
    dbus-monitor "type='signal',sender='org.kde.NightColor',interface='org.freedesktop.DBus.Properties',path=/ColorCorrect,member=PropertiesChanged" |
      sed -nu '/string "running"/{N;s/.*\(true\|false\).*/\1/;p}' |
      while read -r line; do
        target_brightness=0
        if [ "$line" = 'true' ]; then
          target_brightness=30
        fi
        brightnessctl --device='your device here' set "$target_brightness"
      done
    
  5. chmod +x ~/.local/share/keyboard-backlight-kde-night-color-sync-askubuntu-1503945-1004020.sh

  6. 确保你改变了'your device here'部分

  7. systemctl --user daemon-reload

  8. systemctl --user enable --now keyboard-backlight-kde-night-color-sync-askubuntu-1503945-1004020.service

替代解决方案:手动定时器

您可以设置一个计时器,根据时间自动设置亮度:

  1. 执行与其他解决方案相同的设置步骤

  2. 注意输出brightnessctl --list

  3. 粘贴~/.local/share/keyboard-backlight-sync-askubuntu-1503945-1004020.sh

    #!/bin/bash
    
    # Handle AccuracySec=1
    sleep 2
    
    # https://stackoverflow.com/a/19067658/10477326
    until_sunrise=$((($(date -f - +%s- <<<06:00$' tomorrow\nnow')0)%86400))
    until_sunset=$((($(date -f - +%s- <<<18:00$' tomorrow\nnow')0)%86400))
    
    if [ $until_sunrise -lt $until_sunset ]; then # sunrise is closer 
      echo 'Backlight on at nighttime'
      brightnessctl --device='your device here' set 30
    else # sunset is closer
      echo 'Backlight off at daytime'
      brightnessctl --device='your device here' set 0
    fi
    
  4. 确保你更改了两个'your device here'部分

  5. chmod +x ~/.local/share/keyboard-backlight-sync-askubuntu-1503945-1004020.sh

  6. 粘贴~/.config/systemd/user/keyboard-backlight-sync-askubuntu-1503945-1004020.service

    [Unit]
    Description=Sync keyboard backlight
    
    [Service]
    Type=oneshot
    ExecStart=%t/.local/share/keyboard-backlight-sync-askubuntu-1503945-1004020.sh
    
  7. ~/.config/systemd/user/keyboard-backlight-sync-askubuntu-1503945-1004020.timer

    [Unit]
    Description=Sync keyboard backlight to time
    
    [Timer]
    OnCalendar=6,18:00
    OnStartupSec=1
    
    [Install]
    WantedBy=timers.target
    
  8. systemctl --user daemon-reload

  9. systemctl --user enable --now keyboard-backlight-sync-askubuntu-1503945-1004020.timer

您还可以使用/etc/systemd/system代替品来避免sudo或分组并发症。

相关内容