Best way to add KeyEvents and faster Quit
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]
]],
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:
Our events are bound and ready for use:
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