Aa の ほーむぺーじ。

キーボードを拡張する Karabiner-Elements

  • #macOS
  • #Karabiner-Elements

キーボードを拡張する

キーボードを叩く時、よく観察すると2つのイベントが発生しています。

  1. キーを押す
  2. キーを離す

そして、この動作の片方だけで代替できるキーが複数存在します。

karabiner elements — tap and hold keyboard diagram

🟥:長押しのみ / 🟩:単体押しのみ

つまり、以下のことが可能です。

🟥:単体押しを割り当てられる

例えば、⌘キーについて

⌘キーが押される → 他のキーは押されたか?

  1. 押された:標準的な「⌘」
  2. 何もせずキーが離れた:「新しいアクション」

USキーボードで**⌘キーに英数/かな**を割り当てるのが有名

🟩:長押しを割り当てられる

例えば、⇪キーについて

  1. 単体押し:標準的な「⇪」
  2. 長押し/他のキーと同時に押す:「新しいアクション」

⇪キーにHyperキーを割り当てるのが有名

Karabiner-Elements とは?

karabiner elements — app icon

Karabiner-Elementsは、macOS向けのキーボードカスタマイズツールです。

もう分かりますね?

このアプリを使うと既存のキーボード操作を汚染せずに大量のショートカットを追加できます。

ショートカットのアイデア

設定方法は後ほど紹介します。コピペで使用できます。

⌘ に 英数/かな

{
  "description": "⌘ -> 英数/かな",
  "manipulators": [
    {
      "from": { "key_code": "left_command" },
      "to": { "key_code": "left_command" },
      "to_if_alone": [{ "key_code": "japanese_eisuu" }],
      "type": "basic"
    },
    {
      "from": { "key_code": "right_command" },
      "to": { "key_code": "right_command" },
      "to_if_alone": [{ "key_code": "japanese_kana" }],
      "type": "basic"
    }
  ]
}

⇪をHyperキーにする

{
  "description": "⇪ --> Hyper",
  "manipulators": [
    {
      "from": { "key_code": "caps_lock" },
      "to": [
        {
          "key_code": "right_command",
          "lazy": true,
          "modifiers": ["right_control", "right_option", "right_shift"]
        }
      ],
      "to_if_alone": [{ "key_code": "caps_lock" }],
      "type": "basic"
    }
  ]
}

Escキー 長押しで強制終了

{
  "description": "␛  -->  ⌥⌘␛",
  "manipulators": [
    {
      "from": {
        "key_code": "escape",
        "modifiers": { "optional": ["any"] }
      },
      "parameters": {
        "basic.to_if_alone_timeout_milliseconds": 250,
        "basic.to_if_held_down_threshold_milliseconds": 250
      },
      "to_if_alone": [{ "key_code": "escape" }],
      "to_if_held_down": [
        {
          "key_code": "escape",
          "lazy": true,
          "modifiers": ["left_option", "left_command"]
        }
      ],
      "type": "basic"
    }
  ]
}

R⌘ ダブルタップ で スクショ

{
  "description": "R⌘ x2  -->  ^⇧⌘4",
  "manipulators": [
    {
      "conditions": [
        {
          "name": "r_cmd_tapped",
          "type": "variable_if",
          "value": 1
        }
      ],
      "from": { "key_code": "right_command" },
      "parameters": { "basic.to_if_alone_timeout_milliseconds": 150 },
      "to": [
        { "key_code": "right_command" },
        {
          "set_variable": {
            "name": "r_cmd_tapped",
            "value": 0
          }
        }
      ],
      "to_if_alone": [
        {
          "key_code": "4",
          "modifiers": ["left_control", "left_command", "left_shift"]
        },
        {
          "set_variable": {
            "name": "r_cmd_tapped",
            "value": 0
          }
        }
      ],
      "type": "basic"
    },
    {
      "from": { "key_code": "right_command" },
      "parameters": {
        "basic.to_delayed_action_delay_milliseconds": 200,
        "basic.to_if_alone_timeout_milliseconds": 150
      },
      "to": [{ "key_code": "right_command" }],
      "to_delayed_action": {
        "to_if_canceled": [
          {
            "set_variable": {
              "name": "r_cmd_tapped",
              "value": 0
            }
          }
        ],
        "to_if_invoked": [
          {
            "set_variable": {
              "name": "r_cmd_tapped",
              "value": 0
            }
          }
        ]
      },
      "to_if_alone": [
        {
          "set_variable": {
            "name": "r_cmd_tapped",
            "value": 1
          }
        }
      ],
      "type": "basic"
    }
  ]
}

設定方法

1. サイドバーから Complex Modifications を選択

karabiner elements — complex modifications settings

Karabiner-Elements.app > Complex Modifications

2. 画面上部の Add your own rule をクリック。

karabiner elements — add custom rule editor

Complex Modifications > Add your own rule

3. 例を削除し、ルールを貼り付け。

karabiner elements — custom rule json

4. Save を押して保存。

karabiner elements — enabled custom rule

よく使うトリガーを紹介

このアプリは、「キーを離した時」「長押し」「ダブルタップ」「条件分岐」など細かなカスタマイズができます。少し例を紹介します。

キーが押された時

to: []

キーが単体で入力され、単体で離された時

to_if_alone: []

キーが長押しされた時

to_if_held_down: []

