I want to use this to add a keyboard shortcut to an undo function I wrote. The basic problem is the following:

I have a command, let's say x=1, and I want to run the command with a keyboard shortcut. Is there a way to do it?

Editing the KeyEventTranslation.tr file is what I tried.



does not work.

 KeyEvent["z", Modifiers -> {Command}], 

adds the text x=1 where the cursor is, but it still needs to be evaluated and I need to be at the right place with the cursor.

Are there other ways?

This might be related, but I couldn't get anything out of it so far.


1 Answer 1


Here are four approaches to setting up keyboard shortcuts. The last is the best!


First examples using notebook manipulation, (as you tried):

This example adds a command to the Insert menu, with a key combination Control+U (normally the underline command). This addition just lasts for the session, but could be added to an init file.

  {Delimiter, MenuItem["Evaluate x = 1",
     nb = SelectedNotebook[];
     SelectionMove[nb, After, Cell]; 
     NotebookWrite[nb, Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]];
     SelectionMove[nb, Previous, Cell];
    MenuKey["u", Modifiers -> {"Control"}],
    System`MenuEvaluator -> Automatic]}]]

Note, you can find the box format like so:

ToBoxes[Hold[x = 1]]

Out = RowBox[{"Hold", "[", RowBox[{"x", "=", "1"}], "]"}]

This could also be done invisibly like this:

  {Delimiter, MenuItem["Evaluate x = 1",
     nb = CreateDocument[Null, Visible -> False, WindowSelected -> True];
     NotebookWrite[nb, Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]];
     SelectionMove[nb, Previous, Cell];
    MenuKey["u", Modifiers -> {"Control"}],
    System`MenuEvaluator -> Automatic]}]]

A quicker version just uses ToExpression, (not Evaluate, as you first tried):

  {Delimiter, MenuItem["Evaluate x = 1",
    MenuKey["u", Modifiers -> {"Control"}],
    System`MenuEvaluator -> Automatic]}]]


The notebook manipulation can be implemented in KeyEventTranslations.tr like so:

Item[KeyEvent["u", Modifiers -> {Control}], FrontEndExecute[
  FrontEnd`SelectionMove[FrontEnd`InputNotebook[], After, Cell];
    Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]];
  FrontEnd`SelectionMove[FrontEnd`InputNotebook[], Previous, Cell];


Another approach for adding a keyboard shortcut is this:

function[] := x = 1

 NotebookEventActions -> {"DownArrowKeyDown" :> function[]}]

Setting notebook event actions to command or control key combinations does not appear to be possible. A shifted key assignment can be made, e.g. Shift+O (just capital "o"):

NotebookEventActions -> {{"KeyDown", "O"} :> ...

You can replace SelectedNotebook[] with $FrontEnd (note 'Details') - that adds the assignment to your front end init file. (To undo, you would need to edit the init file to remove the event assignment.)


Finally -- and this is probably the most obvious -- place x = 1; in a file called assign.m and call it from a menu item specified in MenuSetup.tr with:

Item["Evaluate x = 1",
 KernelExecute[Get["assign.m", Path -> "C:\\Users\\chrisd\\Documents"]]]],
 MenuKey["u", Modifiers->{"Control"}], MenuEvaluator->Automatic]

Alternatively, use ToExpression:

Item["Evaluate x = 1", KernelExecute[ToExpression["x=1;"]],
 MenuKey["u", Modifiers -> {"Control"}], MenuEvaluator -> Automatic]
  • $\begingroup$ Very nice. That definitely works. Perfect would be to not add any text to the notebook. I like that there is no need to edit any system files. $\endgroup$
    – jens_bo
    Dec 12, 2012 at 12:40
  • $\begingroup$ @jenson_bo - I added a second AddMenuCommands variation which doesn't leave any text in the input notebook. $\endgroup$ Dec 12, 2012 at 13:43
  • $\begingroup$ Great answer, I tried the invisible option, it works, but since I am using the notebooks name in the assigned function creating a new document seems to be a problem. I will try the last option later. Thanks. Once the keyboard shortcuts work in a nice way, I think my layman's undo/version control is presentable - but of course can be improved even more. $\endgroup$
    – jens_bo
    Dec 12, 2012 at 14:30
  • 1
    $\begingroup$ @jenson_bo - In the first version, you could delete the created cell like so: NotebookWrite[nb, Cell[BoxData[RowBox[{RowBox[{"x", "=", "1"}], ";"}]], "Input"]]; SelectionMove[nb, Previous, Cell]; SelectionEvaluate[nb]; SelectionMove[nb, Previous, Cell]; NotebookDelete[nb] $\endgroup$ Dec 12, 2012 at 14:48
  • $\begingroup$ @ Chris Degnen - SelectionMove[nb, Previous, Cell]; NotebookDelete[nb] That does the trick I think. :) $\endgroup$
    – jens_bo
    Dec 12, 2012 at 14:55

Your Answer

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

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