Switching command and control keys

I’m not sure what that means.
If you run the script from Script Editor, does it toggle the Control Key setting?

If not, you may want to put a dialog at key places so you can what what is happening.

In case you don’t get it working with KM, another option to consider is another low-level utility called ControllerMate by OrderedBytes. It is completely compatible with Sierra and has a great graphical interface for configuration/remapping almost any input controller (much nicer [1] than Karabiner’s complicated XML syntax). I’ve been using CM for many years and highly recommend it. Look over the main product page to get an idea but also check out the Examples page for just a small sample of what is possible with CM.

CM is free to try out and might even stay free for you (at least for a while). It is limited to 10 building blocks that send output. For what you need, you would only need 2 (to output control and command depending on the input and the active app)

I use CM to do similar remaps to what you are wanting. For example, when I’m using VMware Fusion to run a Windows VM …

  • CM remaps the left Command key to the Windows Key but keeps the right Command key as an OSX Command key.
  • Control+\ is already used for a KM shortcut so I have CM allow the left Control+\ run the KM shortcut but intercepts the right Control+\ to do a different macro appropriate for the Windows VM.
  • CM disables the main keyboard driver map and enables a custom keyboard map where certain F-Keys are sent through as normal F-Keys for Windows functions (edit, refresh, etc) but the other F-Keys are kept as OSX “media” keys (Play/Pause, Volume, etc).
  • For the Windows F-Keys that would normally do OSX functions, I have an advanced set of CM blocks to do the OSX function when the key is held down for more than 1 second.
  • CM immediately switches the keyboard mapping as soon as I switch out of the Fusion app.

When using a multi-button mouse instead of a Magic Trackpad, I have CM configured to remap the additional buttons to do many different functions based on which application is front-most.

Just recently I was given an old DJ mixer controller which connects via USB and is seen as a MIDI instrument. Using CM, it can react to every button, knob, and slider on the mixer, including sending back MIDI commands to turn the button lights on and off. It is very fun.

This is just a very small sample of how I use CM, and I know it can do a lot more. (Just like Keyboard Maestro. I think I have maybe a dozen KM macros and I’m sure others like Peter and JMichael probably use hundreds every day)

I hope this helps. Let me know if you try CM and run into problems (although we might have to take it over to the CM forums)

[1] Please don’t get me wrong in that I don’t like Karabiner. In fact, before Sierra, I loved it and used it to do many functions. It, along with the companion app Seil, were the only apps (I thought) capable of doing what I needed. Since then, I’ve ported all those functions over to KM and CM (and BetterTouchTool). As Karabiner Elements develops, I check on it but it has yet to even come close to what can be done with KM/CM/BTT.

1 Like

Thanks for posting this. It sounds like a great tool.

I’m a bit confused about which option to use to remap the keyboard keys.
The Examples state:

The goal of this example is to demonstrate how to modify the behavior of keyboard buttons using a Driver Configuration. Driver configurations are available only for devices that use ControllerMate’s keyboard/mouse driver. ControllerMate cannot modify the behavior of another vendor’s keyboard/mouse driver or one that is included with OS X.

So which option should be used to do the remap?


EDIT: 2017-08-30  5:18 PM CT

I have a Logitech K750 keyboard, and it doesn't see like it is compatible.
I just sent this email to support:

>Is ControllerMate Compatible with Logitech K750 Keyboard?
>
>This KB does NOT appear on your ControllerMate Compatibility list.
>
If it is compatible, how would I remap the Control Key to "Command" and Command Key to "Control"?
Also, can I remap the CapsLock Key to be a "hyper" key, a combination of all modifiers:  command, control, option, shift
>
>BTW, you really need a search tool for your web site, and for your forum.

I have confirmed (with others) that the Preferences Keyboard has changed with Sierra, so it is likely that my script will not work with Sierra.

Did you install a driver for the Logitech keyboard? If so, that is why. CM can only override (disable) keys when using it’s own driver. What this means is CM can react to the keys but it can’t stop the original value. For example, in CM you have “a” set to output “bcd”. The text “abcd” will be sent. “a” from the Logitech driver and “bcd” from CM.

