Switching Command and Control Keys

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"
              ]
            }
          ]
        }
      ]
    }
  ]
}
4 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.

I have a VNC viewer application where I want to map cmd_L to ctrl_L. How can I do this?
If I look at the above example for Teamviewer, how do I do it for VNCViewer? Please help, as I am new to macOS