Skip to content

Commit

Permalink
Eject key - modify it! The super simple way
Browse files Browse the repository at this point in the history
  • Loading branch information
verityj committed Jun 28, 2023
1 parent 7b7aa30 commit fb342e6
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions _posts/2023-06-17-eject.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ Here is a command that uses `hidutil` command. Takes in the eject key as source

```
$ hidutil property --set '{"UserKeyMapping":
[{"HIDKeyboardModifierMappingSrc":0xC000000b8,
"HIDKeyboardModifierMappingDst":0x70000004c}]
[{"HIDKeyboardModifierMappingSrc":0xC000000b8,
"HIDKeyboardModifierMappingDst":0x70000004c}]
}'
# Output of the above command:
# Output of the above command:
UserKeyMapping:(
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 51539607736;
}
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 51539607736;
}
)
```
```
$ hidutil property --get "UserKeyMapping"
# shows existing user key maps
# shows existing user key maps
(
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 51539607736;
}
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 51539607736;
}
)
```

Expand All @@ -44,8 +44,8 @@ $ hidutil property --set '{"UserKeyMapping":[null]}'
UserKeyMapping:(
)
$ hidutil property --get "UserKeyMapping"
# shows existing user key maps
# now there are none:
# shows existing user key maps
# now there are none:
(
)
```
Expand Down Expand Up @@ -73,7 +73,7 @@ If you notice, I am using **two** key bindings. The first source is the _eject_

Once you create the above file, _System Settings_ may show a notification that _hidutil_ has been added to your _Login Items_. That makes sense, this will be executed after you log in. For example, these key changes will not be active on the log in screen (unless you put this file at the system level, not user).

<img class="centered" width="50%" border="unset" src="/assets/images/2023-06-17a.png" />
<img class="centered" style="width: 50%; border: unset; border: none;" src="/assets/images/2023-06-17a.png" />

Reboot to see that all is functioning as intended. Or launch the service manually:

Expand All @@ -93,14 +93,14 @@ After a reboot or service start, check that the keys are indeed mapped:
```
$ hidutil property --get "UserKeyMapping"
(
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 51539607736;
},
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 30064771302;
}
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 51539607736;
},
{
HIDKeyboardModifierMappingDst = 30064771148;
HIDKeyboardModifierMappingSrc = 30064771302;
}
)
```

Expand All @@ -114,11 +114,11 @@ We can expand on this idea of identifying a keyboard key by its code in order to

```
for ((i=1;i<=128;++i)); do
printf '0x7000000%0x\n' "$i"
printf '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x7000000%0x,"HIDKeyboardModifierMappingDst":0x70000000a}]}' "$i" |
xargs -0 hidutil property --set >/dev/null
read -p "Type some stuff: "
hidutil property --set '{"UserKeyMapping":[{}]}' >/dev/null
printf '0x7000000%0x\n' "$i"
printf '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x7000000%0x,"HIDKeyboardModifierMappingDst":0x70000000a}]}' "$i" |
xargs -0 hidutil property --set >/dev/null
read -p "Type some stuff: "
hidutil property --set '{"UserKeyMapping":[{}]}' >/dev/null
done
```

Expand All @@ -138,3 +138,9 @@ I also came across a blog post which goes into the benefit of not relying on cus
[^4]: Remapping keys - Apple developer [technical note TN2450](https://developer.apple.com/library/archive/technotes/tn2450/_index.html).
[^5]: [Script source](https://apple.stackexchange.com/a/349440) in a discussion thread.
[^6]: Rakhesh's [blog post](https://rakhesh.com/mac/using-hidutil-to-map-macos-keyboard-keys/)

## Say Hi!

<span class="nocomment">
<script src="https://nocomment.fiatjaf.com/embed.js" id="nocomment" data-owner="npub1vy40z9dxr943vkz6xp54elflf7hxcly46q2qwcpvzfy47qq3syxqqchgk3"></script>
</span>

0 comments on commit fb342e6

Please sign in to comment.