Enable Function Key Lock when using external keyboard/Disable when using internal keyboard on Mac Book Pro

When I'm developing I almost always have an external Bluetooth keyboard connected to my Mac Book Pro, and prefer the function lock to be active so the F1-F12 keys work without having to press the fn key. But when using the internal keyboard, I tend to be doing more casual things and brightness/volume etc are more commonly used.

Can I enable/disable the function lock depending on which keyboard I am using?


To make this work you have to use a third party tool: Karabiner.

To enable this you have to create a filter for the external keyboard and invert all function/consumer keys.

Prerequisites:

  1. Download, install and open Karabiner
  2. Open in the menulet Karabiner -> Preferences
  3. Open the tab Misc & Uninstall
  4. Hit the button Launch EventViewer
  5. Hit the tab Devices
  6. Make a note of the vendor and product ID of the external keyboard
  7. Close the EventViewer
  8. Hit the button Open private.xml
  9. Open the file private.xml with an appropriate editor
  10. Enter the customized keycode and replace the vendor and product ID by IDs found above

    <?xml version="1.0"?>
    <root>
      <devicevendordef>
        <vendorname>Apple</vendorname>
        <vendorid>0x05ac</vendorid>
      </devicevendordef>
      <deviceproductdef>
        <productname>ExternalKeyboard</productname>
        <productid>0x0256</productid>
      </deviceproductdef>
      <item>
        <name>Function to Consumer for External Keyboard</name>
        <identifier>private.function2consumerExtK</identifier>
        <device_only>DeviceVendor::Apple, DeviceProduct::ExternalKeyboard</device_only>
        <autogen>__KeyToKey__ KeyCode::F1, ConsumerKeyCode::BRIGHTNESS_DOWN</autogen>
        <autogen>__KeyToKey__ KeyCode::F2, ConsumerKeyCode::BRIGHTNESS_UP</autogen>
        ...
      </item>
      <item>
        <name>Consumer to Function for External Keyboard</name>
        <identifier>private.consumer2functionExtK</identifier>
        <device_only>DeviceVendor::Apple, DeviceProduct::ExternalKeyboard</device_only>
        <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_DOWN, ModifierFlag::FN, KeyCode::F1</autogen>
        <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_UP, ModifierFlag::FN, KeyCode::F2</autogen>
        ...
      </item>
    </root>
    

    Replace the ellipses by all other necessary consumer and function keys

  11. Save the file

  12. Open in the menulet Karabiner -> Preferences the tab Change Key
  13. Hit the ReloadXML button
  14. Enable the remappings. They should be listed at the beginning.

The final behavior should be as following with the function lock activated:

Internal keyboard:

F1: F1
Fn+F1: brightness up

External keyboard:

F1: brightness up
Fn+F1: F1

etc.

If you disable or skip the Consumer to Function for External Keyboard part it should act like this:

F1: brightness up
Fn+F1: brightness up

which means that the "function key layer" is disabled and only the consumer key layer is working.

etc.


Other possible keycodes:

KeyCode::EXPOSE_ALL,                 KeyCode::F3</autogen>
KeyCode::DASHBOARD,                  KeyCode::F4</autogen>
KeyCode::LAUNCHPAD,                  KeyCode::F4</autogen>
ConsumerKeyCode::KEYBOARDLIGHT_LOW,  KeyCode::F5</autogen>
ConsumerKeyCode::KEYBOARDLIGHT_HIGH, KeyCode::F6</autogen>
ConsumerKeyCode::MUSIC_PREV,         KeyCode::F7</autogen>
ConsumerKeyCode::MUSIC_PLAY,         KeyCode::F8</autogen>
ConsumerKeyCode::MUSIC_NEXT,         KeyCode::F9</autogen>
ConsumerKeyCode::VOLUME_MUTE,        KeyCode::F10</autogen>
ConsumerKeyCode::VOLUME_DOWN,        KeyCode::F11</autogen>
ConsumerKeyCode::VOLUME_UP,          KeyCode::F12</autogen>