I'm trying to write a bash script to swap the command and control keys for when I'm doing a lot of emacs work. I know I can do this from the Keyboard/Mouse PreferencesPane, but it would be faster if I could just run a script.
I swapped the keys via the PreferecePane and then ran defaults -currentHost read -g to see what was changed by the PreferencePane. I then used that info to write the following bash script to swap the keys:
The script takes one argument, if the argument is emacs, then it swaps command and control, if the argument is anything else it restores the defaults. When I run the script it seems to make the appropriate changes when I run defaults -currentHost read -g, but there seems to be no effect (they keys behave the same as before the script was run and the changes are not reflected in the PreferencePane). Is there some sort of refresh I need to do to make these changes go into effect after running the script?
I swapped the keys via the PreferecePane and then ran defaults -currentHost read -g to see what was changed by the PreferencePane. I then used that info to write the following bash script to swap the keys:
Code:
#!/bin/bash
mappingplist=com.apple.keyboard.modifiermapping
if [ $1 == "emacs" ]; then
echo "Switching to emacs modifiers"
defaults -currentHost write -g $mappingplist '(
{
HIDKeyboardModifierMappingDst = 4;
HIDKeyboardModifierMappingSrc = 2; },
{
HIDKeyboardModifierMappingDst = 12;
HIDKeyboardModifierMappingSrc = 10;
},
{
HIDKeyboardModifierMappingDst = 2;
HIDKeyboardModifierMappingSrc = 4;
},
{
HIDKeyboardModifierMappingDst = 10;
HIDKeyboardModifierMappingSrc = 12;
})'
else
echo "Switching to default modifiers"
defaults -currentHost delete -g $mappingplist
fi
The script takes one argument, if the argument is emacs, then it swaps command and control, if the argument is anything else it restores the defaults. When I run the script it seems to make the appropriate changes when I run defaults -currentHost read -g, but there seems to be no effect (they keys behave the same as before the script was run and the changes are not reflected in the PreferencePane). Is there some sort of refresh I need to do to make these changes go into effect after running the script?