I3

在 i3 中設置綁定以控制鍵盤背光

  • September 2, 2018

對於我的i3 視窗管理器設置,我正在尋找一個類似於 的命令行工具,xbacklight但用於控制鍵盤中 LED 的亮度。

基本上,我可以通過命令行設置 LED,但它需要是 root:

# Light off the leds
echo 0 > /sys/class/leds/smc::kbd_backlight/brightness
# Light on the leds (full power)
echo 100 > /sys/class/leds/smc::kbd_backlight/brightness

我知道這是可能的,因為 Gnome3 對此有支持,但我不知道它們是如何進行的……

現在,我的~/.config/i3/config樣子是這樣的:

# screen brightness controls
bindsym XF86MonBrightnessUp exec xbacklight -inc 10
bindsym XF86MonBrightnessDown exec xbacklight -dec 10

# keyboard backlight controls
#TODO
# XF86KbdBrightnessUp
# XF86KbdBrightnessDown

那麼,有沒有一種工具,類似於xbacklight用鍵盤背光來實現螢幕亮度?如果這個工具可以同時控制(螢幕和鍵盤),那就更好了。

Giorgos Keramidas編寫了一個依賴於 dbus 和 UPower 的腳本 - 大多數桌面發行版都已啟動並執行。

我建議使用它而不是寫入的腳本,/sys/class/...因為它們需要以 root 身份執行 - 通過 sudo 或通過設置 SUID - 這是不好的安全做法。

基本用法很簡單

kbdbacklight up
kbdbacklight down
kbdbacklight [ 0 | 1 | 2 | 3 ]

將此添加到 i3 配置中

# increase/decrease keyboard brightness
bindsym XF86KbdBrightnessUp exec kbdbacklight up
bindsym XF86KbdBrightnessDown exec kbdbacklight down 

腳本本身非常簡單

#!/bin/sh
# By Giorgos Keramidas

# backlight_get
#       Print current keyboard brightness from UPower to stdout.
backlight_get()
{
   dbus-send --type=method_call --print-reply=literal --system         \
       --dest='org.freedesktop.UPower'                                 \
       '/org/freedesktop/UPower/KbdBacklight'                          \
       'org.freedesktop.UPower.KbdBacklight.GetBrightness'             \
       | awk '{print $2}'
}

# backlight_get_max
#       Print the maximum keyboard brightness from UPower to stdout.
backlight_get_max()
{
   dbus-send --type=method_call --print-reply=literal --system       \
       --dest='org.freedesktop.UPower'                               \
       '/org/freedesktop/UPower/KbdBacklight'                        \
       'org.freedesktop.UPower.KbdBacklight.GetMaxBrightness'        \
       | awk '{print $2}'
}

# backlight_set NUMBER
#       Set the current backlight brighness to NUMBER, through UPower
backlight_set()
{
   value="$1"
   if test -z "${value}" ; then
       echo "Invalid backlight value ${value}"
   fi

   dbus-send --type=method_call --print-reply=literal --system       \
       --dest='org.freedesktop.UPower'                               \
       '/org/freedesktop/UPower/KbdBacklight'                        \
       'org.freedesktop.UPower.KbdBacklight.SetBrightness'           \
       "int32:${value}}"
}

# backlight_change [ UP | DOWN | NUMBER ]
#       Change the current backlight value upwards or downwards, or
#       set it to a specific numeric value.
backlight_change()
{
   change="$1"
   if test -z "${change}" ; then
       echo "Invalid backlight change ${change}."                    \
           "Should be 'up' or 'down'." >&2
       return 1
   fi

   case ${change} in
   [1234567890]|[[1234567890][[1234567890])
       current=$( backlight_get )
       max=$( backlight_get_max )
       value=$( expr ${change} + 0 )
       if test ${value} -lt 0 || test ${value} -gt ${max} ; then
           echo "Invalid backlight value ${value}."                  \
               "Should be a number between 0 .. ${max}" >&2
           return 1
       else
           backlight_set "${value}"
           notify-send -t 800 "Keyboard brightness set to ${value}"
       fi
       ;;

   [uU][pP])
       current=$( backlight_get )
       max=$( backlight_get_max )
       if test "${current}" -lt "${max}" ; then
           value=$(( ${current} + 1 ))
           backlight_set "${value}"
           notify-send -t 800 "Keyboard brightness set to ${value}"
       fi
       ;;

   [dD][oO][wW][nN])
       current=$( backlight_get )
       if test "${current}" -gt 0 ; then
           value=$(( ${current}  - 1 ))
           backlight_set "${value}"
           notify-send -t 800 "Keyboard brightness set to ${value}"
       fi
       ;;

   *)
       echo "Invalid backlight change ${change}." >&2
       echo "Should be 'up' or 'down' or a number between"           \
           "1 .. $( backlight_get_max )" >&2
       return 1
       ;;
   esac
}

if test $# -eq 0 ; then
   current_brightness=$( backlight_get )
   notify-send -t 800 "Keyboard brightness is ${current_brightness}"
else
   # Handle multiple backlight changes, e.g.:
   #   backlight.sh up up down down up
   for change in "$@" ; do
       backlight_change "${change}"
   done
fi

只需保存在您的某個地方PATH並享受。我使用了這個名稱kbdbacklight,並在上面的範例和 i3 配置中使用了它,如果名稱不同,請注意更新它們。

請注意,Giorgio 的腳本也使用 . notify-send,因此請確保您安裝了正確的軟體包(libnotify-bin在 Debian 和 Ubuntu 上)或從腳本中註釋/刪除這些行。

使用light

bindsym XF86KbdBrightnessUp     exec light -k -A 10
bindsym XF86KbdBrightnessDown   exec light -k -U 10

目前它在 Arch 和 Fedora 的打包版本中可用。但是你總是可以輕鬆地從原始碼建構。在此處找到 git 儲存庫:https ://github.com/haikarainen/light 。

引用自:https://unix.stackexchange.com/questions/260409