24
$\begingroup$

Many users have asked how to add KeyEvents to speed/improve keyboard input. This is just a random example. The technique is always the same. It requires that you manually modify KeyEventTranslations.tr (an important system file).

It always struck me as a bit odd that even power users propose to manually do this. Surely this system file is loaded into Mathematica at the start of a session (or not?), so it is perhaps possible to programmatically add the necessary KeyEvents to an already running Mathematica session. Of course in this case the new key bindings will only be temporary, but this might even be preferable in some cases.

So the question is: is it possible to programmatically add the necessary KeyEvents to an already running Mathematica session, without changing the KeyEventTranslations.tr? Alternatively: can we program Mathematica to backup KeyEventTranslations.tr and programmatically modify it? I know this is possible, I just wonder if someone has done it.

And perhaps automatically reload the new version so that it takes effect right away?


The art of Quitting.

I take the occasion to post another item related to "saving keystrokes for your retirement": It recently came to my attention that many/most users when they want to quit the kernel, type Quit[].

Well...the good news are that you just need to type Quit (saving two keystrokes).

$\endgroup$
1

2 Answers 2

16
$\begingroup$

This will add Quit to Control+Q (and Alt+V Q Q):

FrontEndExecute[
 FrontEnd`AddMenuCommands["MenuListQuitEvaluators",
  {MenuItem["AddMenu &Quit",
    FrontEnd`KernelExecute[ToExpression["Quit[]"]],
    MenuKey["q", Modifiers -> {"Control"}],
    System`MenuEvaluator -> Automatic]}]]

It only persists for the front end session.

Usually I keep the following in my KeyEventTranslations.tr file:

Item[KeyEvent["q", Modifiers -> {Control}],
    FrontEndExecute[
        FrontEnd`FrontEndToken[SelectedNotebook[], "EvaluatorQuit", Automatic]
    ]],
$\endgroup$
6
  • $\begingroup$ @IstvánZachar it works the same even if you do not put the AddMenu string. I took it off $\endgroup$
    – magma
    May 30, 2012 at 15:04
  • $\begingroup$ I like the FrontEndExecute argument. TOTALLY undocumented, lol. Can the same thing be done with the KeyEventTranslations ? Perhaps with some equally undocumented AddKeyEvent command? $\endgroup$
    – magma
    May 30, 2012 at 15:10
  • $\begingroup$ @ magma - setting key events is documented. See the help on NotebookEventActions. $\endgroup$ May 30, 2012 at 16:45
  • 5
    $\begingroup$ There is no way to add/overload KeyEventTranslations programmatically, but as we're seeing here, there are other ways to achieve similar effects. $\endgroup$
    – John Fultz
    Jun 1, 2012 at 8:42
  • $\begingroup$ @magma are you referring to either the "AddMenu &Quit" or "MenuListQuitEvaluators" string because I am not experiencing what described currently? $\endgroup$
    – William
    Jul 7, 2013 at 4:34
6
$\begingroup$

Here is a way that avoids the sometimes buggy FrontEnd`AddMenuCommands and gives us better flexibility. It's based on what I did here and here to do effectively arbitrary menu edits and placed on GitHub here.

The basic idea is to make an entirely new menu for our menu bindings. I'll do this like so:

Get["https://raw.githubusercontent.com/b3m2a1/mathematica-tools/master/CustomKeyEvents.wl"];
AddKeyEvent["r", FontColor -> Red, "Modifiers" -> {"Option"}];
AddKeyEvent["r", FontColor -> Inherited, "Modifiers" -> {"Command", "Option"}];

And then if we look at our new menus:

enter image description here

Our events are bound and ready for use:

enter image description here

enter image description here

The code itself is actually pretty small, but the basic idea is just to edit the Menu expression that Mathematica is using and set a new one using FrontEnd`ResetMenusPacket

$\endgroup$
2
  • 1
    $\begingroup$ This is a great package. Thank you. But trying it I get two Shadow warnings. do you have an update? :FrontEndPaste::shdw: Symbol Paste appears in multiple contexts {FrontEnd,System}; definitions in context FrontEnd may shadow or be shadowed by other definitions. FrontEndEvaluate::shdw: Symbol Evaluate appears in multiple contexts {FrontEnd,System}; definitions in context FrontEnd may shadow or be shadowed by other definitions. $\endgroup$
    – hieron
    Aug 26, 2021 at 21:52
  • $\begingroup$ @b3m2a1 Great package! I'm also having trouble getting rid of those shadows. Strange... I don't see Paste[] used in the entire package so I'm not sure what to fix. $\endgroup$
    – B flat
    Apr 2, 2022 at 21:39

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.