Skip to content

Instantly share code, notes, and snippets.

@shripadk
Last active November 4, 2024 19:12
Show Gist options
  • Save shripadk/57cd564e8a7ce0efb31e03d3d4824f04 to your computer and use it in GitHub Desktop.
Save shripadk/57cd564e8a7ce0efb31e03d3d4824f04 to your computer and use it in GitHub Desktop.
Karabiner Complex Modification: Change F4 to Command + Space for Alfred

Karabiner — Change F4 to Command + Space for Alfred

This is useful for changing the default F4 key that is bound to Spotlight to launch Alfred in macOS Big Sur.

Setup:

  1. Make sure you have Karabiner installed.
  2. Open ~/.config/karabiner/karabiner.json.
  3. Copy the rules object from below snippet and add it within profiles.complex_modifications. The basic structure of your karabiner.json file should look something like this:
{
    "global": {
      ..........
    },
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {
                  ..........
                },
                "rules": [
                    {
                      "description": "F4 to CMD+SPACE",
                      "manipulators": [
                          ..........
  1. Pressing F4 should now launch Alfred instead of Spotlight!

Hope this helps!

{
"rules": [
{
"description": "F4 to CMD+SPACE",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "f4"
},
"to": [
{
"repeat": true,
"key_code": "spacebar",
"modifiers": [
"left_gui"
]
}
]
}
]
}
]
}
@gtdck
Copy link

gtdck commented Apr 24, 2023

Thank you for sharing this!

I couldn't figure out how to add this function to Karabiner by editing karabiner.json through the steps you provided. However, I'm unsure if that was because the steps provided are a bit unclear, or if my extremely limited knowledge of code is to blame.

After some unsuccessful tries of figuring out what code to put where, I found a perhaps simpler solution:
  1. Create a new JSON file in ~/.config/karabiner/assets/complex_modifications (I called it "F4_albert.json")
  2. Open with a text editor and copy paste all lines of code from the code snippet provided by shripadk (labeled "karabiner.json") in to your newly created JSON file.
  3. Save!
  4. Open "Karabiner-Elements Settings"
  5. Navigate to "Complex Modifications"
  6. Click "Add rule"
  7. Click "Enable" on the new rule named "F4 to CMD+SPACE"

Voilà.

The necessary lines of code are added to my karabiner.json automatically, and as a bonus, I can easily remove these lines/this function by disabling the rule in settings. I have no idea if this solution is better in any way, but I decided to leave this here in case any code illiterate people has the same struggles in the future :)

Edit: I believe that one advantage from my solution is that the changes made persists across future updates of Karabiner.

I tried to roll everything back to see if there was something wrong with your code as I noticed my updated karabiner.json had two elements which are not in the code you provided:
  • "repeat": true
  • "type": "basic"

After comparing the original, unchanged karabiner.json with my failed attempt and my successful attempt, I realised that my original mistake was not adding enough of the end brackets from your code. By copy pasting line 3 to 22 "between" the square brackets after "rules" (on line 19 in an original, unedited karabiner.json), it worked perfectly.

So no! Nothing wrong with the code. Just user error.

Again, thank you, shripadk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment