Skip to content

Instantly share code, notes, and snippets.

@mcharo
Last active February 7, 2023 21:25
Show Gist options
  • Select an option

  • Save mcharo/98ffd253c4093da83eaef3e761dfbc22 to your computer and use it in GitHub Desktop.

Select an option

Save mcharo/98ffd253c4093da83eaef3e761dfbc22 to your computer and use it in GitHub Desktop.
Ping output to macOS CapsLock LED
#!/usr/bin/env pwsh
[CmdletBinding()]
param(
$IpAddress,
$Count = 4
)
@'
var capsState;
function run(argv) {
capsState = argv[0] == '1'?1:0;
ObjC.import("IOKit");
ObjC.import("CoreServices");
(() => {
var ioConnect = Ref();
var state = Ref();
$.IOServiceOpen(
$.IOServiceGetMatchingService(
$.kIOMasterPortDefault,
$.IOServiceMatching(
$.kIOHIDSystemClass
)
),
$.mach_task_self_,
$.kIOHIDParamConnectType,
ioConnect
);
// GetModifierLockState seems to be broke
// $.IOHIDGetModifierLockState(ioConnect, $.kIOHIDCapsLockState, state);
// this.console.log(state);
$.IOHIDSetModifierLockState(ioConnect, $.kIOHIDCapsLockState, capsState);
$.IOServiceClose(ioConnect);
})();
}
'@ | Out-File -Path /tmp/caps.jxa
ping $IpAddress -c $Count | Where-Object { $PSItem -match 'from' } | ForEach-Object {
($PSItem -split '[=\ ]')[-2] | Tee-Object -Variable on
$off = 1000 - $on
osascript -l JavaScript /tmp/caps.jxa 1
Start-Sleep -Milliseconds $on
osascript -l JavaScript /tmp/caps.jxa 0
Start-Sleep -Milliseconds $off
}
#!/usr/bin/env bash
PING_TARGET="${1:-google.com}"
cat << EOF > /tmp/toggle.jxa
var capsState;
function run(argv) {
capsState = argv[0] == '1'?1:0;
ObjC.import("IOKit");
ObjC.import("CoreServices");
(() => {
var ioConnect = Ref();
var state = Ref();
$.IOServiceOpen(
$.IOServiceGetMatchingService(
$.kIOMasterPortDefault,
$.IOServiceMatching(
$.kIOHIDSystemClass
)
),
$.mach_task_self_,
$.kIOHIDParamConnectType,
ioConnect
);
// GetModifierLockState seems to be broke
// $.IOHIDGetModifierLockState(ioConnect, $.kIOHIDCapsLockState, state);
// this.console.log(state);
$.IOHIDSetModifierLockState(ioConnect, $.kIOHIDCapsLockState, capsState);
$.IOServiceClose(ioConnect);
})();
}
EOF
ping $PING_TARGET -c 4 -W 1000 | grep from --line-buffered | while read line; do
echo $line
on=$(echo $line | awk -F'[ =.]' '{ print $13 }')
off=$((1000-$on))
on_ms=$(printf "0.%03d\n" $on)
off_ms=$(printf "0.%03d\n" $off)
osascript -l JavaScript /tmp/toggle.jxa 1
sleep $on_ms
osascript -l JavaScript /tmp/toggle.jxa 0
sleep $off_ms
done
@varenc
Copy link

varenc commented Aug 19, 2020

the value of state that's set by IOHIDGetModifierLockState is always false for me. Any clue what's wrong?

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