のどあめ

ググってもなかなか出てこなかった情報を垂れ流すブログ。

(続)macOS SierraでKarabinerの「EISUU to Escape(入力ソースが日本語以外の場合のみ)」を実現する

Deprecated

もっと良い方法を見つけましたので、こちらを利用して下さい。

ykicisk.hatenablog.com

以下、元記事です。


以前の記事で、HammerspoonとKarabiner-Elementsを使って、 擬似的にKarabinerの「EISUU to Escape(入力ソースが日本語以外の場合のみ)」(下図)を 実現する方法を紹介しました。

f:id:ykicisk:20170916162357p:plain

macOS SierraでKarabinerの「EISUU to Escape(入力ソースが日本語以外の場合のみ)」を実現する - のどあめ

この度、Karabiner-ElementsのComplex Modificationsを使うことで、 Karabiner-Elementsのみで実現する方法を見つけたので紹介します。(しかもこちらのほうが安定する)

参考にしたページ

実現方法

まずは、この機能を実現するComplex Modificationsを作成します。

.config/karabiner/assets/complex_modifications/EisuuToEscapeInTerminal.json に以下のようなjsonファイルを配置します。

{
  "title": "Terminal等で入力ソースが日本語以外の場合に英数をEscapeに変更する",
  "rules": [
    {
      "description": "Terminal等で入力ソースが日本語以外の場合に英数をEscapeに変更する",
      "manipulators": [
        {
          "type": "basic",
          "from": { "key_code": "japanese_eisuu", "modifiers": { "optional": [ "any" ] } },
          "to": [
            { "key_code": "japanese_eisuu"},
            { "set_variable": {"name": "jp_input", "value": 0}}
          ],
          "conditions": [
            {
              "type": "variable_if",
              "name": "jp_input",
              "value": 1
            },
            { "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^org\\.vim\\.",
                "^com\\.googlecode\\.iterm2$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": { "key_code": "japanese_eisuu", "modifiers": { "optional": [ "any" ] } },
          "to": [ { "key_code": "escape"} ],
          "conditions": [
            {
              "type": "variable_if",
              "name": "jp_input",
              "value": 0
            },
            { "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^org\\.vim\\.",
                "^com\\.googlecode\\.iterm2$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": { "key_code": "japanese_kana", "modifiers": { "optional": [ "any" ] } },
          "to": [
            { "key_code": "japanese_kana"},
            { "set_variable": {"name": "jp", "value": 1}}
          ],
          "conditions": [ { "type": "frontmost_application_if",
            "bundle_identifiers": [
              "^com\\.apple\\.Terminal$",
              "^org\\.vim\\.",
              "^com\\.googlecode\\.iterm2$"
            ]
          }
          ]
        }
      ]
    }
  ]
}

次に、Karabiner-ElementsのPreferenceから、 「Complex Modifications」→「Add rule」→「Terminal等で入力ソースが日本語以外の場合に英数をEscapeに変更する」を有効にします。

f:id:ykicisk:20171129202029p:plain

以上で、擬似的に日本語入力でないときのみ英数キーがEscape変わります。

ザックリした仕組み

  • 「かな」と「英数」で、変数jp_inputに1, 0を代入します。
  • 変数jp_inputが0のときのみ「英数」を「Escape」に変更します。

これで、「EISUU to Escape(入力ソースが日本語以外の場合のみ)」が擬似的に実現できます。

まとめ

  • Karabiner-ElementsのComplex Modificationsを利用することで、 macOS SierraでKarabinerの「EISUU to Escape(入力ソースが日本語以外の場合のみ)」を実現しました。
  • また、Escapeに変更するアプリケーションを指定できるので、ブラウザで誤爆することもなくなりました(個人的には嬉しい)
  • 設定がおかしくなったときは「かな」「英数」を何度か押すとうまく動くと思います。