Categories
english

Trying to make the TouchBar useful

… for a French Coder.

I know a number of fellow French programmers who always order their new Macs with an English keyboard, despite it being unadapted to the French language. French AZERTY keyboards are inconvenient for programming:

  • one must press the Shift key to type digits
  • typing some characters is awkward, e.g. pressing Option-Shift-( to type a [.
  • By default, text editors have keyboard shortcuts that can not even be typed. I have customized Xcode’s shortcuts for this reason.

I thought I could use the TouchBar to provide me extra keys!

How to customize the TouchBar

You basically have two choices:

  • BetterTouchTool is the most obvious option. I haven’t actually tried it, but it’s cheap (about 10 €) and there’s a 45-day trial.
  • MTMR is free and open-source. I chose it because I wanted to study how it worked under the cover. The app is not notarized for Catalina and still needs refinements, but it works.

The MTMR preset

You may download it now: FrenchProgrammer.json and skip the explanations.

I actually had to study the source code to write the Preset. The documentation is unclear, but there is actually a problem with the manner to simulate key presses on macOS, since you have to provide a “keycode”:

{
	"title": "2",
	"type": "staticButton",
	"action": "keyPress",
	"keycode": 84, // 0x54 is Keypad '2'
	"width": 81
},

You may find a list of keycodes here, or in Events.h of the HIToolbox framework. These codes are given for an American keyboard. For instance, it means that using the keycode 0x00 will type an ‘A’ on an American keyboard but a ‘Q’ on a French keyboard, because keycodes map to locations of keys, not characters.

The second problem is that MTMR currently does not offer a mean to provide key-modifiers along with the keycode, so I cannot simulate directly typing a Option-Shift-( to type a [.

There is however a solution: use AppleScript!

{
	"title": "[",
	"type": "staticButton",
	"action": "appleScript",
	"actionAppleScript": {
  		"inline": "tell application \"System Events\" to keystroke \"(\" using {option down, shift down}\r"
	},
	"width": 81
},

Time to test

I don’t know whether I am gonna keep this configuration but I should try it a couple of weeks to see if I get used to it.

I might also change my layout. Pressing Shift to type digits is not too much of an issue and I could propose other characters, or Alfred actions instead.