Optimize for happiness

Life is easier with simple scripting. Grateful for everyone's encouragement and support!

View this project on GitHub

Eject key - modify it! The super simple way

2023.06.17   •   last modified 2023.06.28   •   7 min read

There is a way to assign any function to the Magic Keyboard Eject key, after all. No software to install, nothing much to mess with. Just a simple key assignment. I could not find that anywhere online so I had to go back to the basics.

First, let’s understand what the eject (⏏) key is. Eject on the Magic keyboard is a consumer one shot control (OSC) key. The official function is “OSC - removes media from the player”.1 Ha, remember when we had computers with CD drives?

Remember, it still has specific roles in two shortcuts: command (⌘) - option - eject (⏏) puts the computer to sleep, and command (⌘) - control - eject (⏏) will close all open apps and reboot without asking you. Once you change what the eject key is, these system shortcuts will not work. That will no longer be recognized as eject, and that is the goal.

Here is a command that uses hidutil command. Takes in the eject key as source and assigns forward delete as the destination:

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

At this point, the magic keyboard eject key is actually a delete forward key. Until your next reboot.

If you would like to remove all user key mappings set above:

$ hidutil property --set '{"UserKeyMapping":[null]}'
UserKeyMapping:(
)
$ hidutil property --get "UserKeyMapping"
    # shows existing user key maps
    # now there are none:
(
)

Surviving through reboots

This setting is cleared during a reboot. Which is a good thing if you just want to test a key map or don’t always use the same keyboard or whatever the situation may be.

  • You could create a Terminal command alias to execute the above command when you need it.
  • You could create an Automator script that executes that shell command and click to run it under the “Services” menu.
  • You could assign an Automator script shortcut as previously discussed.
  • Or, you could have that command run on user login:

User specific launch agents live in ~/Library/LaunchAgents/ directory.2 If you don’t have one, create it.

Inside that directory, create a property list file. Let’s call it com.local.keyremap.plist.

Here are the full contents of my file (it’s all just plain text):

(You could also use an online plist generator that has most keys available.3)

If you notice, I am using two key bindings. The first source is the eject (⏏) key, the second source is the right option key. Both destinations (meaning key functions) are forward delete. The reason for this is that I don’t always use a Magic Keyboard. My macbook built in keyboard does not have the eject key, but it does have a rarely used right option key. So I changed that key as well. I find it super convenient to have a dedicated forward delete.

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).

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

$ launchctl start com.local.keyremap

Then check if your service is on the list:

$ launchctl list | grep com.local
-	0	com.local.keyremap

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;
    }
)

Success! We have two forward delete keys. Or whatever you set using the example key definitions above. There is an Apple developer page going into detail on key remapping.4

Have fun with it! Assign some other destination key for the mighty eject. This completely avoids installing anything (and by that I mean Karabiner, covered previously). You are in control.

Side note

We can expand on this idea of identifying a keyboard key by its code in order to modify its function. If you are stuck in figuring out what the key code is of some special key you need, there is a bit of a brute force way. Go through all the regular keyboard keys (starting with 0x70)5:

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
done

This writes each assignment into a user key map, then you press that key. If you got a “g” (“0x70000000a”) then you know the source for that key is captured. This would not have worked directly for the eject, as you may remember from above, that is a consumer one shot control (OSC) key. In that case, the above loop would need to go through keys starting with the OSC identifiers.

I hope this goes over some options and ideas on how to overcome an obstinate keyboard key that you want to modify to make life easier.

Acknowledgement

I also came across a blog post which goes into the benefit of not relying on custom software to achieve the results that we have been discussing here. It gives another example of a key swap on another keyboard and Mac OS.6

Say Hi!

References

  1. Feel free to get familiar with the entirety of the human interface device (HID) usage universe

  2. The main definition and the Apple documentation for creating launch agents. 

  3. Online Mac OS key remapping generator - does not have some special keys but is a good starting point. 

  4. Remapping keys - Apple developer technical note TN2450

  5. Script source in a discussion thread. 

  6. Rakhesh’s blog post