[SOLVED] Scroll Lock in Cooler master storm keyboard binding

Forum rules
Before you post please read how to get help
Post Reply
Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

[SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Tue Nov 19, 2019 6:54 pm

I have Th CM Storm keyboard with led backlight. The default key to on-off the backlight is scroll lock in windows. In linux mint there is a command from the terminal to turn on the backlight, the command is: "xset led 3".
Now, I did bind the scroll lock key with the xset command and it works fine when I want to turn on the backlight, but to turn it off I must type to a terminal window
"xset -led 3"
Is there a way to create a script to bind to the scroll lock key that could read the state of the "led 3" when I press it, and execute the opposite "xset" command essentially turning the backlight on and off?
Last edited by Chrisriz on Mon Dec 02, 2019 12:22 pm, edited 1 time in total.

1337haXXor123
Level 1
Level 1
Posts: 11
Joined: Tue Jun 11, 2019 5:31 pm

Re: Scroll Lock in Cooler master storm keyboard binding

Post by 1337haXXor123 » Sun Dec 01, 2019 9:39 pm

xset q seems to show the state of the various LEDs (among other information). Combine with sed or awk to programmatically read the status of your LED 3. Then use an if/else construct to determine which command gets run to change its state, e.g.

Code: Select all

#!/bin/bash
if [ $(xset q|sed or awk command that extracts led 3 state) == off ]; then
	xset led 3
else
	xset -led 3
fi
Now you just need to figure out the right sed or awk wizardry...

Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

Re: Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Mon Dec 02, 2019 11:13 am

1337haXXor123 wrote:
Sun Dec 01, 2019 9:39 pm
xset q seems to show the state of the various LEDs (among other information). Combine with sed or awk to programmatically read the status of your LED 3. Then use an if/else construct to determine which command gets run to change its state, e.g.

Code: Select all

#!/bin/bash
if [ $(xset q|sed or awk command that extracts led 3 state) == off ]; then
	xset led 3
else
	xset -led 3
fi
Now you just need to figure out the right sed or awk wizardry...
Thank you, I will see what I can do with it! One question though, the sed and awk are commands for files, yes? So now I have to print to a file the status of my LED in order to use them? So, everytime I press Scroll_Lock it will generate a file whith the status, then I will use one of the commands and after that delete the file? And all that in script ofcourse. Am I right?

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: Scroll Lock in Cooler master storm keyboard binding

Post by rene » Mon Dec 02, 2019 11:33 am

Nah, no, stdout is just as much a file.

I.e., if you don't know how to script this yourself, just post the output of xset q together with the information which part of it shows the state of "led 3". He/we will then be able to complete his/1337haXXor123's suggestion...

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: Scroll Lock in Cooler master storm keyboard binding

Post by rene » Mon Dec 02, 2019 12:06 pm

Sorry, could not resist. Works to toggle Scroll Lock here:

Code: Select all

#!/bin/sh
if xset q | awk '/LED mask:/ { exit and($NF, 4) }'; then
	xset led 3
else
	xset -led 3
fi
Save as e.g. ~/bin/toggle.sh, make executable with chmod +x ~/bin/toggle.sh and call it from your keybind.

Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

Re: Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Mon Dec 02, 2019 12:22 pm

rene wrote:
Mon Dec 02, 2019 12:06 pm
Sorry, could not resist. Works to toggle Scroll Lock here:

Code: Select all

#!/bin/sh
if xset q | awk '/LED mask:/ { exit and($NF, 4) }'; then
	xset led 3
else
	xset -led 3
fi
Save as e.g. ~/bin/toggle.sh, make executable with chmod +x ~/bin/toggle.sh and call it from your keybind.
Thanks a lot!!! It works perfect!! Can you please explain the { exit and($NF, 4)} part?

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by rene » Mon Dec 02, 2019 12:48 pm

Great. And sure. The awk program in full is:

Code: Select all

/LED mask:/ { exit and($NF, 4) }
That first part /LED mask:/ has awk consider only lines that in fact contain the string "LED mask:", input that's piped into it from the output of xset q. If you look at said output you find that the last whitespace-seperated field on said line, $NF, is an actual bitfield, with the state of "led 3" at position 3-1 (since zero-based), i.e., extracted by binary AND-ing it with 2^(3-1) = 4 That is, and($NF, 4) is either 0 or 4 depending on the state being inactive or active respectively. The awk program then exits providing this value 0 or 4 as its exit code, to be tested by the if directly: 0 is considered true, anything else (i.e., 4) false. So, if the state is inactive, it runs the if branch, if active the else one.

A note for the observant: yes, I believe that that xset q LED mask starting with a "0" means that awk thinks it's octal. I also believe that I couldn't care less in this case.

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by rene » Mon Dec 02, 2019 2:09 pm

rene wrote:
Mon Dec 02, 2019 12:48 pm
A note for the observant: yes, I believe that that xset q LED mask starting with a "0" means that awk thinks it's octal. I also believe that I couldn't care less in this case.
Ah, darn, no, I do of course care when a hexdigit appears such as after xset led 3 && xset led 4; was a bit too enthusiastically minimal there. I.e., please tweak to

Code: Select all

if xset q | awk '/LED mask:/ { exit and(strtonum("0x" $NF), 4) }'; then
[ ... ]

User avatar
LinWinux
Level 4
Level 4
Posts: 299
Joined: Fri Aug 30, 2013 9:04 pm
Location: East Coast

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by LinWinux » Tue Dec 03, 2019 9:03 am

I have this identical problem with the scroll/lock key that's supposed to turn the LED keyboard on and off in Windows. Since I have the XFCE desktop, this would work for me as well,but ...

... I NEED SOME HELP PLEASE ...

... because I don't know anything about writing scripts.
I do use the terminal once in awhile for updates, upgrades, and other minor things, but I've never scripted a file in order to perform some kind of a function for me. Can someone please provide me with detailed step by step instructions on how to go about getting a script like this to work? I would really appreciate it. Thank you.

.
PLEASE ... Don't respond to questions with meaningless or thoughtless comments!
PLEASE ... Help others, don't be lazy! Take a moment to mark your post SOLVED if you received a solution.

Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Tue Dec 03, 2019 9:49 am

rene wrote:
Mon Dec 02, 2019 12:48 pm
Great. And sure. The awk program in full is:

Code: Select all

/LED mask:/ { exit and($NF, 4) }
That first part /LED mask:/ has awk consider only lines that in fact contain the string "LED mask:", input that's piped into it from the output of xset q. If you look at said output you find that the last whitespace-seperated field on said line, $NF, is an actual bitfield, with the state of "led 3" at position 3-1 (since zero-based), i.e., extracted by binary AND-ing it with 2^(3-1) = 4 That is, and($NF, 4) is either 0 or 4 depending on the state being inactive or active respectively. The awk program then exits providing this value 0 or 4 as its exit code, to be tested by the if directly: 0 is considered true, anything else (i.e., 4) false. So, if the state is inactive, it runs the if branch, if active the else one.

A note for the observant: yes, I believe that that xset q LED mask starting with a "0" means that awk thinks it's octal. I also believe that I couldn't care less in this case.
Ok, I'm a little confused... (Maybe because english is not my native language).
What I didn't understand is where $NF, 4 corresponds to...
Step by step is going something like this? -->
Step 1: I call xset q
Step 2: I "tell" awk to look at the line that has the LED mask:" string (and since its only one line with that string Its easier)
Step 3: I "tell" awk to exit and give me the output of $NF which the if-then loop will use
What I dont understand is that the xset q returns "LED mask: 00000006" and the only difference between the on and off state of the LED is the last digit (6 for on, 2 for off) Where is that whitespace you said and how did you know what it was?
Last edited by Chrisriz on Tue Dec 03, 2019 9:57 am, edited 1 time in total.

Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Tue Dec 03, 2019 9:56 am

LinWinux wrote:
Tue Dec 03, 2019 9:03 am
I have this identical problem with the scroll/lock key that's supposed to turn the LED keyboard on and off in Windows. Since I have the XFCE desktop, this would work for me as well,but ...

... I NEED SOME HELP PLEASE ...

... because I don't know anything about writing scripts.
I do use the terminal once in awhile for updates, upgrades, and other minor things, but I've never scripted a file in order to perform some kind of a function for me. Can someone please provide me with detailed step by step instructions on how to go about getting a script like this to work? I would really appreciate it. Thank you.

.
first of all you can use the xset command to toggle it on and off xset led 3 is what you type to the terminal. 3 Is the number that worked for me (and others), If it doesn't work for you, you can try numbers from 1-32. So xset led 1-32 to switch on, and xset -led 1-32 to switch off. If that works maybe the exact same solution works for you to!

User avatar
LinWinux
Level 4
Level 4
Posts: 299
Joined: Fri Aug 30, 2013 9:04 pm
Location: East Coast

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by LinWinux » Tue Dec 03, 2019 10:53 am

Thank you, that worked on my keyboard with the 3 in the command as well. Now I just have to figure out how to map that command so the scroll/lock key on the keyboard will accomplish the same thing. Any thoughts, ideas, or step by step solutions on that?

.
PLEASE ... Don't respond to questions with meaningless or thoughtless comments!
PLEASE ... Help others, don't be lazy! Take a moment to mark your post SOLVED if you received a solution.

Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Tue Dec 03, 2019 5:43 pm

LinWinux wrote:
Tue Dec 03, 2019 10:53 am
Thank you, that worked on my keyboard with the 3 in the command as well. Now I just have to figure out how to map that command so the scroll/lock key on the keyboard will accomplish the same thing. Any thoughts, ideas, or step by step solutions on that?

.
Nice! Now Open a text editor and copy-paste the following code from Rene's post

Code: Select all

#!/bin/sh
if xset q | awk '/LED mask:/ { exit and($NF, 4) }'; then
	xset led 3
else
	xset -led 3
fi
After that save it as filename.sh to a folder. (I've made a folder for scripts in my home folder), after saving it go to terminal and make it executable with the following command chmod +x ~/folder you put it/filename.sh, after that go to settings, find the keyboard settings, find the bindings, and bind the scroll_lock key to the script you just made

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by rene » Wed Dec 04, 2019 1:38 am

Note; while it works as is in a practical sense, you want to tweak that a bit as per my above follow-up to say:

Code: Select all

#!/bin/sh
if xset q | awk '/LED mask:/ { exit and(strtonum("0x" $NF), 4) }'; then
	xset led 3
else
	xset -led 3
fi
Looks worse; is better :)

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by rene » Wed Dec 04, 2019 2:29 am

Chrisriz wrote:
Tue Dec 03, 2019 9:49 am
Step 1: I call xset q
Yes.
Chrisriz wrote:
Tue Dec 03, 2019 9:49 am
Step 2: I "tell" awk to look at the line that has the LED mask:" string (and since its only one line with that string Its easier)
Yes.
Chrisriz wrote:
Tue Dec 03, 2019 9:49 am

Step 3: I "tell" awk to exit and give me the output of $NF which the if-then loop will use
Not "the output of $NF" directly, no.

awk works on lines, and splits these lines on (by default) whitespace-boundaries into what it calls "fields". Explicitly then, the '/LED mask:/' bit selects as explained from the output of xsel q the line, say,

Code: Select all

  auto repeat:  on    key click percent:  0    LED mask:  00000006
and breaks it into fields it denotes $1 to $10, "auto", "repeat:", "on"", ..., "mask:", "00000006". That last field is the one we are interested in; $NF is another name for the last field of an input line --- NF stands for Number of Fields, (only) here 10 --- so that's the only field we furthermore concern ourselves with.

That field is a string consisting of a hexadecimal representation of a 32-bit bitfield representing the current state of the LEDs:

Code: Select all

| S32 | S31 | .... | S4 | S3 | S2 | S1 |
in which bit Sn being 1 means the current state of LED n being active, it being 0 meaning inactive. As such, to probe the state of LED 3 we are interested in bit S3 and, as a standard thing in programming, therefore mask it and only it off by applying a binary AND to that current value and the value in which only S3 is set:

Code: Select all

| S32 | S31 | ... | S4 | S3 | S2 | S1 |
|  0  |  0  | ... |  0 |  1 |  0 |  0 |
--------------------------------------- AND
|  0  |  0  | ... |  0 | S3 |  0 |  0 | 
The hexadecimal (and decimal, and octal, ...) representation of that binary value 00...0100 we are AND-ing with is 4, and we are as the result left with either 0 if S3 is 0, 4 if S3 is 1.

That resulting value 0 or 4 is what the awk program then exits with as its exit code. Back in sh-land, the if then interprets the exit code 0 of awk as "true", takes the if-branch, anything else as "false", takes the else branch.

I believe that should explain the idea here. My follow-up inserting strtonum("0x" $NF) is a detail; if any field, essentially a string, is implicitly treated as an integer, awk treats it as an octal value if it starts with a 0, such as $NF here will generally do. I originally braino-ed thinking it wouldn't matter since we were testing against the value 4, represented as 4 in all of hexadecimal, decimal and octal, but that's actually rather nonsensically thought; we do need to have awk treat the hexadecimal value as hexadecimal, i.e., by prefixing it with the hexadecimal prefix "0x".

User avatar
LinWinux
Level 4
Level 4
Posts: 299
Joined: Fri Aug 30, 2013 9:04 pm
Location: East Coast

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by LinWinux » Wed Dec 04, 2019 4:36 am

My gosh, you guys are making this so much more difficult than it needs to be. I can't speak for other flavors of Mint, but the XFCE desktop settings (GUI) permit you to create keyboard function shortcuts in a matter of seconds. So if you're using a Mint Desktop with permissable keyboard shortcuts, simply do this:

Enable Scroll-Lock Key - Turn light on: xset led 3

Disable Home Key - Turn light off: xset -led 3

Alternatively you could also try a single command to perform both on/off functions on the same scroll/lock key, by instead creating a single keyboard shortcut with this command instead (still using the number 3 for the same key):

Code: Select all

sh -c "xset -q -display :0 | grep -q 'Scroll Lock: on' && xset -led 3 || xset led 3"
There are 32 different key functions so if "3" doesn't work for you, you'll have to try some of the other numbers 1 - 32 instead. right now, on my lighted keyboard, the scroll/lock key works as a permanent on switch, while the home key which in my keyboard is located directly underneath the scrol/lock key, turns the light back off again. I would imagine that on most generic keyboards the Scroll/Lock and Home Keys are mapped the same though. Just try it.

.
PLEASE ... Don't respond to questions with meaningless or thoughtless comments!
PLEASE ... Help others, don't be lazy! Take a moment to mark your post SOLVED if you received a solution.

rene
Level 12
Level 12
Posts: 4453
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by rene » Wed Dec 04, 2019 6:31 am

What you are responding to does not concern setting up a keybind (something which one message ago you said to not know how to do, by the way) but only explains the logic behind the posted toggle script/command, since OP wants to know from an intellectual standpoint; he already has it working...

Chrisriz
Level 1
Level 1
Posts: 16
Joined: Fri Nov 15, 2019 1:47 am

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by Chrisriz » Wed Dec 04, 2019 2:47 pm

LinWinux wrote:
Wed Dec 04, 2019 4:36 am
My gosh, you guys are making this so much more difficult than it needs to be. I can't speak for other flavors of Mint, but the XFCE desktop settings (GUI) permit you to create keyboard function shortcuts in a matter of seconds. So if you're using a Mint Desktop with permissable keyboard shortcuts, simply do this:

Enable Scroll-Lock Key - Turn light on: xset led 3

Disable Home Key - Turn light off: xset -led 3

Alternatively you could also try a single command to perform both on/off functions on the same scroll/lock key, by instead creating a single keyboard shortcut with this command instead (still using the number 3 for the same key):

Code: Select all

sh -c "xset -q -display :0 | grep -q 'Scroll Lock: on' && xset -led 3 || xset led 3"
There are 32 different key functions so if "3" doesn't work for you, you'll have to try some of the other numbers 1 - 32 instead. right now, on my lighted keyboard, the scroll/lock key works as a permanent on switch, while the home key which in my keyboard is located directly underneath the scrol/lock key, turns the light back off again. I would imagine that on most generic keyboards the Scroll/Lock and Home Keys are mapped the same though. Just try it.

.
LinWinux I could bind two keys to turn on and off the backlight but I didn't wanted to... I wanted to work as it where, one key for on and off and that script is doing exactly that
rene wrote:
Wed Dec 04, 2019 6:31 am
What you are responding to does not concern setting up a keybind (something which one message ago you said to not know how to do, by the way) but only explains the logic behind the posted toggle script/command, since OP wants to know from an intellectual standpoint; he already has it working...
Agreed! And thanks for the explanation :)

1337haXXor123
Level 1
Level 1
Posts: 11
Joined: Tue Jun 11, 2019 5:31 pm

Re: [SOLVED] Scroll Lock in Cooler master storm keyboard binding

Post by 1337haXXor123 » Fri Dec 06, 2019 7:06 pm

LinWinux wrote:
Wed Dec 04, 2019 4:36 am
Alternatively you could also try a single command to perform both on/off functions on the same scroll/lock key, by instead creating a single keyboard shortcut with this command instead (still using the number 3 for the same key):

Code: Select all

sh -c "xset -q -display :0 | grep -q 'Scroll Lock: on' && xset -led 3 || xset led 3"
Nice oneliner! It never occurred to me you can do if-then-else statements like this. It also didn't occur to me to just grep for Scroll Lock: on and if it doesn't find it it means Scroll Lock is off. Man, I'm so disappointed in my brain sometimes :mrgreen:

Post Reply

Return to “Scripts & Bash”