Using the Driver Configurations, you would disable “a” so that only “bcd” would be sent. In a simple case like Command <-> Control, you would just remap those in the Driver Configuration screen.

As a test, try uninstalling/disabling the Logitech driver and see how CM is able to interact with it. It is possible that CM will be able to replace the Logitech driver, but not always. For example, I use a Microsoft Sculpt keyboard and they don’t make OSX drivers for it. CM is able to detect almost all of the keys except for a few of the “Media” keys (they have Windows 8 Charm icons and are see by CM as multiple keys at the same time)

Thank you so much Onan for the great suggestion (and thank you JMichaelTX for your suggestions and time).

I downloaded CM (I was aware of KM and BTT, but not CM) and it's awesome. Unfortunately, when trying to use CM's Driver Configurations, the Driver Configuration is greyed out. And if I go to "Create Driver Configuration", nothing happens. Any ideas of what I'm missing? When I click on Driver Configuration in the Palette, I get the attached error:

I have KM and BTT applications both quit (in the finder and in the upper right menu bar), so I'm not sure what's already using it.

So just like JMichael, your keyboard is using its own (Apple) driver so CM is not able to take over completely. Unlike my suggestion to JMichael, you probably don’t want to try to disable the Apple keyboard driver unless you know what you are doing. You could if you have an external USB keyboard or can SSH in from another system, just in case things don’t go right and you need to re-enable the driver (probably by booting into single user service mode or SSH in)

I’ve always used external 3rd party keyboards (but using CM’s driver) so it has always worked great for me.

Thank you for your reply. Without taking over the apple keyboard completely, do you think it’s not possible to switch the command and control keys for teamviewer? (When I use Karibiner elements and manually switch back and forth between two profiles, all works great - I guess another solution would be to automate the switch between the two - would the best way to do that be an AppleScript triggered by Keyboard Maestro, or is there a software that does that more easliy?).

Can I somehow use the karabiner virtuyalHID keyboard within ControllerMate? I have trouble understanding which levels which software is controlling which software from. I’ll play around with the karabiner virtuyalHID keyboard and see what I can come up with.

If CM doesn’t have driver level control, it would only be able to add to keypresses. So if you configured CM to send Control when you pressed the Command key, the Mac would see it as both Control and Command being pressed at the same time.

Last year before the old Karabiner app broke with the OS X Sierra upgrade, I was using Karabiner and had it configured to change functions based on the active app. I don’t know the current progress of KE so I can’t say if that functionality has been added yet. Try asking/searching in the KE issue/discussion pages.

I read in the CM forum that CM will not work with any of the Apple keyboards.

Ah, ok, thank you JMichaelTX and Onan.

I’m trying to record an action with automator (that I can trigger with Keyboard Maestro) to change the Karibiner elements Profile in the menu bar from one profile to another (or if there were a key command shortcut to do that, it would be awesome and easier). I can record an action in OSX’s automator and it works when I try it, but then when I save it as an application, I get an error when running that application (or when triggering that application from Keyboard Maestro). OR, if I try to trigger an automator action from Keyboard Maestro, it shows the Automation tasks as greyed out. Any ideas of a good way to do this, or what I might be doing wrong? If this works, I’ll be all set (when I do it manually, it works great - I just always forget to change it back so it’s a pain)

@cinematree Take a look at this discussion. It is a request to have Karabiner Elements switch profiles automatically based on the active application. About half way, a user did it himself and made it available but then at the very end, another user (Luca) says that it is now a default feature (as of 2 days ago). So try updating KE and see if you can configure separate profiles for each app.

Thank you @Onan. I wasn’t yet able to find the application-based profiles as a default feature in the updated Karibiner Elements, but I asked on that thread and will hopefully get a solution. If not, I will attempt the more challenging solution on that thread. Thank you so much for directing me to that thread.

