Swap modifier keys with bash script

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

  1. thevibesman macrumors regular

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

    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?
     
  2. oschrenk, Jun 29, 2012
    Last edited: Jun 29, 2012

    oschrenk macrumors newbie

    Joined:
    May 29, 2012
    #2
    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

    Joined:
    Feb 7, 2012

Share This Page