點燈坊

失くすものさえない今が強くなるチャンスよ

我的 Karabiner 基本設定

Sam Xiao's Avatar 2023-12-23

自從 M2 Macbook Air 重新將 Fn 鍵回歸取代 Touch Bar 後,如何善用 Fn 鍵就成為重要的課題,畢竟 macOS 不像 Windows 常常會使用到 Fn 鍵。我們可使用 Karabiner-Elements 重新定義 Fn 鍵取代 Touch Bar 切換 app,並保留 + Fn 執行原本的功能。

Version

Karabiner-Elements 14.12.0

Installation

$ brew install --cask karabiner-elements
  • 使用 Homebrew 安裝 Karabiner

Fn Keys

  • F1Safari
  • F2WebStorm
  • ⌥ + F2Android Studio
  • F3WebP Converter
  • F4iTerm2
  • F5Typora
  • F6FortLift
  • F7Sublime Text
  • F8MOJi 辞書
  • F9ChatGPT
  • F10Gitfox
  • F11Chrome
  • F12Firefox
  • ⌘+ F1Decrease Volumn
  • ⌘ + F2Increase Volumn
  • ⌘ + F3Mission Control
  • ⌘ + F4Zoom
  • ⌘ + F5Capture Text by TextSnipper
  • ⌘ + F6Capture Screen by CleanShot X
  • ⌘ + F7Rewind
  • ⌘ + F8Play / Pause
  • ⌘ + F9Foward
  • ⌘ + F10Mute
  • ⌘ + F11Decrease Volumn
  • ⌘ + F12Increase Volumn

Karabinar Configuration

karabiner.json

