8

On my MacBook I have use all F1 F2 etc. keys as standard function keys option unchecked. I use FunctionFlip to map F3 from Expose to Spaces.

However I would like to use function keys as real function keys when working under virtualised environment like VirtualBox or VMware Fusion. Going to System > Preferences is not very convenient.

Do you now how to check use all F1 F2 etc. keys as standard function keys only when a specific application is active?

1

8 Answers 8

5

Use Karabiner

This application can provide the exact functionality the OP asked for. user495470's answer about KeyRemap4MacBook was helpful in pointing me in the right direction. However, it's outdated and no longer works as written because KeyRemap4MacBook has been updated. It is now called Karabiner. It looks to be well-maintained and is free and open source software (not the case with BetterTouchTool mentioned in another answer which requires payment after 45 days).

Setting it up

First download and install the Karabiner application. Once installed, in order to remap the function keys as standard function keys, you will need to add a "Complex-Modification" in preferences. There are several of these complex modifications prebuilt and downloadable from the Karabiner website, but you can also write your own in json.

Use the following code and place it in a file (named something like "functionkeys.json") in the ~/.config/karabiner/assets/complex_modifications folder. Once it is there, go to Karabiner preferences and click Complex-Modifications, then Add rule. You should see the text Function Keys in Terminal Apps and there click Enable All.

How to change which foreground application causes the effect

This code specifically provides function key standard functionality while using any of several terminal apps (and nothing when using any other app). This code also allows media key usage in terminal apps by using fn + function key.