個人的に便利だと思うショートカット

L⌘ で スクショ

{
  "description": "L⌘ -> ⇧⌘5",
  "manipulators": [
    {
      "from": { "key_code": "left_command" },
      "parameters": { "basic.to_if_alone_timeout_milliseconds": 200 },
      "to": [{ "key_code": "left_command" }],
      "to_if_alone": [
        {
          "key_code": "5",
          "modifiers": ["left_command", "left_shift"]
        }
      ],
      "type": "basic"
    }
  ]
}

R⌘ ダブルタップ で スクショ

{
  "description": "R⌘ x2  -->  ^⇧⌘4",
  "enabled": false,
  "manipulators": [
    {
      "conditions": [
        {
          "name": "r_cmd_tapped",
          "type": "variable_if",
          "value": 1
        }
      ],
      "from": { "key_code": "right_command" },
      "parameters": { "basic.to_if_alone_timeout_milliseconds": 150 },
      "to": [
        { "key_code": "right_command" },
        {
          "set_variable": {
            "name": "r_cmd_tapped",
            "value": 0
          }
        }
      ],
      "to_if_alone": [
        {
          "key_code": "4",
          "modifiers": ["left_control", "left_command", "left_shift"]
        },
        {
          "set_variable": {
            "name": "r_cmd_tapped",
            "value": 0
          }
        }
      ],
      "type": "basic"
    },
    {
      "from": { "key_code": "right_command" },
      "parameters": {
        "basic.to_delayed_action_delay_milliseconds": 200,
        "basic.to_if_alone_timeout_milliseconds": 150
      },
      "to": [{ "key_code": "right_command" }],
      "to_delayed_action": {
        "to_if_canceled": [
          {
            "set_variable": {
              "name": "r_cmd_tapped",
              "value": 0
            }
          }
        ],
        "to_if_invoked": [
          {
            "set_variable": {
              "name": "r_cmd_tapped",
              "value": 0
            }
          }
        ]
      },
      "to_if_alone": [
        {
          "set_variable": {
            "name": "r_cmd_tapped",
            "value": 1
          }
        }
      ],
      "type": "basic"
    }
  ]
}

英数 を Hyperキー に

{
  "description": "英数 -> L✦",
  "manipulators": [
    {
      "from": { "key_code": "japanese_eisuu" },
      "to": [
        {
          "key_code": "left_command",
          "lazy": true,
          "modifiers": ["left_control", "left_option", "left_shift"]
        }
      ],
      "to_if_alone": [{ "key_code": "japanese_eisuu" }],
      "type": "basic"
    }
  ]
}

かな を Hyperキー に

{
  "description": "かな  -->  R✦",
  "manipulators": [
    {
      "from": { "key_code": "japanese_kana" },
      "to": [
        {
          "key_code": "right_command",
          "lazy": true,
          "modifiers": ["right_control", "right_option", "right_shift"]
        }
      ],
      "to_if_alone": [{ "key_code": "japanese_kana" }],
      "type": "basic"
    }
  ]
}

^FBPN に →←↑↓ を焼き込み

{
  "description": "^fbpn --> →←↑↓",
  "manipulators": [
    {
      "from": {
        "key_code": "f",
        "modifiers": {
          "mandatory": ["control"],
          "optional": ["shift"]
        }
      },
      "to": [{ "key_code": "right_arrow" }],
      "type": "basic"
    },
    {
      "from": {
        "key_code": "b",
        "modifiers": {
          "mandatory": ["control"],
          "optional": ["shift"]
        }
      },
      "to": [{ "key_code": "left_arrow" }],
      "type": "basic"
    },
    {
      "from": {
        "key_code": "p",
        "modifiers": {
          "mandatory": ["control"],
          "optional": ["shift"]
        }
      },
      "to": [{ "key_code": "up_arrow" }],
      "type": "basic"
    },
    {
      "from": {
        "key_code": "n",
        "modifiers": {
          "mandatory": ["control"],
          "optional": ["shift"]
        }
      },
      "to": [{ "key_code": "down_arrow" }],
      "type": "basic"
    }
  ]
}

^HD に ⌫⌦ を焼き込み(ターミナルで無効化)

{
  "description": "^hd --> ⌫⌦",
  "manipulators": [
    {
      "from": {
        "key_code": "h",
        "modifiers": { "mandatory": ["control"] }
      },
      "to": [{ "key_code": "delete_or_backspace" }],
      "type": "basic"
    },
    {
      "conditions": [
        {
          "bundle_identifiers": ["^com\\.apple\\.Terminal$", "^com\\.mitchellh\\.ghostty$"],
          "type": "frontmost_application_unless"
        }
      ],
      "from": {
        "key_code": "d",
        "modifiers": { "mandatory": ["control"] }
      },
      "to": [{ "key_code": "delete_forward" }],
      "type": "basic"
    }
  ]
}

⇪ と fn を融合

{
  "description": "⇪  -->  fn",
  "manipulators": [
    {
      "from": {
        "key_code": "caps_lock",
        "modifiers": { "optional": ["any"] }
      },
      "to": [{ "key_code": "fn" }],
      "to_if_alone": [
        {
          "hold_down_milliseconds": 100,
          "key_code": "caps_lock"
        }
      ],
      "type": "basic"
    }
  ]
}

https://forms.gle/PH8XcGR3NoZEqAyL8