LoginSignup
1
1

More than 1 year has passed since last update.

Mac で特定のアプリ時のみ、ファンクションキー(F1~F12)をデフォルトにする

Last updated at Posted at 2022-03-18

こちらに先に記事を書かれた方がいらっしゃいました

TLTR

  • Karabiner で特定のアプリ(開発用エディタ)でのみ、Function Key をデフォルトに設定する方法。
    • ↑正確にいうと、F1 から F12 までのキーを標準のファンクションキーとして使用し、割り当てられた特有の機能を disable する。

[すべてのアプリで]ファンクションキーのデフォルトの動作を変更する

普通はこれでよいが、これではすべてが変わってしまう。

「fn」キーを押さなくても、最上段に並んだキーを標準的なファンクションキーとして使えるようにしたい場合は、以下の手順を実行してください。
Apple メニュー  >「システム環境設定」の順に選択します。
「キーボード」をクリックします。
「F1、F2 などのキーを標準のファンクションキーとして使用」を選択します。

解決策

  • Karabiner-Elements (v14) を入れて、任意のアプリの識別を調べて、Karabiner-Elements の設定を書き換える。
  1. Karabiner-Elements をインストール
  2. Karabiner-Elements の設定を開く
  3. Complex Modifications を開く
  4. + Add rule を開く
  5. Import more rules ... を開く
  6. 適当に「Function Keys in [VSCode , iterm2 等アプリ名]」と検索し、指定したアプリでのみファンクションキーがデフォルトになる拡張機能をインストールする(拡張機能は消えたり、変化したりするため、ここでは具体的に指定はしない。)
  7. Karabiner-Elements の設定から Misc を開く
  8. Open config folder ~/config/karabiner を開く
  9. JSON形式設定ファイル karabiner.json をエディタで開く
  10. アプリ名を書き換える
  11. 動作確認して終了

アプリ名の書き換え方

  1. Karabiner-EventView を起動し、指定したいアプリのBundle Identifier を調べる。(VSCode なら "com.microsoft.VSCode")
  2. 追加した拡張機能がBundle Identifierを指定している部分を探す。
  3. 指定しているアプリ名を書き換える。

こんな感じ

    "bundle_identifiers": ["com.microsoft.VSCode"],

面倒な人用

  • 適当な拡張機能を入れてkarabiner.jsonに以下をコピペすれば、VSCode でのみ、ファンクションキーをデフォルトにできる。
{
    "global": {
        "check_for_updates_on_startup": true,
        "show_in_menu_bar": true,
        "show_profile_name_in_menu_bar": false
    },
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {
                    "basic.simultaneous_threshold_milliseconds": 50,
                    "basic.to_delayed_action_delay_milliseconds": 500,
                    "basic.to_if_alone_timeout_milliseconds": 1000,
                    "basic.to_if_held_down_threshold_milliseconds": 500,
                    "mouse_motion_to_scroll.speed": 100
                },
                "rules": [
                    {
                        "description": "Quit application by holding command-q",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "q",
                                    "modifiers": {
                                        "mandatory": [
                                            "command"
                                        ],
                                        "optional": [
                                            "caps_lock"
                                        ]
                                    }
                                },
                                "to_if_held_down": [
                                    {
                                        "key_code": "q",
                                        "modifiers": [
                                            "left_command"
                                        ],
                                        "repeat": false
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },
                    {
                        "description": "Function keys work as fn keys (VSCode)",
                        "manipulators": [
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f1",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f1"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f2",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f2"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f3",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f3"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f4",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f4"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f5",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f5"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f6",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f6"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f7",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f7"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f8",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f8"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f9",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f9"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f10",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f10"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f11",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f11"
                                    }
                                ],
                                "type": "basic"
                            },
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": ["com.microsoft.VSCode"],
                                        "file_paths": [
                                            "ck2\\.app",
                                            "eu4\\.app"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f12",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "f12"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    }
                ]
            },
            "devices": [],
            "fn_function_keys": [
                {
                    "from": {
                        "key_code": "f1"
                    },
                    "to": [
                        {
                            "consumer_key_code": "display_brightness_decrement"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f2"
                    },
                    "to": [
                        {
                            "consumer_key_code": "display_brightness_increment"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f3"
                    },
                    "to": [
                        {
                            "apple_vendor_keyboard_key_code": "mission_control"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f4"
                    },
                    "to": [
                        {
                            "apple_vendor_keyboard_key_code": "spotlight"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f5"
                    },
                    "to": [
                        {
                            "consumer_key_code": "dictation"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f6"
                    },
                    "to": [
                        {
                            "key_code": "f6"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f7"
                    },
                    "to": [
                        {
                            "consumer_key_code": "rewind"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f8"
                    },
                    "to": [
                        {
                            "consumer_key_code": "play_or_pause"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f9"
                    },
                    "to": [
                        {
                            "consumer_key_code": "fast_forward"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f10"
                    },
                    "to": [
                        {
                            "consumer_key_code": "mute"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f11"
                    },
                    "to": [
                        {
                            "consumer_key_code": "volume_decrement"
                        }
                    ]
                },
                {
                    "from": {
                        "key_code": "f12"
                    },
                    "to": [
                        {
                            "consumer_key_code": "volume_increment"
                        }
                    ]
                }
            ],
            "name": "Default profile",
            "parameters": {
                "delay_milliseconds_before_open_device": 1000
            },
            "selected": true,
            "simple_modifications": [],
            "virtual_hid_keyboard": {
                "country_code": 0,
                "indicate_sticky_modifier_keys_state": true,
                "mouse_key_xy_scale": 100
            }
        }
    ]
}

実行環境

$ sw_vers                                                                                               
ProductName:    macOS
ProductVersion: 12.2.1

1
1
0

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
1
1