4

I use a PC usb keyboard with my macbook. So, I've gone into System Preferences > Keyboard > Modifier Keys... and swapped command and option for the usb keyboard. This puts those keys in the "correct" position for a mac.

But, for part of the day, I remote desktop to a windows machine. So now, I need to swap the keys back, so the remote desktop client isn't confused.

It's easy enough to do this through the Preferences UI, but its cumbersome.

I'd like to use a script of some kind (maybe applescript), so I could quickly swap back and forth.

I've found several scripts online, but none of them are for Lion.

2 Answers 2

3

I was able to modify some of the scripts already out there, and get them to work in Lion. To create these scripts:

  • Run the Applescript Editor
  • Create two new empty script files (command-N)
  • Paste in these two scripts
  • Save them as something like "swap command option" and "restore command option" or whatever you'd like
  • You can test them by running them in the applescript editor.

Here is the script to swap command to option, and option to command:

#
# Script to swap the Command and Option keys
# in the System Preferences Keyboard settings.
#
# Helpful if using a PC keyboard
#

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
    tell process "System Preferences"
    click button "Modifier Keys…" of tab group 1 of window "Keyboard"

    # Select keyboard: pop up button
    click pop up button 5 of sheet 1 of window "Keyboard"
    # The 4th choice there.. my USB Keyboard
    click menu item 4 of menu 1 of pop up button 5 of sheet 1 of window "Keyboard"

    # The Option Key pop up
    click pop up button 2 of sheet 1 of window "Keyboard"
    # Change it to Command, the 4th choice
    click menu item 4 of menu 1 of pop up button 2 of sheet 1 of window "Keyboard"

    # The Command Key pop up
    click pop up button 1 of sheet 1 of window "Keyboard"
    # Change it to Option, the 3rd choice
    click menu item 3 of menu 1 of pop up button 1 of sheet 1 of window "Keyboard"

    click button "OK" of sheet 1 of window "Keyboard"

    end tell
end tell

tell application "System Preferences"
    quit
end tell

Here is the script to swap them back:

#
# Script to restore the Command and Option keys to their defaults 
# in the System Preferences Keyboard settings.
#

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
    tell process "System Preferences"
    click button "Modifier Keys…" of tab group 1 of window "Keyboard"

    # Select keyboard: pop up button
    click pop up button 5 of sheet 1 of window "Keyboard"
    # The 4th choice there.. my USB Keyboard
    click menu item 4 of menu 1 of pop up button 5 of sheet 1 of window "Keyboard"

    # The Option Key pop up
    click pop up button 2 of sheet 1 of window "Keyboard"
    # Change it to Option, the 3rd choice
    click menu item 3 of menu 1 of pop up button 2 of sheet 1 of window "Keyboard"

    # The Command Key pop up
    click pop up button 1 of sheet 1 of window "Keyboard"
    # Change it to Command, the 4th choice
    click menu item 4 of menu 1 of pop up button 1 of sheet 1 of window "Keyboard"

    click button "OK" of sheet 1 of window "Keyboard"

    end tell
end tell

tell application "System Preferences"
    quit
end tell

To make these scripts easy to access, you can go to Applescript Editor Preferences and check "Show Script menu in menu bar". Then, copy your scripts to your home directory Library/Scripts directory, i.e. /Users/ryan/Library/Scripts

Now you can access them right off the menu bar script menu.

3
  • Thanks for this! In Catalina, it looks like Option is pop up button 4 but the rest worked more or less as-is.
    – Sonafets
    Commented Feb 1, 2020 at 17:02
  • I also found click button "Restore Defaults" of sheet 1 of window "Keyboard" a more elegant / generic way to go back to defaults, assuming you don't tend to mess with any of the other mappings.
    – Sonafets
    Commented Feb 1, 2020 at 17:10
  • On my machine, there is enough delay before the Keyboard preference pane finishes loading that the window "Keyboard" reference will fail with Can’t get window "Keyboard" of process "System Preferences". You can quick fix this by adding delay 1 before the System Events tell. In theory it's possible to script out a wait and check for the window to be loaded...
    – NReilingh
    Commented Jan 2, 2021 at 22:58
1

You could also use a private.xml like this with KeyRemap4MacBook:

<?xml version="1.0"?>
<root>
  <item>
    <name>test</name>
    <identifier>test</identifier>
    <not>REMOTEDESKTOPCONNECTION</not>
    <autogen>__KeyToKey__ KeyCode::OPTION_L, KeyCode::COMMAND_L</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_L, KeyCode::OPTION_L</autogen>
    <autogen>__KeyToKey__ KeyCode::OPTION_R, KeyCode::COMMAND_R</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_R, KeyCode::OPTION_R</autogen>
  </item>
</root>

REMOTEDESKTOPCONNECTION is defined in appdef.xml.

This would make fn-escape toggle swapping option and command:

<?xml version="1.0"?>
<root>
  <item>
    <name>test</name>
    <identifier>test</identifier>
    <autogen>__KeyToKey__ KeyCode::ESCAPE, ModifierFlag::FN, KeyCode::VK_CONFIG_TOGGLE_swapoptcmd</autogen>
  </item>
  <item>
    <name>swapoptcmd</name>
    <identifier vk_config="true">swapoptcmd</identifier>
    <autogen>__KeyToKey__ KeyCode::OPTION_L, KeyCode::COMMAND_L</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_L, KeyCode::OPTION_L</autogen>
    <autogen>__KeyToKey__ KeyCode::OPTION_R, KeyCode::COMMAND_R</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_R, KeyCode::OPTION_R</autogen>
  </item>
</root>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .