Macro that shows a caps lock icon in the menu bar when caps lock is activated?

I just got a new mechanical keyboard, and I'm loving it so far, except for its lack of a caps lock indicator.

I figure there must be some way to accomplish something similar with KM? Specifically, I'd like to have a macro that shows a caps lock icon in the menu bar whenever caps lock is activated. (This icon would remain in the menu bar until caps lock is turned off, at which point it would disappear.)

Any suggestions or attempts at this would be greatly appreciated!

It is probably not too difficult to get the status of the CAPS key using AppleScript. It's not difficult to get KM to display information in the menu bar, but there's usually a noticeable amount of lag in getting that menu bar updated, and I'm not sure if that's caused by macOS or by the KM Engine. My guess is that you don't want any lag at all, and I'm not sure how to solve this with zero lag. I'm pondering it.

Getting the status of CAPS is less simple than I expected. You didn't say which Mac model or which version of macOS you are using, and certain methods of getting the status of CAPS depend on this information, so I may not be able to solve it for you without that information. I've tried ioreg, hidutil, and AppleScript so far without success on my M1 Mac with Sonoma. I may be able to get the status of the CAPS button using a python script, which means KM can get the result from python, but python is something I've never used, so I'll leave it to others to try that. In the meantime, let us know what your hardware and macOS version numbers are.

1 Like

Not the answer you are expecting but UNLESS you need the capslock key, why not map it to something more useful e.g. Control?

Thanks. MacBook Pro 2023 with Sonoma 14.6.

I'm not sure I follow. I do use my caps lock key, which is why I'm looking for this solution.

While I have not yet found a way to detect the CAPSLOCK status, it is certainly possible to INFER the CAPSLOCK status by just noticing when the button is pressed (which KM can do.) It is fairly safe (but not 100.0% safe) to assume that each time the key is pressed, the CAPSLOCK status is changing from OFF to ON.

So as a test, you could try this macro. For now, the "indicator" is just toggling the dark/light mode of the KM editor. I'm sure that's not the kind of indicator that you want, but this is just for testing purposes.

I notice on my Mac that there is occasionally some lag between the press of the CAPS key and the indication of its change. This would probably be caused by the KM Engine. Any attempt to modify the system menu bar would incur ADDITIONAL lag, probably several times as much.

Since you have a different type of keyboard than mine, you would have to update the trigger of this macro to make it trigger on your own CAPSLOCK key. I trust that you know how to make that change.

If you are satisfied with this macro, we could change it to include any kind of "indicator" that you want, such as a system menu indicator. However you may not like the lag in this macro.

This macro should be updated to have a hotkey that "corrects" the value of the CAPSLOCK indicator. For example, if your CAPSLOCK appears to be in the opposite state that your indicator says it is, the macro would correct (flip) the value of the CAPS variable without changing the indication (or it could change the indication without changing the variable.)

CAPS keypress Macro (v11.0.3)

CAPS keypress.kmmacros (6.2 KB)

1 Like

Here's an AppleScript, found in the thread Programmatically Toggle the Caps Lock Key, that checks the capslock status, and returns a 1 if it is ON and a 0 if it is OFF:

use framework "Foundation"
use framework "AppKit" -- for NSEvent

property NSCapsLockMask : a reference to 65536
property NSEvent : a reference to current application's NSEvent

set KeyFlags to NSEvent's modifierFlags() as integer

if ((KeyFlags div NSCapsLockMask) mod 2) is not 0 then
	return 1
else
	return 0
end if
1 Like

That seems to work! Let me update my macro to use that. Ok here it is. It's simpler than before. It still invokes only when the CAPSLOCK key is pressed, but it actually detects its state rather than infer its state. This is better, but there's still a small lag which is slightly disappointing. The lag will be even worse if we use the Menu Bar to display the state.

Personally, I wouldn't want a visible indicator on the screen. I would want a verbal warning when the state changes. That way I would always notice the change. A small icon on the top of the screen would be useless to me because I would have already discovered the error by typing. So what I would do to make this macro work for me is have it speak the word "UPPERCASE" when this macro detects a value of "1" and have it speak the word "LOWERCASE" whenever it detects a value of "0". If you want that, you can easily change this macro to do that.

CAPS keypress Macro (v11.0.3)

CAPS keypress.kmmacros (4.3 KB)

1 Like

Thanks, @Airy and @Alexander!

I've made the modification to @Airy's macro where instead of toggling KM between dark mode and light mode, the macro toggles a macro group called "Caps lock" between activated and deactivated.

This macro group – which doesn't actually contain any macros – has a custom icon associated with it, specifically the caps lock icon.

That way, our macro shows a caps lock icon in the menu bar when caps lock and thus the caps lock macro group is activated, and removes this caps lock icon from the menu bar when caps lock and thus the caps lock macro group is deactivated.

Everything seems to be working well so far. Well done, guys!

2 Likes

Great idea. Is the lag, as I predicted, pretty bad?

You mean is there a lag for the icon to appear in the menu bar? If so, I'm not experiencing that. It's just about instantaneous for me, at least so far.

If you're happy, I'm happy. (Although I did notice the lag.)

1 Like