Change keyboard backlight from cmd line?

Hi, is it possible to change keyb backlight colour from the command line (similar to hyper + trackball)? Any pointers?
thx

Yes, you can send commands to /dev/hidraw0 to control the RGB backlight.

Looks like by default you have to be root to write to /dev/hidraw0 and then this works:

echo -n -e ‘xLRGB\x00\xFF\x00’ > /dev/hidraw0

Where 00 FF 00 is an RGB code (although it appears the order is Blue,Green,Red ?)

(check the end of main.c in the keyboard firmware source for various 4 letter commands you can send to hidraw0)

10 Likes

thx
tried it with sudo, but get permission denied, … hmm

Sudo won’t work because it doesn’t apply to the > output redirection which gets executed in the context of your shell. You can try su -c "..." instead of sudo.

2 Likes
printf 'xLRGB\x00\xFF\x00' | sudo tee /dev/hidraw0

In POSIX sh, echo flags are undefined, so maybe instead of echo, you want to use printf in case you are writing a script that can be used by more people than just you.

7 Likes

Sending this command to /dev/hidraw does not work on my Pocket.

EDIT: solved on my pocket, needed a hard reset (physical power switch).

2 Likes

Is there a way to keep the keyboard backlight color after a restart? Using the printf command I can get the color to change, but it goes back to magenta when I restart the device.

There may be a more elegant method, but I’d just bung the line in a bash script and have it run on boot.

Neat; where would “bash” go in the “printf ‘xLRGB\x##\x##\x##’ | sudo tee /dev/hidraw0” command?

You could add it to your root cron, for example:

sudo crontab -e

then add a line like:

@reboot test -c /dev/hidraw0 && printf "xLRGB..." > /dev/hidraw0

This will execute the command on each boot. First will check that /dev/hidraw0 exists and is a character device.

Thanks for the suggestion. These are separate commands?

Say I want to make the backlight a medium intensity white. Would the commands be:

sudo crontab -e

followed by:

@reboot test -c /dev/hidraw && printf ‘xLRGB\x33\x33\x33’ | sudo tee /dev/hidraw0

Is that correct?

fwiw I got bored of having to use sudo and wanted to do this from a user permissioned script so I added:

KERNEL=="hidraw*", SUBSYSTEM=="hidraw", MODE="0660", GROUP="plugdev"

to allow users in the plugdev group (which my user is part of - I think that’s default but I’m not sure, I don’t remember setting it up) to read and write to devices in the hidraw subsystem and then:

sudo udevadm control --reload-rules
sudo udevadm trigger

to pick up the new configuration.

Bear in mind these are permission changes so please make sure you understand the implications of what you’re doing before you do it!

3 Likes

As someone who is VERY inexperienced with any kind of Linux distro, rest assured that I do NOT understand those implications! :wink:

Is there any benefit of doing it one way versus the other? Are the commands I typed out up there going to do the trick?

Thanks for the suggestions!

I would personally go with the crontab method, as it doesn’t override any default behaviour. The other method would potentially be good to make the call directly on the command line without needing “sudo”, but generally (especially if unsure), it’s better to leave that with system permissions as there’s potential for security issues (albeit unlikely I suspect in this case).

1 Like

Yeah to be fair I was posting that more as an FYI on this thread rather than a direct response, sorry I should have made that more clear.

1 Like

Nope! You want to use the commands that @amospalla suggested!

When you use sudo crontab -e, you’re saying “as the root user” (sudo) I would like to edit the crontab (crontab -e). The crontab is the configuration file for a system process (daemon) called cron which runs tasks at scheduled times.

This will drop you into an editor where you will be able to paste in the line. You might get asked which editor you want to use if this is your first time editing a file as root - choose nano if you don’t have another preference!

The reason that you can just use > /dev/hidraw0 instead of having to | sudo tee /dev/hidraw0 is because this is root’s crontab, so when the tasks run they will run as root with all the permissions that implies. The | sudo tee ... trick is a way to make sure that you can output to a file owned by root when you’re running a shell that’s only owned by the user. If you were to use | sudo tee in the crontab it wouldn’t work because it’s not interactive (i.e. it runs at a scheduled time in the background) so there’s no terminal attached for it to ask for your password!

3 Likes

I got around to playing with it tonight and I was able to edit crontab using nano. I added the line:

@reboot test -c /dev/hidraw0 && printf “xLRGB\x33\x33\x33” > /dev/hidraw0

I left out the “sudo tee” part like you said, but added the specific RGB values. I used the “write out” command, closed nano, and rebooted the device, but nothing changed.

I went back into terminal, entered “sudo crontab -e,” and the cron file opened up with my command still there with the original text. It seems to have saved the command into the file, at least.

As I understand it, I am using nano to edit a file containing a command that will run every time when booting to change a setting (in this case the keyboard backlight color) so I don’t have to change the setting myself every time I use the device. Am I understanding the process, at least?

Now that I have the command added to the file, how do I get it to run? This is certainly a learning experience; I’m not used to changing settings this way.

As an experiment, I entered the command into “execute” and got an error message that “@reboot” wasn’t a valid command–I’m glad that didn’t mess anything up!

Test this, open a terminal, and set yourself root:

sudo su

Then execute what you are executing within cron but manually:

test -c /dev/hidraw0 && printf “xLRGB\x33\x33\x33” > /dev/hidraw0

What happens?

I can’t try that specific command right now, since I’ve been running into some hangups on booting the device, but I was able to change the backlight color from the terminal using the printf command Josch posted earlier in the thread.

Try with this root crontab entry instead:

@reboot sleep 30 && test -c /dev/hidraw0 && printf “xLRGB\x33\x33\x33” > /dev/hidraw0

It waits some seconds before attempting to set the colour.