draft0 - a shared blog by just some people

Menu

Setting Screen Brightness To Any Value With A Three-Step Keyboard Shortcut (Linux)

Initially out of necessity because the brightness keys of my new laptop didn't work out of the box (the driver was aded to my distro not a month later, which should have been acceptable, but I didn't know that at first), I was looking for a way to set the backlight brighness of my laptop's internal screen easily, without typing a command in a shell. What I ended up using I like even better than the usual + and - keys.

I'm using i3wm and dmenu. The way I set screen brightness is

  • 1) I enter the shortcut (mod+B in my case)
  • 2) I enter a number and
  • 3) I hit Enter.
  • It's simple to implement. Just put this line in the i3 config file:

    
    bindsym $mod+b exec \
      thatbright=$(echo "1000\n2000\n3000\n4000\n5000\n6000\n7000\n100\n10" \
      | dmenu -p "How bright though?") && echo $thatbright \
      > /sys/class/backlight/*/brightness
    

    You can put it into one line (without the \s inbetween) if you want.

    You could easily change that to a two-step or single shortcut if you like. I like the three-step version because it allows me to choose from one of seven brightness modes easily but also lets me enter a value below or between those pre-sets without taking up more than one key.

    Code Explanation

    First, the variable thatbright is set to the number that dmenu outputs, which can be one of the numbers that are echoed to the pipe (selected with arrow keys or completed when typed in dmenu) or another number that is entered into dmenu. If that was suggessful, the value is written to /sys/class/backlight/*/brightness. If you have multiple backlights in /sys/class and you only want to set one or if your shell doesn't support wild cards in paths, you can change the * to whatever applies to your system, e.g. intel_backlight.

    Scripts/Commands to set the screen brightness

    The simplest script that sets the screen brightness in Linux is probably the one-liner from above: echo $1 > /sys/class/backlight/*/brightness. This sets the raw numerical value. You need to know what a sensible range of numbers is and what the maximal accepted value is (look at '/sys/class/backlight/[YOUR_BACKLIGHT]/max_brightness'). But there are more elaborate scripts, like bbacklight by Giuseppe Eletto with which you set the brightness with a percentage value.

    Comment via email
    Mastodon