This website collects many methods for implementing user defined keyboard shortcuts through KeyEvent items in KeyEventTranslations.tr, see 16697, 16165, 23285, 2572, etc. Some of these were collected in a package by Rolf Mertig.

It seems that latest Mathematica upgrades had modifications in front end event handling that caused problems for those who used personalised keyboard shortcuts, specifically in macOS (see 219137 and 197177). For me the problem started with macOS Catalina + Mathematica 12.

My manually defined Mathematica shortcuts present in KeyEventTranslations.tr do not override macOS keyboard shortcuts any more. For example, adding any combo with option key, like option+a for my shortcut in KeyEventTranslations.tr does not work, because macOS handles this event by inserting some special character defined by the active keyboard layout. These can be seen by doing: System Preferences ➙ Keyboard ➙ Show keyboard and emoji viewers in menu bar, then: status menus (right side of menu bar) ➙ Character Palette (language flag item) ➙ Show Keyboard Viewer.

This is how far I arrived:

The modifications to key bindings in macOS can be done using DefaultKeyBinding.dict, as explained in superuser/941286.

By default, "~a" would write å with U.S. layout if nothing is defined in DefaultKeyBinding.dict.

Adding the item "~a" = (); to DefaultKeyBinding.dict disables any predefined action to option+a globally and destroys the event, so nothing happens and nothing arrives to Mathematica nor any other application.

Adding "~a" = "noop:"; makes sure this event does nothing, but it will produce only a macOS beep in Safari, TextEdit and Mathematica without inserting å character. Then, adding a new item in KeyEventTranslations.tr: Item[KeyEvent["a", Modifiers -> {Option}], "Radical"],, instead of inserting square root on this key event, Mathematica inserts å character. At this point all Apple applications have just a beep, while Mathematica types å. It seems that some key event is passed to Mathematica, but it gets handled using macOS layout, instead of using KeyEventTranslations.tr.

Using a new option key combination that is unknown to macOS and Mathematica, say 'command+option+a' can be done only by first inserting "@~a" = "noop:"; in DefaultKeyBinding.dict, which, kind of, defines this key event and starts producing the beep everywhere. Then, add Item[KeyEvent["a", Modifiers -> {Command, Option}], "Radical"], in KeyEventTranslations.tr and these two together (with new keyboard shortcuts) will result in square root inserting on this key event. So, I can not override the present macOS shortcuts.

All of the key binding business is very native to macOS and it is not new in Catalina. I am not sure what changed in overall control of events, so I do not know how much of this is due to Mathematica and what is macOS' fault, but it seems that Mathematica has modified or lost the control of key events. So, I can introduce shortcuts in KeyEventTranslations.tr, but only by defining them also in DefaultKeyBinding.dict, and only for completely new key strokes.

Is there a way to access and control the front end handling of key events in Mathematica, in order to ensure that these events are passed properly and definitions in KeyEventTranslations.tr are used with priority? Certainly, it would be best to introduce a change that will be valid for all notebooks, or something that executes itself automatically on the start of every front end session.

Btw. MenuSetup.tr could also be used for defining a few keyboard shortcuts, before it starts producing a messy menu (see 219137 and 197177). I am curious why KeyEventTranslations.tr does not work.

P.S. every modification of DefaultKeyBinding.dict requires application restart before seeing the effects in that app.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.