LoginSignup
14
9

More than 3 years have passed since last update.

Karabiner-Elementsを使って特定のアプリでショートカットキーを上書きする

Posted at

はじめに

Karabiner-Elementsのcomplex modificationsを使うと、特定のアプリでのみ有効になるショートカットキーを定義することができる。

ここではMS officeのショートカットキーを上書きする。
というのも、MS officeはいわゆるemacsキーバインドで使うキーにショートカットが割り当てられているのだが、emacsキーバインドに慣れているとctrl+n,ctrl+f,ctrl+hなどのキーを押した時に、新しい書類が作られたり、検索画面が出てきたり、ヘルプが出てきたりして、ストレスが尋常じゃない。

これを通常のemacsキーバインドに戻して、普通のmacアプリのような使い勝手を実現する。

手順

手順1: キーリマップを定義したJSONファイルの準備

以下のようなjsonファイルを~/.config/karabiner/assets/complex_modificationsディレクトリに配置する

emacs_in_msapps.json
{
  "title": "emacs keybind in MS apps",
  "rules":
  [
    {
      "description": "emacs keybind in skype & office",
      "manipulators": [
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.skype\\.skype$",
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "h",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "delete_or_backspace"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "d",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "delete_forward"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "f",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "right_arrow"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "b",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "left_arrow"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "p",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "up_arrow"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "n",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "down_arrow"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "e",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "end"
            }
          ],
          "type": "basic"
        },
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "a",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "key_code": "home"
            }
          ],
          "type": "basic"
        }
      ]
    }
  ]
}

ここでポイントとなるのは以下のキーのリマップの条件が有効になる条件を定義した部分。
特定のアプリが最前面に出てきている時だけ有効化している。

"conditions": [
            {
              "bundle_identifiers": [
                "^com\\.microsoft\\."
              ],
              "type": "frontmost_application_if"
            }
          ]

アプリ名を調べるには、Karabiner-EventViewerを起動して"Frontmost application"という画面のログをみる。
実際にそのアプリを全面に出すと、その時のログが残る。

image.png

手順2: Karabiner-Elementsで有効にする

JSONで定義を書いたら、そのキーリマップを有効にする。
こちらと全く同じ手順でできる。

参考

14
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9