If you’re using the Logitech MXKeys keyboard via Bluetooth on macOS and want to swap the right CMD (⌘) and OPT (⌥) keys, this guide will walk you through remapping the keys using a custom script and a plist configuration.
Why Remap?
The default key mapping on macOS for the MXKeys might not match your personal preference, especially if you’re coming from another operating system or have a specific workflow in mind. In this case, we want to swap the right-side CMD (⌘) and OPT (⌥) keys to better suit the user’s needs.
By following this guide, you’ll set up a script that automatically remaps these keys every 60 seconds to ensure it works even if the keyboard is turned off and on again.
Step 1: Create the Key Remapping Script
The first step is to create a simple shell script that uses macOS’s built-in hidutil tool to remap the keys.
Create a file named mxkeys_remap.sh in your scripts directory, for example: /Users/mhoroszkiewicz/scripts/mxkeys_remap.sh.
mxkeys_remap.sh
#!/usr/bin/env bash
hidutil property --matching '{"ProductID":0xB35B}' --set '{"UserKeyMapping":
[{"HIDKeyboardModifierMappingSrc":0x7000000e7,
"HIDKeyboardModifierMappingDst":0x7000000e6},
{"HIDKeyboardModifierMappingSrc":0x7000000e6,
"HIDKeyboardModifierMappingDst":0x7000000e7}]
}'
This script uses hidutil to map the keys for devices with the ProductID 0xB35B, which corresponds to the MXKeys keyboard.
Explanation:
0x7000000e7: Refers to the right CMD key. 0x7000000e6: Refers to the right OPT key. The script swaps these keys’ functionality. Save this script and ensure it’s executable:
chmod +x /Users/mhoroszkiewicz/scripts/mxkeys_remap.sh
Step 2: Create a Launch Agent to Run the Script Automatically
Next, you’ll create a plist file to ensure the script is executed regularly. This file will schedule the script to run every 60 seconds using launchd.
Create a file named com.user.keyboardremap.plist in ~/Library/LaunchAgents/ with the following content:
com.user.keyboardremap.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.keyboardremap</string>
<key>ProgramArguments</key>
<array>
<string>/Users/mhoroszkiewicz/scripts/mxkeys_remap.sh</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Explanation:
Label: This is a unique identifier for the process (com.user.keyboardremap). ProgramArguments: Specifies the full path to the remapping script. StartInterval: The script will run every 60 seconds to ensure the keys are correctly mapped, even if the keyboard is disconnected or reconnected. RunAtLoad: Ensures the script is executed immediately when the system is booted or the user logs in.
Step 3: Load the Launch Agent
Once you’ve created the plist, you need to load it into launchd so it starts running.
To load the plist, use the following command:
launchctl load ~/Library/LaunchAgents/com.user.keyboardremap.plist
This command tells launchd to start managing your script, running it as specified (every 60 seconds).
If you ever want to unload the agent (stop it from running), you can use:
launchctl unload ~/Library/LaunchAgents/com.user.keyboardremap.plist
Step 4: Test the Setup
To ensure everything works correctly:
Log out and log back in or restart your Mac. After the login, wait a few moments for the script to run. Try using the right CMD (⌘) and OPT (⌥) keys to verify that they have been swapped.
Conclusion
By following this guide, you’ve successfully configured a script to swap the CMD and OPT keys on your MXKeys keyboard and set it to run periodically to handle situations where the keyboard might be turned off or disconnected.
This method provides flexibility, ensuring that your preferred key mapping is always applied, without the need for manual intervention.