pqrs-org / Karabiner-Elements

Karabiner-Elements is a powerful utility for keyboard customization on macOS Sierra (10.12) or later.
https://pqrs.org/osx/karabiner/
The Unlicense
18.83k stars 838 forks source link

Controlling the Num Lock and Scroll Lock lights for our own nefarious purposes #3173

Open Muirium opened 2 years ago

Muirium commented 2 years ago

I know the Mac has no Num Lock or Scroll Lock modes. But I would like to be able to control those two LEDs on PC keyboards from Karabiner, for my own purposes.

For example: the Topre Realforce 87u.

This tenkeyless keyboard has an integrated numpad (like the IBM SSK and indeed my PowerBook) with U I O becoming 4 5 6 etc. when Num Lock is engaged. You can see the front legends on the image above. This works great in Windows, where the LED inside the Num Lock key will shine when engaged. Sadly, you get neither on the Mac: no numbers and no light. Well, I'd like to fix this.

Besides, Apple did exactly the same on the aluminium PowerBooks:

I can, and do, make my own numpad layer in Karabiner. But it would be much nicer to have the LED on the keyboard light up when this mode of mine is engaged. I'd just love to be able to send it commands for myself. And why not the Scroll Lock light as well, on all those fullsize keyboards which have all three?

Another trick I play with numpads is using them for mouse control. I have rules to convert my fullsize keyboards' numpads into a virtual mouse, with diagonals and wheel scrolling all setup on those many keys. I toggle it on and off with the Num Lock key. But again I'd really like a light on the keyboard (which always has one already!) to warn me which mode I'm in.

So how about it? Can we get commands to turn on and off the Num Lock and Scroll Lock lights in Karabiner? I'd use them to show me layer state, like I just mentioned. But the opportunities are wide indeed.

Muirium commented 2 years ago

If this is simply impossible for Karabiner to ever achieve, do say so. The Caps Lock LED sync between all attached keyboards has got my hopes up. But is that mechanism even able to address lock lights the Mac itself does not use?

MuhammedZakir commented 2 years ago

It is possible to set keyboard LEDs; there is a usage page for LEDS. However, I don't whether that will toggle keyboards' internal modes. You can test it using below programs. Note that you will have to either turn off input modification for that device, or quit KE, for this to work.

Edit: I haven't used those myself. I have checked the code, and it seems okay, but use at your own risk. Also, it's not sure whether these will work with recent macOS versions.

Muirium commented 2 years ago

Thanks for the lead, Muhammed!

I built setledsmac on my M1 Air, where it works quite nicely for Num Lock and Scroll Lock LED control on external keyboards when Karabiner isn’t running. So it’s possible to control them on the Mac after all! When Karabiner is active, setledsmac doesn’t work, just like you said.

This comment suggests scripting Karabiner’s device list when doing LED state changes. Do you know if there’s a more Karabiner-style way to do this inside a complex rule (running a shell script if necessary) rather than hacking karabiner.json externally while live? Can Karabiner’s active devices be manipulated from within a complex rule? Because momentarily switching it off, updating the LEDs, then turning it back on again could be just the trick I need.

https://github.com/damieng/setledsmac/issues/17

Muirium commented 2 years ago

Hooking up several keyboards, like a crazy person, I can report how this almost works. The lock light behaviour is a little different on different keyboards. Nothing I couldn't resolve with my own device_if complex rules in Karabiner, however. If only I could access them with it running…

Filco Majestouch 2 Tenkeyless: This USB native, non-programmable keyboard has Caps Lock and Scroll Lock lights, but no Num Lock. setledmac will manipulate "all 3", reporting no error when accessing the Num Lock which is not there. The keyboard behaves normally besides its lights. Entering Caps Lock does not GO ALL CAPS.

Topre Realforce 87u: This USB native, non-programmable tenkeyless keyboard has Caps Lock and Num Lock LEDs, integrated into their respective keys. setledmac will toggle the Caps Lock light with no effect on typing. But toggling the Num Lock light d6es 5n fact engage the 5ntegrated n40pad 06de! Turn the Num Lock light back off and the alpha keys are restored, so this keyboard does in fact internally produce numeric mode. EventViewer shows the keys as keypad_1 etc., matching all the front legends. Including even the comma key which becomes 00 by pressing 0 twice.

NMB RT8255C+: This fullsize vintage PC/AT keyboard has an internal TMK AT to USB programmable converter, and all 3 lock lights in hardware, above the numpad. setledmac freely manipulates all of them, and the keyboard simply sends the same keystrokes no matter their state. I believe this is how it's meant to be for all keyboards, without size-reducing sneaky tricks like Topre's above!

Muirium commented 2 years ago

I neglected to mention: Karabiner is kind enough to leave the Num and Scroll Lock LEDs maintain their state when it is reactivated on each altered keyboard. The tenkeyless Realforce stays in numpad mode as it should.

The only problem remains Karabiner blocking access while it is active for any given keyboard.

Muirium commented 2 years ago

I’d be very happy if Karabiner either:

  1. Allowed setting the Num Lock and Scroll Lock LEDs in complex rules. Let us look after implementing them! I just want to control the light.
  2. Allowed setleds to control those two lights, as I could run it by a shell command. Currently, Karabiner blocks it out.

I’m very pleased with my numpad mouse layer / numeric toggle and just wish I could shine the Num Lock LED right there on the vintage keyboard hardware to tell me which mode is active.

judge2005 commented 9 months ago

I just want to add some information from setleds, which is basically saying that Karabiner implements a virtual keyboard that "does not support the kHIDPage_LEDs usage page".

https://github.com/damieng/setledsmac/issues/7#issuecomment-353618163

I'm considering taking a crack at either Karabiner or setleds to see if I can implement something that works. Probably start with setleds because it is way simpler and less likely to crash my whole machine - the idea here being to see if I can get it to use a specific keyboard rather than the default keyboard, i.e. bypass the Karabiner virtual keyboard. If I can, then I can probably script it to work.

There is also a possibility to script this anyway (this is not my idea, but I can't remember where I saw it ATM) by creating a profile that does not include the physical keyboard device, then having the numlock complex rule issue a shell command that switches to a different profile, executes setleds and then switches the profile back. I'm away from my Mac or I would be trying this right now.

Anyway, it would be nice if Karabiner exposed a way for a script to do this without having to execute an external application. I might poke around there too.

judge2005 commented 9 months ago

I forgot that setleds already explicitly tries to set the hardware (as well as the karabiner virtual device), so I just wrote a shell script to switch karabiner profiles and run setleds, and then modified the numlock complex rule to call it. You can get the entire package here.

Still it would be nice if, for example, Karabiner had an internal command that controlled the keyboard leds that you could then call from a complex script instead of having to call an external script, which messes with the profiles and executes another binary...