點燈坊

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

我的 Karabiner 基本設定

Sam Xiao's Avatar 2024-09-22

自從 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
  • F2Android Studio
  • F3WebP Converter
  • F4iTerm2
  • F5Sublime Text
  • F6FortLift
  • F7Typora
  • F8MOJi 辞書
  • F9ChatGPT
  • F10WebStorm
  • F11Gitfox
  • F12Firefox
  • ⌘ F1Decrease Volumn
  • ⌘ F2Increase Volumn
  • ⌘ F3Mission Control
  • ⌘ F4Zoom
  • ⌘ F5Capture Text by TextSnipper
  • ⌘ F6Capture Screen by CleanShot X
  • ⌘ F7Record Screen by CleanShot X
  • ⌘ F8Play/Pause
  • ⌘ F9Fast Forward
  • ⌘ F10Mute
  • ⌘ F11Decrease Volumn
  • ⌘ F12Increase Volumn

Karabinar Configuration

karabiner.json

{
  "global": {
    "show_in_menu_bar": false
  },
  "profiles": [
    {
      "complex_modifications": {
        "rules": [
          {
            "description": "F1 to Safari",
            "manipulators": [
              {
                "from": {
                  "key_code": "f1"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Safari.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F1 to Decrease Brightness",
            "manipulators": [
              {
                "from": {
                  "key_code": "f1",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "display_brightness_decrement"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F2 to Android Studio",
            "manipulators": [
              {
                "from": {
                  "key_code": "f2"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Users/oomusou/Applications/Android Studio.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F2 to Increase Brightness",
            "manipulators": [
              {
                "from": {
                  "key_code": "f2",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "display_brightness_increment"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F3 to Web Converter",
            "manipulators": [
              {
                "from": {
                  "key_code": "f3"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/WebP Converter.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F3 to Mission Control",
            "manipulators": [
              {
                "from": {
                  "key_code": "f3",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "apple_vendor_keyboard_key_code": "mission_control"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F4 to iTerm",
            "manipulators": [
              {
                "from": {
                  "key_code": "f4"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/iTerm.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F4 to Zoom",
            "manipulators": [
              {
                "from": {
                  "key_code": "f4",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "key_code": "8",
                    "modifiers": [
                      "command",
                      "option"
                    ]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F5 to Sublime Text",
            "manipulators": [
              {
                "from": {
                  "key_code": "f5"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Sublime Text.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F5 to Capture Text",
            "manipulators": [
              {
                "from": {
                  "key_code": "f5",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "key_code": "1",
                    "modifiers": [
                      "command",
                      "shift"
                    ]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F6 to ForkLift",
            "manipulators": [
              {
                "from": {
                  "key_code": "f6"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/setapp/ForkLift.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F6 to Capture Screen",
            "manipulators": [
              {
                "from": {
                  "key_code": "f6",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "key_code": "3",
                    "modifiers": [
                      "command",
                      "shift"
                    ]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F7 to Typora",
            "manipulators": [
              {
                "from": {
                  "key_code": "f7"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Typora.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F7 to Record Screen",
            "manipulators": [
              {
                "from": {
                  "key_code": "f7",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "key_code": "7",
                    "modifiers": [
                      "command",
                      "shift"
                    ]
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F8 to MOJisho",
            "manipulators": [
              {
                "from": {
                  "key_code": "f8"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/MOJisho.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F8 to Play/Pause",
            "manipulators": [
              {
                "from": {
                  "key_code": "f8",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "play_or_pause"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F9 to ChatGPT",
            "manipulators": [
              {
                "from": {
                  "key_code": "f9"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/ChatGPT.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F9 to Fast Forward",
            "manipulators": [
              {
                "from": {
                  "key_code": "f9",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "fast_forward"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F10 to WebStorm",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Users/oomusou/Applications/WebStorm.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F10 to Mute",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "mute"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F11 to Gitfox",
            "manipulators": [
              {
                "from": {
                  "key_code": "f11"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/setapp/Gitfox.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F11 to Decrease Volumn",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "volume_decrement"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "F12 to FireFox",
            "manipulators": [
              {
                "from": {
                  "key_code": "f12"
                },
                "to": [
                  {
                    "shell_command": "open -a '/Applications/Firefox.app'"
                  }
                ],
                "type": "basic"
              }
            ]
          },
          {
            "description": "Command + F12 to Increase Volumn",
            "manipulators": [
              {
                "from": {
                  "key_code": "f10",
                  "modifiers": {
                    "mandatory": [
                      "command"
                    ]
                  }
                },
                "to": [
                  {
                    "consumer_key_code": "volume_increment"
                  }
                ],
                "type": "basic"
              }
            ]
          }
        ]
      },
      "name": "Default profile",
      "selected": true,
      "virtual_hid_keyboard": {
        "country_code": 0,
        "keyboard_type_v2": "ansi"
      }
    }
  ]
}

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