Hijacking the right command key (Macbook)
Hijacking the right command key (Macbook)
When creating the perfect work environment. You sometimes want to customize a few tools. That you cannot always easily customize without a third party. This was an attempt to circumvent that. While going right to the source.
Let's start off with why I might want to do something like changing around my keyboard for my daily use. Can't I just learn how to use something and move on. Well, yes. The only downside of just using the keys that are already preset. Is these keys like to change on different keyboards. So the task is just making it easier to work with your tools.
Let's go over a few aspects that I have already changed for this post. Along with some default mentions.
- Tmux uses
Control + a
as it's trigger. - Macbook uses
Command +
many different keys.
Some of the habits I have built up were around Mac, Windows, Linux (Ubuntu, NixOS, etc.). So to find a key pattern that works across all of these operating systems is a challenge. Some of the keys are in different positions, but that doesn't mean you can just remap to the keys where you want them. There might be conflicts.
For my daily driver in the office I have a fancy split keyboard. That moves the keys into a better and easier to access area. Where my thumbs can handle most of the heavy lifting. Which is something that doesn't really happen on the laptop. You're forced into a more un-ergonomical way of typing. This is fine, and if you want to be mobile. It's a muscle memory that we must move past. Shout out to Kinesis their Advantage2 and 360 pro. I am a huge fan, but that's not what we are writing about today.
- Switch the option key with control.
- 🚀 Switch escape with the left caps lock for easy vim use.
Right away I make the modification to move the control into the place of the option keys. This removed the need for clicking on the control with my pinky. Which I find to be some of the most challenging fingers to use all day. It's very easy to get lost having to move your hands around that much.
That's really the problem that we are working to fix. Having our hands move around too much on a laptop. Unless you can touch the home keys. You may not really know where you are on the keyboard. If you move your hands from it normal position. How we are going to modify this is how we get to the script we are writing about.
💬 Does it even matter that your hands move around... I think so.
Now, since we changed the control and the option keys. We now have to choose how we activate this key.
We choose to use the left thumb and bend it slightly. So instead of hitting command. We hit the option key. The option key that was switched to have the modifier of control. So this allows us to use our left thumb for both of the modifier keys I will be using.
We also have the option key on the right side of the keyboard as well. This key is just hard to use without having to move your hands. Since our overall goal is to keep close to home as much as possible.
Wouldn't it be cool if you could also move the control modifier functionality to the command on the right side of the keyboard? 💡
There was a thought in my head. How about I change the right command key with control as well. Which didn't seem possible without a third party software. At least with a simple search. I did not find what I wanted within the rules. The rules don't always matter if you go third party, but if someone else can do it. Maybe we can ourselves.
This is where
hidutil
comes in. We can use this program to remap keys on a mac.
The hard part is figuring out what the right key code is. The codes I kept finding in my search did not all work. There was a few tries where this script messed up my keyboard. This is fine. It was a temp change. I just rebooted my computer every time I messed up.
⚠️ If you mess up your keyboard this way. It seems to be temporary. A reboot would reset my keys back to the default. To do this every time. It would have to go into it's own startup script.
So with a good search of the internet. I believe I came across this blog post. Which has a table at the bottom. USB HID Usage Tables Specification, Section 10 Keyboard /Keypad Page. Then you have to convert it into the correct format for the command. There may be a better way. I'm just not sure.
- Run the script with
sudo
. - Create a rule for the remapping.
- Set the property in hidutil. Using the rule.
One note is that this command would need administrator to run. So sudo
is required.
We create a string JSON object with the key of UserKeyMapping. This has
an array of the key remaps with the keys HIDKeyboardModifierMappingSrc
and HIDKeyboardModifierMappingDst
. We just want to make the right command
0x7000000E7
gets remapped with the control key 0x7000000E0
. So the
source that we want to map is the right command key. The destination key
the key that is remapping the source. It feels backwards, but that's
just how it is.
Our JSON is
'{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x7000000E7,"HIDKeyboardModifierMappingDst":0x7000000E0}]}'
Let's checkout the script we will be using. Then we invoke it inside of
hidutil
like hidutil property --set "$rule"
. Where rule is the JSON
string we have above. Seems simple enough.
#!/usr/bin/env bash
# Check if the script is run with sudo
if [[ $(id -u) -ne 0 ]]; then
echo "Please run this script with sudo."
exit 1
fi
# Create a rule to remap the right Command key to the left Control key
rule='{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x7000000E7,"HIDKeyboardModifierMappingDst":0x7000000E0}]}'
# Apply the rule using hidutil
hidutil property --set "$rule"
echo "Right Command key remapped to Left Control key."
If we make sure this script it executable. We can then run it with
whatever filename we created for it. sudo commandControlRemap
. Then
use the right command key as a control key.
From my understanding you cannot do that just in the settings. So this little script is something that we will have to use. Anytime I want the macs I use to have this modification.
So why did we do this again?
This script is not something that we can expect everyone to want. It's going to be for a select group of people who do this for hobby or work. Creating a working environment that we can thrive in without having to move around too much. Less smaller distractions while we want to be focused on a more important task. If that's the type of modifications you're into. Welcome to the party 🧙!
It's hard to fully understand the workflow of someone else. Which it's very personal. We all choose to build it out a bit different. While we learn from others around us. Ways that we could use these tools. So keep on learning and educating around you. So we can inspire more people like us.