Mapping a Physical Escape Key on a MacBook Pro with Touch Bar

Posted

Updated

11 Jan 2019

So you miss the physical Escape key on your brand new MacBook Pro with Touch Bar? Here's my fix to re-map a seldom used key to Escape.

I like the tactile clickety-clack of physical keyboard - there is something more reassuring about it. But, on the latest MacBook Pro with Touch Bar, the Escape key has been removed in favour of a virtual button on the Touch Bar:

MacOS User Guide - Use the Touch Bar on Mac
Source: macOS User Guide

The macOS Way: Keyboard Preferences

The simplest option is to re-map a modifier key - Caps Lock, Control, Option, Command or Function key - to Escape.

  • Go to System Preferences > Keyboard > Modifier Keys...
  • Change a key e.g. Caps Lock to Escape:

System Preferences Keyboard Modifier Keys

For me, this solution is than ideal because my muscle memory demands the top-left most key be Escape, especially when using vi while on auto-pilot. Besides, I don't want to go with the standard modifier keys.

Simple Karabiner Elements Setup

Enter Karabiner Elements 12.1.0: "A powerful and stable keyboard customizer for macOS." With with it I can re-map the top-left-most key to Escape - plus, is free and open source.

On my US keyboard (ANSI) the top-left-most is the "grave accent" ` and "tilde" ~ key:

US keyboard layout Accent / Tilde Key

So to remap this key to Escape:

  • After installing, run the Karabiner-Elements.app
  • From the Simple Modifications tab, press Add Item
  • Then map the From Keygrave_accent_and_tilde (`) to the Escape key as shown:

Karabiner Elements Preferences Simple Modifications

Easy!

If you keyboard has a UK layout (ISO), the top-left-most key may be the non_us_backslash§ key.

Advanced Karabiner Elements Setup

But, that still wasn't good enough for me! The steps above lost me the tilde and grave accent keys, whereas I wanted to maintain:

  • ` - Escape
  • Shift-` -"tilde" ~ (which is the default behaviour of the key)
  • Option-` - "grave accent" to create an accented character e.g. ù
  • Command-` - for a normal tick mark `
  • Option-Command-` - to open the Force Quit dialog.

I need tilde for things like the Home Directory shortcut (cd ~/Desktop) and the tick mark for Markdown's "double tick".

In Karabiner, this can be done via Complex Modifications, but it requires a bit of code.

  • Fire up your editor of choice - I use Visual Studio Code
  • Edit ~/.config/karabiner/karabiner.json.
  • Under the rules element, add this:
{
    "description": "A Grave Escape",
    "manipulators": [
        {
            "type": "basic",
            "from": 
                { "key_code": "grave_accent_and_tilde" },
            "to": 
                [ { "key_code": "escape" } ]
        },{
            "type": "basic",
            "from": { 
                "key_code": "grave_accent_and_tilde", 
                "modifiers": { "mandatory": [ "command" ] } 
            },
            "to": [ { "key_code": "grave_accent_and_tilde" } ]
        },{
            "type": "basic",
            "from": { 
                "key_code": "grave_accent_and_tilde", 
                "modifiers": { "mandatory": [ "option", "command" ] }
            },
            "to": [ {
                "key_code": "escape",
                "modifiers": [ "option", "command" ]
            } ]
        }
    ]
}

You should get something looking similar to this. For clarity I've intentionally mis-indented it (indents don't matter anyway), plus the screen shot is for a previous version of my code (see the Update below):

Visual Studio Code Editing karabiner.json

  • Save, and the Karabiner Elements Complex Modifications tab, you should see the rule loaded. A successful configuration will be reflected in the Log:
[2018-12-18 21:00:00.539] [info] [grabber] Load karabiner.json...
[2018-12-18 21:00:00.540] [info] [grabber] core_configuration is updated.

Now test it!

You can also use the Karabiner-EventViewer.app to monitor the re-mapped keystrokes.

Here I press Escape, then Left Shift-Escape and finally, Fn-Escape:

Karabiner EventViewer Test Results

Hope it helps you get a physical Escape keyboard key while maintaining the keystrokes for ` and ~!

Updated 4 Jan 2019: Changed the script. I realized some lines were not required, and I added code to maintain the Force Quit Applications keystroke.

Updated 11 Jan 2019: Yeesh, missed out the Command modifier keystroke.