{
  "global": {
    "ask_for_confirmation_before_quitting": true,
    "check_for_updates_on_startup": true,
    "show_in_menu_bar": false,
    "show_profile_name_in_menu_bar": false,
    "unsafe_ui": 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": "Use F1 to Open Safari",
            "manipulators": [
              {
                "from": {
                  "key_code": "f1"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Safari.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F1 to Decrease Brightness",
            "manipulators": [
              {
                "from": {
                  "key_code": "f1",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "display_brightness_decrement"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F2 to Open WebStorm",
            "manipulators": [
              {
                "from": {
                  "key_code": "f2"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Users/oomusou/Applications/WebStorm.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Option + F2 to Open Android Studio",
            "manipulators": [
              {
                "from": {
                  "key_code": "f2",
                  "modifiers": {
                    "mandatory": ["option"]
                  }
                },
                "to": [
                  {
                    "shell_command": "open -a '/Users/oomusou/Applications/Android Studio.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F2 to Increase Brightness",
            "manipulators": [
              {
                "from": {
                  "key_code": "f2",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "display_brightness_increment"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F3 to Open Web Converter",
            "manipulators": [
              {
                "from": {
                  "key_code": "f3"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/WebP Converter.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F3 to Misson Control",
            "manipulators": [
              {
                "from": {
                  "key_code": "f3",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "apple_vendor_keyboard_key_code": "mission_control"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F4 to Open iTerm",
            "manipulators": [
              {
                "from": {
                  "key_code": "f4"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/iTerm.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F4 to Zoom",
            "manipulators": [
              {
                "from": {
                  "key_code": "f4",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "key_code": "8",
                    "modifiers": ["command", "option"]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F5 to Open Typora",
            "manipulators": [
              {
                "from": {
                  "key_code": "f5"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Typora.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F5 to Capture Text",
            "manipulators": [
              {
                "from": {
                  "key_code": "f5",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "key_code": "1",
                    "modifiers": ["command", "shift"]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F6 to Open ForkLift",
            "manipulators": [
              {
                "from": {
                  "key_code": "f6"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/setapp/ForkLift.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F6 to Capture Screen",
            "manipulators": [
              {
                "from": {
                  "key_code": "f6",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "key_code": "3",
                    "modifiers": ["command", "shift"]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F7 to Open Sublime Text",
            "manipulators": [
              {
                "from": {
                  "key_code": "f7"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Sublime Text.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F7 to Rewind",
            "manipulators": [
              {
                "from": {
                  "key_code": "f7",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "rewind"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F8 to Open MOJisho",
            "manipulators": [
              {
                "from": {
                  "key_code": "f8"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/MOJisho.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F8 to Play/Pause",
            "manipulators": [
              {
                "from": {
                  "key_code": "f8",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "play_or_pause"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F9 to Open ChatGPT",
            "manipulators": [
              {
                "from": {
                  "key_code": "f9"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/ChatGPT.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },          
          {
            "description": "Use Command + F9 to Fast Foward",
            "manipulators": [
              {
                "from": {
                  "key_code": "f9",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "fast_forward"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F10 to Open Gitfox",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/setapp/Gitfox.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F10 to Mute",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "mute"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F11 to Open Chrome",
            "manipulators": [
              {
                "from": {
                  "key_code": "f11"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Google Chrome.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F11 to Decrease Volumn",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "volume_decrement"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use F12 to Open FireFox",
            "manipulators": [
              {
                "from": {
                  "key_code": "f12"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Firefox.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Use Command + F12 to Increase Volumn",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10",
                  "modifiers": {
                    "mandatory": ["command"]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "volume_increment"
                  }
                ],
                "type": "basic"
              }
            ]
          }
        ]
      },
      "devices": [
        {
          "disable_built_in_keyboard_if_exists": false,
          "fn_function_keys": [],
          "identifiers": {
            "is_keyboard": true,
            "is_pointing_device": false,
            "product_id": 0,
            "vendor_id": 0
          },
          "ignore": false,
          "manipulate_caps_lock_led": true,
          "simple_modifications": [],
          "treat_as_built_in_keyboard": false
        },
        {
          "disable_built_in_keyboard_if_exists": false,
          "fn_function_keys": [],
          "identifiers": {
            "is_keyboard": false,
            "is_pointing_device": true,
            "product_id": 0,
            "vendor_id": 0
          },
          "ignore": true,
          "manipulate_caps_lock_led": false,
          "simple_modifications": [],
          "treat_as_built_in_keyboard": false
        }
      ],
      "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
      }
    }
  ]
}

karabiner.json 位於 /Users/oomosou/.config/karabiner 目錄下。

Shell Command

Line 19

"rules": [
  • rules:若要使用 Complex Modification,要統一寫在 rules

Line 20

{
  "description": "Use F1 to Open Safari",
  "manipulators": [
    {
      "from": {
        "key_code": "f1"
      },
      "to": [
        {
          "shell_command": "open -a '/Applications/Safari.app'"
        }
      ],
      "type": "basic"
    }
  ]
},
  • from.key_code : 設定 Fn
  • to.shell_command : 設定 Fn 要開啟的 app

Shortcut

Line 184

{
  "description": "Use Command + F5 to Capture Text",
  "manipulators": [
    {
      "from": {
        "key_code": "f5",
        "modifiers": {
          "mandatory": [
            "command"
          ]
        }
      },
      "to": [
        {
          "key_code": "1",
          "modifiers": [
            "command",
            "shift"
          ]
        }
      ],
      "type": "basic"
    }
  ]
},
  • manipulators.from.key_code:設定 Fn
  • manipulators.to.key_code:設定 shortcut 主鍵
  • manipulators.to.modifiers:設定 shortcut 的輔助鍵
  • manipulators.type:shortcut 一定要設定為 basic

System Function

Line 36

{
  "description": "Use Command + F1 to Decrease Brightness",
  "manipulators": [
    {
      "from": {
        "key_code": "f1",
        "modifiers": {
          "mandatory": [
            "command"
          ]
        }
      },
      "to": [
        {
          "consumer_key_code": "display_brightness_decrement"
        }
      ],
      "type": "basic"
    }
  ]
}
  • manipulators.from.key_code:設定 Fn
  • manipulators.from.modifiers.mandatory:設定 Fn 的 modifier key
  • manipulators.to.consumer_key_code:設定系統功能

Conclusion

  • 事實上 Karabiner-Elements 的功能非常複雜,本文只使用了最基本的重新定義 Fn
  • Fn 鍵若要執行 shell command 可使用 fn_function_keys ,亦可使用 Complex Modification,本文統一使用 Complex Modification
  • F6F7 因為很少用到原本功能,因此重新定義成 Capture Text 與 Sleep
  • F3F4 目前能維持原本的 Mission Control 的 Spotlight,若將來有其他需求,亦可率先選擇這兩個按鍵自行定義
  • Fn 鍵可搭配 後,則可同時保留 切換 app原本功能,若有更多的 app 要切換,可搭配 ,如此實用性已經可超越 Touch Bar