Skip to content

Instantly share code, notes, and snippets.

@varenc
Created August 19, 2020 03:53
Show Gist options
  • Save varenc/039f5e7096e8a1a28443d84090b0005f to your computer and use it in GitHub Desktop.
Save varenc/039f5e7096e8a1a28443d84090b0005f to your computer and use it in GitHub Desktop.
Toggle Caps Lock light on macOS Catalina from a shell scripts/terminal
#!/usr/bin/osascript -l JavaScript
# Usage: capstoggle.sh [1 or 0, defaulting to 0]
# Pass in "1" to turn caps lock on. By default it turns caps lock off (0)
#
# Make this an executable shell script by running `chmod +x capstoggle.sh`
# Then `./capstoggle.sh 1` to turn caps lock on, and hence toggle the caps lock keyboard light!
# You can make your caps lock light flash 5 times by running this
# for i in {0..5}; do ./capstoggle.sh 1; ./capstoggle.sh 0; done
ObjC.import("IOKit");
ObjC.import("CoreServices");
var capsState;
function run(argv) {
capsState = argv[0] == '1'?1:0;
console.log("Setting caplock:",capsState);
(() => {
var ioConnect = Ref();
var state = Ref();
$.IOServiceOpen(
$.IOServiceGetMatchingService(
$.kIOMasterPortDefault,
$.IOServiceMatching(
$.kIOHIDSystemClass
)
),
$.mach_task_self_,
$.kIOHIDParamConnectType,
ioConnect
);
$.IOHIDSetModifierLockState(ioConnect, $.kIOHIDCapsLockState, capsState);
// The code below is supposed to get the current capslock state, but it never works for me
// state[0] is always false... I think it broke in Catalina.
// $.IOHIDGetModifierLockState(ioConnect, $.kIOHIDCapsLockState, state);
// console.log("--->", state[0]);
$.IOServiceClose(ioConnect);
})();
}
@Cyb3rN8
Copy link

Cyb3rN8 commented Nov 27, 2022

Got the same problem here

state always returns false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment