Mac で Karabiner-Elements を使ってファンクションキーにショートカットキー Ctrl + C を割り当てる方法

ロジクール マウス MX Master 3 を愛用しているのですが、macOS Monterey バージョン 12.1 にしてから、ホイールの挙動がおかしいので、純正マウスに戻すことにしました。

但し、アプリ毎に MX Master 3 の Back/forward ボタンにキーを割り当てています。

特に ターミナルで Back/forward ボタンにショートカットキー Ctrl + C を割り当てていました。

これがないと不便なので、カスタムキーボードなどを導入しようかと思ったのですが、キーボードのファンクションキーに割り当ててみることにしました。

2022/03/21(Mon) 12:38:36
Macでアプリケーションのキーボードショートカットを作成する - Apple サポート (日本)
https://support.apple.com/ja-jp/guide/mac-help/mchlp2271/12.0/mac/12.0

macOS 純正の機能では、アプリケーションのメニュータイトルをショートカットキーに割り当てることができます。

欲しい機能は、ファンクションキー f1 を押すと Ctrl + C を押したことになる動作です。即ち、特定のキーを押すと違うキー操作を実行するようにしたいのですが、アプリケーションのメニュータイトルをショートカットキーに割り当てることでは実現できません。

ちょうど US 配列キーボードを使っているので、Karabiner-Elements にお世話になっていました。

2022/03/21(Mon) 12:37:23
Karabiner-Elements
https://karabiner-elements.pqrs.org/
2022/03/21(Mon) 12:40:22
Use more complex rules | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/manual/configuration/configure-complex-modifications/

2022/03/21(Mon) 12:40:59
Karabiner-Elements complex_modifications rules
https://ke-complex-modifications.pqrs.org/

Table of Contents
	▼International (Language Specific)
		▼For Japanese (日本語環境向けの設定) (rev 6)
			▼コマンドキーを単体で押したときに、英数・かなキーを送信する。(左コマンドキーは英数、右コマンドキーはかな) (rev 3)

2022/03/21(Mon) 12:41:31
JSON ソース
https://ke-complex-modifications.pqrs.org/json/japanese.json

普段は、上記 Complex Modifications サイトから日本語キーバインド設定ファイル(左コマンドキーは英数、右コマンドキーはかな)をダウンロードして、英数が左コマンドキーに設定されている機能を左オプションキーに変更して使っています。

ファンクションキーの設定は、 /アプリケーション/Karabiner-Elements.app の Function keys タブで変更できますが、カスタマイズしたいので、下記 Complex Modifications サイトから

2022/03/21(Mon) 12:43:29
Karabiner-Elements complex_modifications rules
https://ke-complex-modifications.pqrs.org/

Table of Contents
	▼Modifier Keys
		▼Change Command_R Key (Right Command)
			▼Post Escape if Command_R is pressed alone.
2022/03/21(Mon) 12:44:16
JSON ソース
https://ke-complex-modifications.pqrs.org/json/change_command_l.json

Post Escape if Command_R is pressed alone. を雛形にして、ファンクションキー f1 を入力すると Ctrl + C に変更する設定ファイルを作成してみました。

2022/03/21(Mon) 13:38:48
KeyBinding.zip
https://kyoshiaki.sakura.ne.jp/blog/KeyBinding.zip

良かったら、上記リンクからダウンロードして利用してください。

使用方法は、ダウンロードした KeyBinding.zip ファイルを Finder で展開し、下記の作成されたファイルから

KeyBinding
|--ReadMe.txt
|--control_break_keybindings.json
|--bathyscaphe_and_terminal_key_bindings.json
|--japanese_option_eisuu_key_bindings.json
|--open_complex_modifications.sh
|--open_complex_modifications.scpt
|--Mac で Karabiner-Elements を使ってファンクションキーにショートカットキー Ctrl + C を割り当てる方法 – KOYAMA Yoshiaki のブログ – WordPres….webloc
|--Mac で Karabiner-Elements を使ってファンクションキーにショートカットキー Ctrl + C を割り当てる方法 – KOYAMA Yoshiaki のブログ – WordPres.url
|--LICENSE.txt

自分が使用したい拡張子が json の設定ファイルを $HOME/.config/karabiner/assets/complex_modifications/ フォルダにコピーしてください。

ターミナルで KeyBinding.zip を展開したいフォルダに移動し、

2022/03/21(Mon) 13:40:59
$ cd KeyBinding
$ ./open_complex_modifications.sh

上記のように open_complex_modifications.sh を実行すると $HOME/.config/karabiner/assets/complex_modifications/ フォルダをFinder で開くことができます。

また、KeyBinding.zip ファイルを展開して作成された KeyBinding フォルダに含まれるアップルスクリプト open_complex_modifications.scpt ファイルをダブルクリックするとスクリプトエディタが開くので、右上の実行ボタン (▶ アイコン) をクリックすると同じく $HOME/.config/karabiner/assets/complex_modifications/ フォルダを Finder で表示できます。

ファンクションキー f1 にショートカットキーCtrl + C を割り当てたいなら、control_break_keybindings.json ファイルを $HOME/.config/karabiner/assets/complex_modifications/ フォルダにコピーしてください。

2022/03/21(Mon) 13:41:48
Use more complex rules | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/manual/configuration/configure-complex-modifications/

後は、上記サイトのように /アプリケーション/Karabiner-Elements.app を起動し、下図のように Complex Modifications タブ > Rules タブ > Add rule ボタンをクリックしてください。

Add rule

下図から “無条件に f1 キーを押すと Control + C を送信する。(rev 0)” の ‘+ Enable’ ボタンをクリックして有効にしてください。

Enable
“無条件に f1 キーを押すと Control + C を送信する。(rev 0)”

上図のように表示されれば、これで f1 キーの入力を Ctrl + C に変更できます。

しかし、自分の好みのショートカットキーを登録したくなると思います。

2022/03/21(Mon) 13:46:25
External JSON generators | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/external-json-generators/

2022/03/21(Mon) 13:46:50
yqrashawn/GokuRakuJoudo: config karabiner with ease
https://github.com/yqrashawn/GokuRakuJoudo#usage

2022/03/21(Mon) 13:47:16
Goku は、拡張子 clj より Closure で作られている。
GokuRakuJoudo/tutorial.md at master · yqrashawn/GokuRakuJoudo
https://github.com/yqrashawn/GokuRakuJoudo/blob/master/tutorial.md

上記ドキュメントを読むと自分で設定ファイルを作るのは、敷居が高いと思うかもしれませんが、

control_break_keybindings.json

上記ファイルのように既存の JSON 形式 設定ファイルのキーコード (key_code) や修飾キー (modifiers) の文字列を変更すれば簡単に実現できます。

2022/03/21(Mon) 13:49:28
Visual Studio Code - Code Editing. Redefined
https://code.visualstudio.com/

2022/03/21(Mon) 13:49:57
JSON editing in Visual Studio Code
https://code.visualstudio.com/docs/languages/json

Visual Studio Code で JSON 形式 設定ファイルを開けば、構文チェックもしてくれます。

設定するキー文字列も、 /アプリケーション/Karabiner-EventViewer.app を起動し、確認したいキーを押せば表示してくれます。

2022/03/21(Mon) 13:51:08
快適キーボード操作のためのキーカスタマイズ ~Mac編~ | さくらのナレッジ
https://knowledge.sakura.ad.jp/23355/

上記サイトのキーバインド設定 JSON ファイルの解説が一番参考になりました。
特に mandatory、optional の違いは、ここで理解しました。

mandatory は、キーの組み合わせを指定し 、この組み合わせが入力されたときに ”to” の処理を行う。
optional に指定したキーは、”to” の時にもキー入力が引き継がれます。

私自身、

2022/03/21(Mon) 13:52:33
Karabiner-Elements complex_modifications rules
https://ke-complex-modifications.pqrs.org/

Table of Contents
	▼Emulation Modes
		▼Emacs key bindings (rev 12)
			▼Emacs key bindings [C-x key strokes] (rev 2)

2022/03/21(Mon) 13:53:00
JSON ソース
https://ke-complex-modifications.pqrs.org/json/emacs_key_bindings.json

のソースを読んで、理解を深めました。変数を使ってショートキーを押した数を数えたり、特定のアプリだけ動作する方法を学びました。

最後に JSON 設定ファイルを作成するのに必要なドキュメントの箇所をまとめておきます。

目次

JSON 設定ファイル書式

2022/03/21(Mon) 13:56:11
complex_modifications manipulator definition | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/
"manipulators": [
    {
        "type": "basic",
        "from": {...},
        "to": [...],
        "to_if_alone": [...],
        "to_if_held_down": [...],
        "to_after_key_up": [...],
        "to_delayed_action": {
            "to_if_invoked": [...],
            "to_if_canceled": [...],
        },
        "conditions": [...],
        "parameters": {...},
        "description": "Optional description for human"
    },
    ...
]
Name	Required	Description
type				Required	"basic" is specified
from				Required	The name of key code, consumer key code or pointing button which you want to change
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 which are sent after 500 milliseconds at you press from key
conditions		Optional	Manipulator is applied only if condition is matched (e.g., the frontmost application)
parameters		Optional	Override parameters such as to_if_alone_timeout_milliseconds
description		Optional	A human-readable comment
2022/03/21(Mon) 13:57:49
to_if_alone | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/to-if-alone/

2022/03/21(Mon) 13:58:14
to_if_held_down | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/to-if-held-down/

2022/03/21(Mon) 13:58:35
from.any | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/from/any/

Conditions

