Keyboard LED control

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SomeNewb
Posts: 52
Joined: 08 Jan 2017, 18:43

Keyboard LED control

Post by SomeNewb » 08 Jan 2017, 18:50

Can someone please explain the code in the following page to us mere mortals?

https://autohotkey.com/board/topic/9587 ... ck-lights/

How does one go about to make it work, assign a key to it? Just run the script? well tried both but doesn't work.
A simple explanation for a noob please, or does anyone know if you can reverse the numlock LED state using AHK? As in turn off the numlock led when numlock state is on.

Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Keyboard LED control

Post by Noesis » 08 Jan 2017, 21:44

Have you tried different keyboard parameters ? If not make sure you're using the one on the second page by Lexicos https://www.autohotkey.com/forum/viewto ... 000#468000

So using: KeyboardLED(LEDvalue, Cmd, Kbd=0) as the template,

LedValue: Send 1(scroll lock) 2(numlock) or 4(capslock) as the LEDValue, depending on what leds you want on or off (to do more than one add the numbers together , so a value of 7 would do all three).

Cmd: I only use "On" for the cmd (if you send 3 using "On" leds 1 & 2 will go on and 4 will go off), the others don't seem to do what I usually want so I don't use them.

Kbd: put in a number here for the keyboard you want to control. If this isn't correct nothing will happen on the keyboard your looking at if you have more than one keyboard windows sometimes changes the numbers around after a reboot so, I use this code to get the correct one but you'll need to use trial and error initially:

Code: Select all

