Results 1 to 7 of 7

Thread: How do I change the screen brightness with a keyboard shortcut?

  1. #1
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Question How do I change the screen brightness with a keyboard shortcut?

    I have been searching how to change my screen's brightness with a keyboard shortcut, because my keyboard doesn't have a physical key.

    A tool such as xrandr isn't suitable, because it messes with my Night Light setting.

    From the terminal or from Alt+F2, these two commands correctly raise or lower the screen's brightness respectively:
    Code:
    xdotool key XF86MonBrightnessUp
    xdotool key XF86MonBrightnessDown
    However, if I add these to my custom keyboard shortcuts, they don't work.

    I created a basic script to do the same thing, and I discovered something odd. This script works when run from the terminal or Alt+F2, but not from a keyboard shortcut:
    Code:
    #!/usr/bin/env bash
    xdotool key XF86MonBrightnessUp
    However, this does work from a keyboard shortcut:
    Code:
    #!/usr/bin/env bash
    zenity --info --text='Brightness up'
    xdotool key XF86MonBrightnessUp
    Of course, there's the inconvenience of pressing Enter to close Zenity's display before it works.

    Putting Zenity into the background prevents it from working:
    Code:
    #!/usr/bin/env bash
    zenity --info --text='Brightness up' &
    xdotool key XF86MonBrightnessUp
    Obviously, running Zenity to put something on the screen somehow "activates" the screen from Bash's point of view.

    How can I achieve what I'm after without having to display something on the screen?
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  2. #2
    Join Date
    Oct 2004
    Location
    33.4N -112.1W
    Beans
    2,497
    Distro
    Ubuntu Budgie

    Re: How do I change the screen brightness with a keyboard shortcut?

    Can you use xbacklight with your GFX to control the brightness?

    Brightness Down (example):
    $ xbacklight -1

    Brightness Up (example):
    $ xbacklight +5

    If it works test it by adding it:
    Settings → Keyboard → Application Shortcuts

    Reboot may be required.
    /path/to/Truth

  3. #3
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: How do I change the screen brightness with a keyboard shortcut?

    Thank you for the reply, Xian.

    Unfortunately, xbacklight does nothing whatsoever to my screen display, even after rebooting. There's no error message; it just does nothing. I've also tried using -display :0.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  4. #4
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: How do I change the screen brightness with a keyboard shortcut?

    I've discovered the solution, with a bit of intuition and some trial-and-error.

    The keys XF86MonBrightnessDown and XF86MonBrightnessUp are assigned in dconf respectively to
    /org/gnome/settings-daemon/plugins/media-keys/screen-brightness-down-static
    /org/gnome/settings-daemon/plugins/media-keys/screen-brightness-up-static


    Changing the key assignments to something that I could use (I chose <Super>F11 and <Super>F12) didn't work.

    However, I found that using those assignments on a different key worked:
    /org/gnome/settings-daemon/plugins/media-keys/screen-brightness-down
    /org/gnome/settings-daemon/plugins/media-keys/screen-brightness-up


    Problem solved!
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  5. #5
    Join Date
    Aug 2011
    Location
    52.5° N 6.4° E
    Beans
    6,824
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: How do I change the screen brightness with a keyboard shortcut?

    Alternatively, the brightness is available in the /sys pseudofilesystem. You can read the current brightness, the max brightness and set a new brightness.
    Code:
    $ cd /sys/class/backlight/acpi_video0
    $ ls
    actual_brightness  bl_power  brightness  device  max_brightness  power  scale  subsystem  type  uevent
    $ cat actual_brightness
    16
    $ cat max_brightness
    20
    $ echo 4 | sudo tee brightness
    ## This make my screen very dim
    $ cat actual_brightness
    4
    This tends to be somewhat hardware dependent. Just have a look around there.

  6. #6
    Join Date
    Mar 2022
    Beans
    1

    Re: How do I change the screen brightness with a keyboard shortcut?

    Your problem with xdotool is that your keyboard shortcut and the xdotool key overlap. You need to clear the key with:

    xdotool key --clearmodifiers XF86MonBrightnessDown

  7. #7
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: How do I change the screen brightness with a keyboard shortcut?

    Quote Originally Posted by davelacorneille View Post
    Your problem with xdotool is that your keyboard shortcut and the xdotool key overlap. You need to clear the key with:

    xdotool key --clearmodifiers XF86MonBrightnessDown
    Hah! I wish that I'd thought of that!

    Thank you — this will be useful in future.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •