Swap modifier keys with bash script

Discussion in 'macOS' started by thevibesman, Jun 24, 2010.

  1. thevibesman macrumors regular

    Oct 26, 2007
    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:

    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;                                  
        echo "Switching to default modifiers"
        defaults -currentHost delete -g $mappingplist
    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?
  2. oschrenk, Jun 29, 2012
    Last edited: Jun 29, 2012

    oschrenk macrumors newbie

    May 29, 2012
    Were you able to solve your problem?

    I tried (in OSX 10.7.4):
    - killing Finder and/or SystemUIServer
    - changing language or input source in hope to trigger some kind of reload
    - opening /System/Library/CoreServices/{KeyboardSetupAssistant.app, Setup Assistant.app} in hope to trigger some kind of reload

    Logging out and in again changes what is displayed in system settings but does not actually change the settings.

    I just want to be able to automate the task of remapping the caps lock key and found this which references your post.
  3. krasnoyarsk macrumors newbie

    Feb 7, 2012

Share This Page

2 June 24, 2010