I’m getting closer to success. On Github, Transamericamoon wrote the awesome code below (https://github.com/tekezo/Karabiner-Elements/issues/808)- I have it working under “Complex modifications” in Karibiner Elements, but it works for all applications. Now I need to tweak it so that it only works when Teamviewer is the frontmost application. Could anyone please offer advice on how to tweak it for just Teamviewer?

Here are Transamericamoon’s instructions (I’m not sure how to properly format the code for this forum, but you can find it at the link above):
You can copy this example into ~/.config/karabiner/assets/complex_modifications/windows-binding.json

Got to karabiner preferences->complex Modifications->Add rule and press enable on it.

You can modify it to your liking but make sure you run it through a json validator first.

{
  "title": "Windows keys",
  "rules": [
    {
      "description": "swap control/command fix home/end/redo except for iterm2/vim",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "y",
            "modifiers": {
              "mandatory": [
                "command"
              ]
            }
          },
          "to": [
            {
              "key_code": "z",
              "modifiers": [
                "command",
                "shift"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "end"
          },
          "to": [
            {
              "key_code": "e",
              "modifiers": [
                "control"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "home"
          },
          "to": [
            {
              "key_code": "a",
              "modifiers": [
                "control"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "left_command"
          },
          "to": [
            {
              "key_code": "left_control"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "left_control"
          },
          "to": [
            {
              "key_code": "left_command"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "right_command"
          },
          "to": [
            {
              "key_code": "right_control"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "right_control"
          },
          "to": [
            {
              "key_code": "right_command"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_unless",
              "bundle_identifiers": [
                "^org\\.vim\\.",
                "^com.googlecode.iterm2"
              ]
            }
          ]
        }
      ]
    }
  ]
}

I have modified your post to add the code block syntax.

###How to Put Script in a Forum Code Block
Just insert your script between the lines with the triple backquotes:

 ```applescript
 -- Your Script Here

If your script is another language, like JavaScript, then use the keyword in lower case for that language, like:
`javascript`

Here is a macro that will paste the script on the clipboard into the forum in the proper format:
####[MACRO: KM Forum -- Paste Script Block](https://forum.keyboardmaestro.com/t/paste-script-block-in-km-forum/4047)

@JMichaelTX Thank you for that. I’l use that going forward. And thank you for fixing what I posted.

Here is the Karibiner script for when I have two profiles - one called Mac where the keys are normal and one called Windows where the command and control keys are reversed. With Karibiner set like this, I can manually switch between the two profiles. Does anyone know how to tweak this script so that the “Windows” profile will spontaneously be activated only when TeamViewer is the frontmost application in OSX? I would greatly appreciate it.

{
    "global": {
        "check_for_updates_on_startup": true,
        "show_in_menu_bar": true,
        "show_profile_name_in_menu_bar": true
    },
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {
                    "basic.to_if_alone_timeout_milliseconds": 1000
                },
                "rules": []
            },
            "devices": [
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 34304,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {
                        "escape": "vk_none"
                    }
                },
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 631,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {}
                },
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 65535,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {}
                }
            ],
            "fn_function_keys": {
                "f1": "display_brightness_decrement",
                "f10": "mute",
                "f11": "volume_decrement",
                "f12": "volume_increment",
                "f2": "display_brightness_increment",
                "f3": "mission_control",
                "f4": "launchpad",
                "f5": "illumination_decrement",
                "f6": "illumination_increment",
                "f7": "rewind",
                "f8": "play_or_pause",
                "f9": "fastforward"
            },
            "name": "Mac",
            "selected": true,
            "simple_modifications": {},
            "virtual_hid_keyboard": {
                "caps_lock_delay_milliseconds": 0,
                "keyboard_type": "ansi"
            }
        },
        {
            "complex_modifications": {
                "parameters": {
                    "basic.to_if_alone_timeout_milliseconds": 1000
                },
                "rules": []
            },
            "devices": [
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 631,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {}
                }
            ],
            "fn_function_keys": {
                "f1": "display_brightness_decrement",
                "f10": "mute",
                "f11": "volume_decrement",
                "f12": "volume_increment",
                "f2": "display_brightness_increment",
                "f3": "mission_control",
                "f4": "launchpad",
                "f5": "illumination_decrement",
                "f6": "illumination_increment",
                "f7": "rewind",
                "f8": "play_or_pause",
                "f9": "fastforward"
            },
            "name": "Windows",
            "selected": false,
            "simple_modifications": {
                "left_command": "left_control",
                "left_control": "left_command"
            },
            "virtual_hid_keyboard": {
                "caps_lock_delay_milliseconds": 0,
                "keyboard_type": "ansi"
            }
        }
    ]
}
```applescript
{
    "global": {
        "check_for_updates_on_startup": true,
        "show_in_menu_bar": true,
        "show_profile_name_in_menu_bar": true
    },
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {
                    "basic.to_if_alone_timeout_milliseconds": 1000
                },
                "rules": []
            },
            "devices": [
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 34304,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {
                        "escape": "vk_none"
                    }
                },
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 631,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {}
                },
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 65535,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {}
                }
            ],
            "fn_function_keys": {
                "f1": "display_brightness_decrement",
                "f10": "mute",
                "f11": "volume_decrement",
                "f12": "volume_increment",
                "f2": "display_brightness_increment",
                "f3": "mission_control",
                "f4": "launchpad",
                "f5": "illumination_decrement",
                "f6": "illumination_increment",
                "f7": "rewind",
                "f8": "play_or_pause",
                "f9": "fastforward"
            },
            "name": "Mac",
            "selected": true,
            "simple_modifications": {},
            "virtual_hid_keyboard": {
                "caps_lock_delay_milliseconds": 0,
                "keyboard_type": "ansi"
            }
        },
        {
            "complex_modifications": {
                "parameters": {
                    "basic.to_if_alone_timeout_milliseconds": 1000
                },
                "rules": []
            },
            "devices": [
                {
                    "disable_built_in_keyboard_if_exists": false,
                    "fn_function_keys": {},
                    "identifiers": {
                        "is_keyboard": true,
                        "is_pointing_device": false,
                        "product_id": 631,
                        "vendor_id": 1452
                    },
                    "ignore": false,
                    "simple_modifications": {}
                }
            ],
            "fn_function_keys": {
                "f1": "display_brightness_decrement",
                "f10": "mute",
                "f11": "volume_decrement",
                "f12": "volume_increment",
                "f2": "display_brightness_increment",
                "f3": "mission_control",
                "f4": "launchpad",
                "f5": "illumination_decrement",
                "f6": "illumination_increment",
                "f7": "rewind",
                "f8": "play_or_pause",
                "f9": "fastforward"
            },
            "name": "Windows",
            "selected": false,
            "simple_modifications": {
                "left_command": "left_control",
                "left_control": "left_command"
            },
            "virtual_hid_keyboard": {
                "caps_lock_delay_milliseconds": 0,
                "keyboard_type": "ansi"
            }
        }
    ]
}

I think it will use something like this below, but I don’t know how/where to fit it into the above script:

 "manipulators": [
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": [
                                            "^org.gnu.Emacs",
                                            "^com.apple.Teamviewer”
                                        ],
                                        "type": "frontmost_application_if"
                                    }

Try this. I built it from the thread you linked and by looking over the examples at the KE-complex_modifications GitHub repo. I don’t have KE installed so I couldn’t actually test it.

Best to keep each configuration in separate files and out of your main global config file. So save this into ~/.config/karabiner/assets/complex_modifications/teamviewer-swap-cmd-ctrl.json

Go into Karabiner Preferences > Complex Modifications > Add rule then enable “Swap Left Control/Command only in Teamviewer”

{
  "title": "Swap Left Control/Command only in Teamviewer",
  "rules": [
    {
      "description": "Left Command to Control",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "left_command"
          },
          "to": [
            {
              "key_code": "left_control"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com.teamviewer.TeamViewer"
              ]
            }
          ]
        }
      ]
    },
    {
      "description": "Left Control to Command",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "left_control"
          },
          "to": [
            {
              "key_code": "left_command"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com.teamviewer.TeamViewer"
              ]
            }
          ]
        }
      ]
    }
  ]
}
2 Likes

@Onan Wow!!! That works perfectly. Exactly what I was looking for! Thanks a million. NOw I can control my music program from my laptop and not have to adjust anything on the keyboard. Thank you thank you! Thanks to everyone for their help.