While the OP asked about a generic specific application, VirtualBox was mentioned in particular. The below code could be changed to create the function key functionality in whatever application(s) was required by replacing the contents of each instance of "Bundle Identifiers: [ ... ]" with any application(s). (VirtualBox may be "^org\\.virtualbox\\.VirtualBoxVM" although I haven't tried it.) Use the included Karabiner EventViewer to find the application identifier for any application desired, and then plug in the values.

{
  "title": "Function Keys in Terminal Apps",
  "rules": [
    {
      "description": "Use regular function keys in terminal",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "f1"
          },
          "to": [
            {
              "key_code": "f1",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f2"
          },
          "to": [
            {
              "key_code": "f2",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f3"
          },
          "to": [
            {
              "key_code": "f3",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f4"
          },
          "to": [
            {
              "key_code": "f4",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f5"
          },
          "to": [
            {
              "key_code": "f5",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f6"
          },
          "to": [
            {
              "key_code": "f6",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f7"
          },
          "to": [
            {
              "key_code": "f7",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f8"
          },
          "to": [
            {
              "key_code": "f8",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f9"
          },
          "to": [
            {
              "key_code": "f9",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f10"
          },
          "to": [
            {
              "key_code": "f10",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f11"
          },
          "to": [
            {
              "key_code": "f11",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f12"
          },
          "to": [
            {
              "key_code": "f12",
              "modifiers": [
                "fn"
              ]
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f1",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f1"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f2",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f2"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f3",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f3"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f4",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f4"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f5",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f5"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f6",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f6"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f7",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f7"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f8",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f8"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f9",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f9"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f10",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f10"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f11",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f11"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        },
        {
          "type": "basic",
          "from": {
            "key_code": "f12",
            "modifiers": {
              "mandatory": [
                "fn"
              ]
            }
          },
          "to": [
            {
              "key_code": "f12"
            }
          ],
          "conditions": [
            {
              "type": "frontmost_application_if",
              "bundle_identifiers": [
                "^com\\.apple\\.Terminal$",
                "^com\\.googlecode\\.iterm2$",
                "^co\\.zeit\\.hyperterm$",
                "^co\\.zeit\\.hyper$",
                "^io\\.alacritty$",
                "^net\\.kovidgoyal\\.kitty$"
              ]
            }
          ]
        }
      ]
    }
  ]
}
3
  • 1
    Excellent solution! Thanks.
    – Theo Tiger
    Apr 10, 2020 at 19:35
  • Thanks! Any idea how to also make this work when for example Shift or Cmd + Shift is pressed? Like Shift + F1 or Cmd + Shift + F1 ?
    – Motsel
    Jul 29, 2020 at 20:59
  • @Motsel I had the same problem because I wanted that in Android Studio. What I did was 1. Import more rules from the internet. 2. look for Function keys work as fn keys (instead of media keys). 3. Either import as it or Edit JSON and ADD BUNDLE IDENTIFIER
    – Xi Wei
    Sep 2, 2022 at 3:19
2

Try combining FunctionFlip with BetterTouchTool:

  • Set the keys you want "flipped" with FunctionFlip
  • Set up application specific keys with BetterTouchTool
1
  • This is a fantastic app combination!
    – Lantrix
    Jun 20, 2014 at 0:20
2

I finally started to use Palua and it is just great.

Paula is no longer available so I use Fluor these days.

1

Grab IronAHK and use it to remap ALL keys instead of System Preferences (reason being is that it might conflict with system -> preferences), it comes with a makefile for compiling easily under Mono.

Then use it just like AutoHotkey:

#IfWinActive,,VirtualBox
F1::Send { F1 }
2
  • I cannot find any compiled version. Do I need to compile on my own ? Oct 2, 2009 at 19:46
  • Currently they do not provide any, yes you will need to compile your own unfortunately. But they provide a makefile I believe.
    – John T
    Oct 3, 2009 at 3:34
1

One alternative to Palua is KeyRemap4MacBook:

<?xml version="1.0"?>
<root>
    <appdef>
        <appname>FKEYS</appname>
        <equal>com.vmware.fusion</equal>
        <equal>org.virtualbox.app.VirtualBoxVM</equal>
    </appdef>
    <item>
        <name>fkeys</name>
        <identifier>fkeys</identifier>
        <only>FKEYS</only>
        <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_DOWN, KeyCode::F1</autogen>
        <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_UP, KeyCode::F2</autogen>
        <autogen>__KeyToKey__ KeyCode::EXPOSE_ALL, KeyCode::F3</autogen>
        <autogen>__KeyToKey__ KeyCode::LAUNCHPAD, KeyCode::F4</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::KEYBOARDLIGHT_LOW, KeyCode::F5</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::KEYBOARDLIGHT_HIGH, KeyCode::F6</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::MUSIC_PREV, KeyCode::F7</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::MUSIC_PLAY, KeyCode::F8</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::MUSIC_NEXT, KeyCode::F9</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::VOLUME_MUTE, KeyCode::F10</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::VOLUME_DOWN, KeyCode::F11</autogen>
        <autogen>__ConsumerToKey__ ConsumerKeyCode::VOLUME_UP, KeyCode::F12</autogen>
        <autogen>__KeyToKey__ KeyCode::F1, KeyCode::BRIGHTNESS_DOWN</autogen>
        <autogen>__KeyToKey__ KeyCode::F2, KeyCode::BRIGHTNESS_UP</autogen>
        <autogen>__KeyToKey__ KeyCode::F3, KeyCode::EXPOSE_ALL</autogen>
        <autogen>__KeyToKey__ KeyCode::F4, KeyCode::LAUNCHPAD</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F5, ConsumerKeyCode::KEYBOARDLIGHT_LOW</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F6, ConsumerKeyCode::KEYBOARDLIGHT_HIGH</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F7, ConsumerKeyCode::MUSIC_PREV</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F8, ConsumerKeyCode::MUSIC_PLAY</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F9, ConsumerKeyCode::MUSIC_NEXT</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F10, ConsumerKeyCode::VOLUME_MUTE</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F11, ConsumerKeyCode::VOLUME_DOWN</autogen>
        <autogen>__ConsumerToKey__ KeyCode::F12, ConsumerKeyCode::VOLUME_UP</autogen>
    </item>
</root>

See the source for the key code values and predefined settings.

0

Unfortunately, this isn't an option that Apple or FunctionFlip provides.

However, the author of FunctionFlip has obviously been able to write code that allows for dynamic configuration of the function keys so it can be done.

I would contact the author of FunctionFlip, and ask if he could add the features you need. Namely, controlling function key behavior depending on the active application.

If you pledge to donate for the feature it might get implemented faster.

Otherwise, I don't know of any other programs that offer this functionality.

0

You could use AppleScript to toggle the state of the function keys:

tell application "System Preferences"
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
click checkbox 1 of tab group 1 of window 1
end tell
quit application "System Preferences"

In addition, you can make this app specific with the help of Keyboard Maestro. If written a small tutorial how to pull this of here.

0

Use Fluor (https://fluorapp.net) app. This app make change function key behavior per app basis.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .