MacOS – Automate Fn key switch when opening Excel

applescriptautomatorkeyboardmacosms office

So Microsoft released a free preview of Office for Mac, and it is pretty sweet.

One of my biggest problem when using Excel on a mac has always been that the most popular/frequent shortcut (F2 -> edit cell) was not available on macs (there was a default replacement, cmd-T I think, but not half as handy as F2).

Now F2 is here in the preview, great! But one still has to either press the Fn key, or switch the whole Fn key row (link from Apple page). Hence the question:

Is there a way to either:

  • selectively only switch the F2 key, or
  • switch the Fn key row automatically when opening Excel?

Best Answer

I think the best way to achieve this is to install a keyboard mapper program. I use Karabiner. Once installed (might need a reboot?) you'll then see a menu bar icon that looks like a rounded square probably with the word 'Default' next to it; it is suggesting you're using the default mapping profile. Click on it and select 'Preferences' to access Karabiner's prefs applet so you can edit the private.xml file and add in the mapping below. You should be able to find a button for "Open private.xml" under the "Misc & Uninstall" tab of the preferences window.

Put this into your private.xml file:

<?xml version="1.0"?>
<root>

  <!-- ============================================================ -->
  <!-- Make F2 send as Ctrl-U for formula editing -->

  <appdef>
    <appname>Excel</appname>
    <equal>com.microsoft.Excel</equal>
  </appdef>

  <item>
    <name>F2 becomes Ctrl-U in Excel</name>
    <identifier>private.map_F2_to_ctrlU</identifier>
    <only>Excel</only>
    <autogen>__KeyToKey__ KeyCode::F2, KeyCode::U, ModifierFlag::CONTROL_L</autogen>
  </item>
</root>

That will now make it that when you hit F2, it will send to Excel the Ctrl-U sequence instead. Whatever F2 was doing before will no longer work, but it appears to be some kind of cut-and-paste.

Note that I am doing this on a wired apple extended keyboard (from a Mac Pro) where F2 doesn't require Fn (the alternate functions like volume do), so I don't know what the non-numeric keypad or laptop keyboards will do, but perhaps you'll have to add either KeyCode::FN or ModifierFlag::FN to the left side of that mapping if it doesn't work right away. Then again, the Fn behavior might be a keyboard preference- I don't recall off the top of my head.

It is worth pointing out that in the mapping file I created for this, I defined it in a way that this mapping will only be valid for the Excel application, it won't do it for any other application.

The only other thing you might need to do in the preferences window is under the "Change Key" tab, there will now be a checkbox for "F2 becomes Ctrl-U in Excel" which will need to be checked.

Let me know if that works. FWIW, I've used Karabiner for a few years now as a way to map the Right-Option key as a Windows Menu/Context key when running under Citrix Receiver (remote login to work PC) and it has worked well for me and updates have been painless.