Karabiner Mac Key mapping tool Notes

depth
2

References

Notes

  • Karabiner is the key mapping tool in mac.

  • The key mapping script syntax has changed. The "rules" top level element is gone. The "manipulators" and "description" appear at the top.

  • By default mouse buttons are not intercepted by karabiner (unlike keyboard), you need to enable it.

  • CapsLock key handling is special. It is better to leave caps key alone without mapping.

  • You need a hyper key without any delay handling. It will immediately generate Shift+Cmd+Ctrl+Opt combination.

  • You need another Cmd Mode Key (with large timeout like 1 or few secs) that will make the entire keyboard available for generating further keys.

  • Recommended Hyper Key configuration for me:

    • Application Key: Application Mode Key Mappings:
      • a-g and q-r ==> F16-24
      • App + Shift ==> Esc
      • App + c, v, b ==> Left Click, Right Click, Middle Click
    • Right Control Key: Hyper Key and More:
      • Right Control ==> Shift+Ctrl+Cmd+Opt (HyperKey)
      • Right Control (Click) ==> Shift+Ctrl+Cmd+Opt (HyperKey) + Space (Raycast Toggle)
      • Shift + Ctrl ==> CMD
      • App + Ctrl ==> Ctrl (Pass through Ctrl in App mode)
      • Left CMD (Click) ==> CMD + Space (Spotlight)
      • Left Opt (Click) ==> Hyper + Space (Raycast Toggle)
    • CapsLock Key: Hyper Key and More.
      • CapsLock ==> HyperKey
      • App + CapsLock ==> CapsLock (Pass Through)
    • Right Opt Key: Hyper Key2 and more.
      • Right Opt ==> Hyper Key2 (Ctrl Cmd Opt)
      • Right Opt Click ==> Left Mouse Click
      • App + Opt ==> Opt (Pass through Opt in App Mode)
  • Chrome shortcuts:

    • F1, F2 ==> Cmd mode on/off
    • button5 ==> Toggle Cmd Mode
    • Shift+btn4 ==> button5 (Back - Forward)
    • f d r e ==> Scroll up and down
    • z x ==> Prev/Next Tab
    • c v ==> Home, End
    • q ==> Close Tab
    • t ==> button 1
  • hammerspoon shortcuts:

    • Alt + Space ==> Hyper-Key

    • Hyper2 + . ==> Show local windows for current application

    • Hyper + u ==> Minimize Window (mapped to Cmd M)

    • Hyper + o ==> Restore last minimized Window

    • Multiple keys mapped to left mouse click:
      Hyper + i Hyper + ;

    • Hyper + d ==> Scroll up

    • Hyper + s ==> Scroll down

  • Global Karabiner Mappings:

    Hyper + hjkl ==> Arrow keys Hyper + nm,. ==> Mouse Move Left, Down, Up, right Hyper + / ==> Left click

  • Raycast shortcuts:

    • Hyper + T ==> iTerm
    • Hyper + G ==> Chrome
    • Hyper + F ==> Finder
    • Hyper + W ==> sw Switch Windows
    • Hyper + R ==> rs Raycast Settings Window
    • Hyper2 C ==> gcm Chrome - Munian
    • Hyper2 G ==> gct Chrome - Thava
  • System Shortcuts:

    • CMD - Tab ==> Cycle through open Apps
    • CMD - ` ==> Cycle through open windows
    • CMD M ==> Minimize
    • CMD W ==> Close Window
  • Raycast Hyper shortcut commands:

    • menu ==> Search menu items
    • mt ==> multi-translate
    • rs ==> Raycast Settings Extensions

Manipulators and Order Of Processing

.
. Rule1:  input --> manipulator1 --> manipulator2 --> ...
.                       |
.                       +--> Match Found, To key generated -> Rule1 Halt.
.
. Rule2:   Rule1 Output --> Rule2 Manipulators.
.
. To Halt Processing: "to": [ { "key_code": "b" }, { "halt": true } ]
.
  • Every config.json file is single Rule. Order defines Rules Priority.
  • You should specify the manipulators with "conditions" first, so that they are given more preference to match first effectively disregarding the fallback match.

Mapping Parameters

  • type: Required "basic" is specified
  • from: Required The name of key code, consumer key code or pointing button
  • to Optional Events which are sent when you press from key
  • to_if_alone Optional Events which are sent when you press from key alone
  • to_if_held_down Optional Events which are sent when you hold down from key
  • to_after_key_up Optional Events which are sent after you release from key
  • to_delayed_action Optional Events sent after 500 milliseconds after press from key
  • conditions Optional Manipulator is applied only if condition is matched
  • parameters Optional Override parameters such as to_if_alone_timeout_milliseconds
  • description Optional A human-readable comment

Parameter Examples: aaaaaaaaaaaaaaaa

How to use simultaneous keys mapping

{
    "description": "Pressing the a,s,d keys simultaneously launches Mission Control",
    "manipulators": [
        {
            "type": "basic",
            "from": {
                "simultaneous": [
                    { "key_code": "a" },
                    { "key_code": "s" },
                    { "key_code": "d" }
                ],
                "modifiers": { "optional": ["any"] }
            },
            "to": [{ "apple_vendor_keyboard_key_code": "mission_control" }]
        }
    ]
}

How to map button 4 click to another keyboard key?

{
    "title": "Map Button 4 to F Key",
    "rules": [
        {
            "description": "Map Button 4 to F Key",
            "manipulators": [
                {
                    "type": "basic",
                    "from": {
                        "pointing_button": "button4"
                    },
                    "to": [
                        {
                            "key_code": "f"
                            "modifiers": ["left_control"]
                        }
                    ]

                }
            ]
        }
    ]
}

How to use single key to toggle command mode ?

{
    "title": "Toggle Command Mode with F1",
    "rules": [
        {
            "description": "Toggle Command Mode with F1",
            "manipulators": [
                {
                    // When command_mode is 0, pressing F1 activates command mode
                    "type": "basic",
                    "from": {
                        "key_code": "f1"
                    },
                    "to_if_alone": [
                        {
                            // Set command_mode to 1
                            "set_variable": {
                                "name": "command_mode",
                                "value": 1
                            }
                        },
                        {
                            // Show notification for activation
                            "shell_command": "/usr/bin/osascript -e 'display notification \"Command Mode Activated\" with title \"Karabiner\"'"
                        }
                    ],
                    // Condition checks if command_mode is currently 0
                    "conditions": [
                        {
                            // Only execute if command_mode is 0
                            "type": "variable_if",
                            "name": "command_mode",
                            "value": 0
                        }
                    ]
                },
                {
                    // When command_mode is 1, pressing F1 deactivates command mode
                    "type": "basic",
                    "from": {
                        "key_code": "f1"
                    },
                    // Action when pressed alone while in command mode
                    "to_if_alone": [
                        {
                            // Set command_mode to 0
                            "set_variable": {
                                "name": "command_mode",
                                "value": 0
                            }
                        },
                        {
                            // Show notification for deactivation
                            "shell_command": "/usr/bin/osascript -e 'display notification \"Command Mode Deactivated\" with title \"Karabiner\"'"
                        }
                    ],
                    // Condition checks if command_mode is currently 1
                    // This block should be executed when command_mode is active (1)
                    "conditions": [
                        {
                            // Only execute if command_mode is 1
                            "type": "variable_if",
                            "name": "command_mode",
                            "value": 1
                        }
                    ]
                }
            ]
        }
    ]
}

How to map mouse button click to another action with modifiers ?

...
{
    "from": {
        "pointing_button": "button4",
        "modifiers": { 
          "mandatory": [ "left_shift"],
          "optional": ["any"]
        }
    },
    "to": [
        {
           "pointing_button": "button5"
        }
    ],
    "type": "basic",
    "conditions": [
        {
            "bundle_identifiers": [
                "^com.google.Chrome$"
            ],
            "type": "frontmost_application_if"
        }
    ]
}

How to use Application Key with Delayed action to simulate F15-F20 ?

  • How delayed action works ?

Parameters for basic.to_delayed_action:

  • "to_if_invoked" (No other key pressed in 0.5 secs)
  • "to_if_canceled":
  • "basic.to_delayed_action_delay_milliseconds": 500
{
  "type": "basic.to_delayed_action",
  "from": {
    "key_code": "application",
    "modifiers": ["left_shift"]
  },
  "to_if_invoked": [
    { "key_code": "f16" }
  ],
  "to_if_canceled": [
    { "key_code": "f17" }
  ],
  "parameters": {
    "basic.to_delayed_action_delay_milliseconds": 500
  }
}

{
  "description": "Use Application key as a chord to trigger F16–F20 with 200ms delay",
  "manipulators": [
    {
      "type": "basic",
      "from": {
        "key_code": "application"
      },
      "to": [
        {
          "set_variable": {
            "name": "application_key_active",
            "value": 1
          }
        }
      ],
      "to_delayed_action": {
        "to_if_invoked": [
          {
            "set_variable": {
              "name": "application_key_active",
              "value": 0
            }
          }
        ],
        "to_if_canceled": [],
        "delay_milliseconds": 200
      }
    },
    {
      "type": "basic",
      "conditions": [
        {
          "type": "variable_if",
          "name": "application_key_active",
          "value": 1
        }
      ],
      "from": {
        "key_code": "q"
      },
      "to": [
        {
          "key_code": "f16"
        }
      ]
    },

    {
      "type": "basic",
      "conditions": [
        {
          "type": "variable_if",
          "name": "application_key_active",
          "value": 1
        }
      ],
      "from": {
        "key_code": "w"
      },
      "to": [
        {
          "key_code": "f17"
        }
      ]
    },
    {
      "type": "basic",
      "conditions": [
        {
          "type": "variable_if",
          "name": "application_key_active",
          "value": 1
        }
      ],
      "from": {
        "key_code": "e"
      },
      "to": [
        {
          "key_code": "f18"
        }
      ]
    },
    {
      "type": "basic",
      "conditions": [
        {
          "type": "variable_if",
          "name": "application_key_active",
          "value": 1
        }
      ],
      "from": {
        "key_code": "r"
      },
      "to": [
        {
          "key_code": "f19"
        }
      ]
    },
    {
      "type": "basic",
      "conditions": [
        {
          "type": "variable_if",
          "name": "application_key_active",
          "value": 1
        }
      ],
      "from": {
        "key_code": "t"
      },
      "to": [
        {
          "key_code": "f20"
        }
      ]
    }
  ]
}