Mapping different command keys to different layouts in macos
Alexander Solovyov in Switching languages with Karabiner:
I discovered that Karabiner has an excellent way to switch inputs directly, without needing Hammerspoon! So now you can go to where complex modifications are shared and just install it in your Karabiner. Easy-peasy.
Here’s the thing: Writing code if easier with a language layout in your keyboard –that is, your keyboard is mapped as if it were an English keyboard– but sometimes you need other layouts as the native to your first language for example. As you need both, you need a way to change layouts but macOS only let’s you change between your available ones. It doesn’t let you choose which one you want to use, which makes it uncomfortable and using the wrong one all the time.
This has bothered me since the beginning, but it was not until now that I put the effort to not stop until I finally have a solution, only to discover that Karabiner –the software to map keys to whatever you need– can natively switch the keyboard layout! This is perfect, because I’m already a user and I don’t want more utilities to launch on login. The magic lines are here:
{
"description": "Left CMD alone -> Spanish, held -> CMD",
"from": { "key_code": "left_command" },
"to": [{ "key_code": "left_command" }],
"to_if_alone": [{ "select_input_source": { "language": "es" } }],
"type": "basic"
},
{
"description": "Right CMD alone -> English, held -> right command",
"from": { "key_code": "right_command" },
"to": [{ "key_code": "right_command" }],
"to_if_alone": [{ "select_input_source": { "language": "en" } }],
"type": "basic"
},
This lines in your karabiner.json
–or as Alexander says, your complex modifications folder– map the command keys to each keyboard layout.
It’s not perfect: I still have to wire my brain to “select” the language before starting to write. But this removes the doubt of Am I in the correct layout feeling whenever I’m about to write something.