GetKeyboardIndex(kbd := "HID\VID_045E&PID_0752\")
{
	idx := -1
	RegRead, devno, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Services\kbdclass\Enum, Count
	loop, %devno%
	{
		RegRead, devid, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Services\kbdclass\Enum, % A_Index - 1
		If (InStr(devid,kbd))
			idx := (A_Index-1)
	}
	Return idx
}
You will need to know your devices hardware ID for this part to work the value included likely won't work for you but will give you an idea of the kind of string needed. When you work out the device number, open registry editor and go to the registry key listed (ending in enum). look for the number that works and the data next to it will be the string required. Note the "\" backslashes, you'll only need the string of characters from the left including any VID & PID sections up to the backslash after the PID anything after that is actually the usb port the keyboard is plugged into, you won't need that (FYI : VID is Vendor ID, PID is Product ID).

So that's the background info. Run the following code when you press ctrl+t, it will pop up a message box after it tries to set the led's. If they came on take note of the displayed number (that's the kbd number you need to use) if not click "ok" on the message box and it will try again, and it will do this 10 times, one of them should work.

Code: Select all

^t::
Loop, 10
{
	KeyboardLED(7, "On", A_Index-1)
	Msgbox, % "kbd = " A_Index-1
}
return
Hopefully that clears things up ? :wtf:

SomeNewb
Posts: 52
Joined: 08 Jan 2017, 18:43

Re: Keyboard LED control

Post by SomeNewb » 09 Jan 2017, 02:34

Thanks a lot for your reply! Ok so I found out my kbd number and device hardware ID. But not sure what I need the hardware ID for when your second code reveals which kbd number i should be using.
Anyway this is what i set it to: KeyboardLED(LEDvalue=4, Cmd="Off", Kbd=7)
and then I ran the script, and nothing happened. The line above is the only thing I changed in Lexicos' code. Am I supposed to include my hardware ID somewhere in there too?

I'm not sure you realize exactly how noob I am :P, I suppose it's hard for coders to explain these stuff to people who know nothing about coding.

Anyway thx for the help again, but I used my hardware tinkering skillz instead of software and came up with a workaround to not get annoyed by the Numlock LED, I put some duct tape on the LED!

Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Keyboard LED control

Post by Noesis » 10 Jan 2017, 03:37

From what I'm understanding you only have a script with a function and that's all, which will do nothing on it's own, You need to call the function for it to do something. You say you modified the line which was originally "KeyboardLED(LEDvalue, Cmd, Kbd=0)", you should return it to how it was and only change the 0 in Kbd=0 to 7 if that's what worked for you.
So to explain calling the function, you need a script with the function in it (or as a library but I'm not explaining that, it's included in the help file). And then you need to call that function. eg:

Code: Select all

;you need a line like this
KeyboardLed(2,"Off",7) ; this will call the function, what is below is the function definition.  (remember 4 is capslock, 2 is numlock, 1 is scroll lock).  If you change the Kbd=0 to Kbd=7 in the definition below you could use KeyboardLED(2,"Off") without a third parameter, then 7 will be used in it's place or whatever number is after the Kbd= will be used it can always be overriden in the function call, it's simply saying if nothing is passed here, use this.
return ; marks the end of the auto execute section not really needed here as functions aren't auto executed but would be if there were labels and some other things in the script 

;Lexicos Function
KeyboardLED(LEDvalue, Cmd, Kbd=0)
....
This code should turn it off when you run it, and then finish (exit). If you explained exactly what you were trying to achieve I could help you more, but one thing to be wary of is even after running this script pressing the numlock key will change the light to whatever it normally would be (i.e. either numlock state being on or off, also the same might happen if the capslock or sroll lock keys are pressed, I cant remember). You'd need to put a numlock hotkey in to stop this which would also make the script stay active in the background instead of exiting when run. I'm a bit confused as to what you actually want to achieve. i.e. do you want the light to simply indicate the opposite to what it does or want it always off ? and do you want to be able to still change the actual numlock state i.e. the behavior of the number pad ?

Finally the hardware id thing is only necessary if your kbd number changes after a reboot, sometimes windows changes the order of the keyboards so what was once 7 might become 4. So what worked once seemingly stops working for no apparent reason. Get it working first then worry about that side of it if it stops working one day.

SomeNewb
Posts: 52
Joined: 08 Jan 2017, 18:43

Re: Keyboard LED control

Post by SomeNewb » 10 Jan 2017, 06:29

Umm k thx again for the reply. I was trying to turn the Numlock LED off at all times, and still be able to use the Numpad as numbers. ie numlock should be on but the LED off.
I just wanted to do this because the LED light is annoying, but as mentioned a duct tape did the trick, a lot less headache..:P
Anyway I also remapped all the numpad number buttons to send numbers instead of "home", "end" etc. So now I turn numlock off and still am able to use the numpad for the numbers.

Again thx for the help but this is way too complicated for me, and I've found a workaround so I'm happy. :P

autobot
Posts: 3
Joined: 14 Sep 2017, 13:19

Re: Keyboard LED control

Post by autobot » 15 Sep 2017, 12:41

This tool is new for me, thank you very much for your explanation Noesis, it really helped a lot.

autobot
Posts: 3
Joined: 14 Sep 2017, 13:19

Re: Keyboard LED control

Post by autobot » 28 Oct 2017, 17:45

I found an issue as I've been using Noesis's GetKeyboardIndex function for a while.
After a few sleeps/wakes of Windows (10), my USB keyboard's ID changes, but the ID returned by that function isn't updated, so it stops working.

My current workaround is calling KeyboardLED function in a loop, with every possible keyboard ID:

Code: Select all

	Loop, 12
	{
		KeyboardLED(2,"off",A_Index-1)
	}

I found an interesting C# project in which they are able to get the id of the keyboard which was the source of the key press:
https://www.codeproject.com/Articles/17 ... e-keyboard

Code: Select all

        private void OnKeyPressed(object sender, RawInputEventArg e)
        {
            lbSource.Text = e.KeyPressEvent.Source;
            ...
Does anyone know how to identify the ID of the keyboard which was the source of the event, using autohotkey?

Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Keyboard LED control

Post by Noesis » 29 Oct 2017, 07:49

Hi Autobot, bummer that function stopped working for you, still working for me (I think, no problems so far at least, also Win 10).

Anyway, it looks like that code project code is using RawInput, there is an AHK lib called AHKHID which you could use for the same purpose, which, can be found here: https://github.com/jleb/AHKHID.

However, to be honest, I'm pretty sure it doesn't actually use the same device ID numbers, (I can't remember 100% for certain), but I've got a vague memory of it being due to mice which are also given a device ID, via that interface, so as a result they "interfere" with the keyboard only ID, expected by the LED control function (but I may be wrong).

Another suggestion, it's possible Windows 10 has changed the registry key used for this info, or perhaps I had the wrong key to begin with, as there are a couple of other keys that (for me are all actually identical but for you they may actually be different, and I don't remember if they were present in Win7 when I wrote that ID function). the 3 registry keys (i.e. 3 meaning including the one in the code you're already using) are located in the same "parent key", so have a look in your registry at the key:

SYSTEM\CurrentControlSet\Services\kbdclass\Enum

Which is the one already being used, and compare it with the same key but replacing kbdclass with kbdhid and/or keyboard. Like I said all three have an Enum subkey and contain the count value and indexes with keyboard device names, but perhaps one of them is a different order and the one you actually need.

Ps: I'd be interested in hearing how you solved it if you do manage to do so :)

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Keyboard LED control

Post by jeeswg » 02 Nov 2017, 09:10

I have an example using AHKHID here. It was quite straightforward.
NEW Barcode Input Capture (USB and RS232 compatible) - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 73#p170673
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

ThePizzaMuncher
Posts: 1
Joined: 01 May 2022, 18:41
Contact:

Re: Keyboard LED control

Post by ThePizzaMuncher » 18 May 2022, 14:36

Noesis wrote:
08 Jan 2017, 21:44
Have you tried different keyboard parameters ? If not make sure you're using the one on the second page by Lexicos https://www.autohotkey.com/forum/viewtopic.php?p=468000#468000

So using: KeyboardLED(LEDvalue, Cmd, Kbd=0) as the template,

LedValue: Send 1(scroll lock) 2(numlock) or 4(capslock) as the LEDValue, depending on what leds you want on or off (to do more than one add the numbers together , so a value of 7 would do all three).

Cmd: I only use "On" for the cmd (if you send 3 using "On" leds 1 & 2 will go on and 4 will go off), the others don't seem to do what I usually want so I don't use them.

Kbd: put in a number here for the keyboard you want to control. If this isn't correct nothing will happen on the keyboard your looking at if you have more than one keyboard windows sometimes changes the numbers around after a reboot so, I use this code to get the correct one but you'll need to use trial and error initially:

Code: Select all

GetKeyboardIndex(kbd := "HID\VID_045E&PID_0752\")
{
	idx := -1
	RegRead, devno, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Services\kbdclass\Enum, Count
	loop, %devno%
	{
		RegRead, devid, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Services\kbdclass\Enum, % A_Index - 1
		If (InStr(devid,kbd))
			idx := (A_Index-1)
	}
	Return idx
}
You will need to know your devices hardware ID for this part to work the value included likely won't work for you but will give you an idea of the kind of string needed. When you work out the device number, open registry editor and go to the registry key listed (ending in enum). look for the number that works and the data next to it will be the string required. Note the "\" backslashes, you'll only need the string of characters from the left including any VID & PID sections up to the backslash after the PID anything after that is actually the usb port the keyboard is plugged into, you won't need that (FYI : VID is Vendor ID, PID is Product ID).

So that's the background info. Run the following code when you press ctrl+t, it will pop up a message box after it tries to set the led's. If they came on take note of the displayed number (that's the kbd number you need to use) if not click "ok" on the message box and it will try again, and it will do this 10 times, one of them should work.

Code: Select all

^t::
Loop, 10
{
	KeyboardLED(7, "On", A_Index-1)
	Msgbox, % "kbd = " A_Index-1
}
return
Hopefully that clears things up ? :wtf:
I apologise for disturbing this thread’s grave, but I come with a question.

I had a look in

Code: Select all

Computer\HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\KeyboardClass
, where it lists up to KeyboardClass38 (somehow), but in

Code: Select all

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\kbdclass\Enum
it only lists up to 2. I made the second snippet run to 40 instead of 10, after which I found out that the right KeyboardClass for me was 37. Given that kbdclass\enum only shows three, how do I find the hardware ID then?

Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Keyboard LED control

Post by Noesis » 21 May 2022, 03:52

Yeah, pretty old thread, anyway, not sure when it happened but I noticed somewhere along the line, I've changed the code on my machine to use "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\keyboard\Enum" now instead of "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\kbdclass\Enum". For me in the "...\kbdclass" key there is no longer an Enum subkey at all.

Also, as I mentioned a couple of posts ago you could also try looking in the "...\kbdhid\Enum" key, which on my machine is actually the same as the keyboard one I'm currently using. Alternatively, you could just force it to use 37 (unless it changes on reboots for you). Finally I'm using Win 10 so if you're using Win 11, I have no idea what changes MS may have made in this area.

Post Reply

Return to “Ask for Help (v1)”