2022/03/21(Mon) 13:59:55
Conditions | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/conditions/
Conditions
frontmost_application_if, frontmost_application_unless
device_if, device_unless
keyboard_type_if, keyboard_type_unless
input_source_if, input_source_unless
variable_if, variable_unless
event_changed_if, event_changed_unless
2022/03/21(Mon) 14:01:04
frontmost_application_if, frontmost_application_unless | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/conditions/frontmost-application/
frontmost_application_if, frontmost_application_unless
Change an event if/unless the frontmost application is the specified application.

{
    "type": "frontmost_application_if",
    "bundle_identifiers": [
        bundle identifier regex,
        bundle identifier regex,
        ...
    ],
    "file_paths": [
        file path regex,
        file path regex,
        ...
    ]
}
Name	Required	Description
type	Required	"frontmost_application_if" or "frontmost_application_unless"
bundle_identifiers	Optional	Bundle identifier regexs such as ["^com\\.apple\\.Terminal$", "^com\\.googlecode\\.iterm2$"]
file_paths	Optional	File path regexs such as ["/Finder$"]
description	Optional	A human-readable comment
Multiple

Event

2022/03/21(Mon) 14:02:38
from event definition | Karabiner-Elements
https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/from/
from event definition
{
    "from": {
        "key_code": "The name of key_code",
        "consumer_key_code": "The name of consumer_key_code",
        "pointing_button": "The name of pointing_button",
        "any": "key_code or consumer_key_code or pointing_button",

        "modifiers": {
            "mandatory": [
                modifier,
                modifier,
                ...
            ],
            "optional": [
                modifier,
                modifier,
                ...
            ]
        },

        "simultaneous": [
            {
                "key_code, consumer_key_code, pointing_button or any"
            },
            {
                "key_code, consumer_key_code, pointing_button or any"
            },
            ...
        ],
        "simultaneous_options": {
            "detect_key_down_uninterruptedly": false,
            "key_down_order": "A restriction of input events order",
            "key_up_order": "A restriction of input events order",
            "key_up_when": "When key_up events are posted",
            "to_after_key_up": [
                to event definition,
                to event definition,
                ...
            ]
        }
    }
}
Name	Required	Description
key_code				Optional	Key code which you want to change
consumer_key_code		Optional	Consumer key code (media key code) which you want to change
pointing_button		Optional	Pointing button name which you want to change
any					Optional	"any": "key_code", "any": "consumer_key_code" or "any": "pointing_button"
modifiers				Optional	Specify mandatory and optional modifiers (e.g., “change control-h to delete”)
simultaneous			Optional	Specify multiple events which are pressed simultaneously
simultaneous_options	Optional	Options for simultaneous

Note
key_code, consumer_key_code, pointing_button and any are exclusive.
You have to specify one of them.

Caution
Be careful using "pointing_button": "button1" and "any": "pointing_button".
You may lose the left click button and system will be unusable.

Bundle identifier の探し方

Finder で Bundle identifier を確認したいアプリケーション (例として ‘ターミナル.app ‘ を使って解説します。) を右クリック (Ctrl + 左クリック) し

メニュー

メニュー > パッケージの内容を表示 を選択

Contents

表示された Contents フォルダをダブルクリックして開く。

Info.plist

Finder 上で Info.plist ファイルをダブルクリックする。 Xcode をインストールしている場合、Xcode の property list editor で表示されます。

Xcode の property list editor

表示されたウインドウの Bundle identifier の横に表示された文字列 com.apple.Terminal が目的の値です。

Bundle identifier を raw で表示するには、

Raw Keys & Values

上図のように property list editor 内を右クリック (Ctrl + 左クリック) して、メニュー > Raw Keys & Values を選択すれば表示されます。

raw で表示された状態

詳細は、少し古いですが下記ページを参照してください。間違って値を変更しないように気を付けてください。

2022/03/21(Mon) 14:09:42
Edit property lists - Xcode Help
https://help.apple.com/xcode/mac/11.4/#/dev3f399a2a6

2022/03/21(Mon) 14:10:12
Xcode Help
https://help.apple.com/xcode/mac/11.4/

Twitter

Twitter のツイートを埋め込んでみた。

ブックマークレット

「Bookmarklet」

使い方

  1. ブラウザで上記リンクをブックマークバーにドラッグ・ドロップして登録します。
  2. ブラウザでタイトルと URL をコピーしたいサイトを開き、先程登録したブックマークを選ぶとクリップボードに日付、タイトル、URL がコピーされます。
  3. テキストエディタなどで挿入したい箇所にペーストしてください。
  4. このページを上記3番目のブックマークレットを使うと下記の通りになります。
2020/12/13(Sun) 16:56:22
ブックマークレット | KOYAMA Yoshiaki のブログ - WordPress 出張所
https://kyoshiaki.sakura.ne.jp/wp/?p=588

最近、WordPress を使い始めたので、必要にかられて作成してみました。

追記: 2020/12/19(Sat) 23:42:14
文字色と背景色を変更してみた。