# How to use a shortcut to evaluate an expression without having it show in the notebook, using the KeyEventTranslations.tr?

I'm trying to have a custom function evaluated via a keyboard shortcut, and add this shortcut via the KeyEventTranslations.tr.

As a test example I took the example provided in this answer to add and evaluate the expression x=1 when pressing ctrl+u.

My only problem with that is that it leaves the x=1 expression on the notebook, while I don't want to see it. I therefore tried to delete it after evaluation, using the following:

Item[KeyEvent["u", Modifiers -> {Control}],
FrontEndExecute[
FrontEndSelectionMove[FrontEndInputNotebook[], After, Cell];
FrontEndNotebookWrite[
FrontEndInputNotebook[],
Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]
];
FrontEndSelectionMove[FrontEndInputNotebook[], Previous, Cell];
FrontEndSelectionEvaluate[FrontEndInputNotebook[]];
FrontEndSelectionMove[FrontEndInputNotebook[], Previous, CellGroup, 1];
FrontEndNotebookDelete[FrontEndInputNotebook[]]
]
]


This is however not working for some reason. It seems to work up to the delete line (even though I didn't manage to properly select the whole cell group). When the last NotebookDelete line is added however, nothing seems to happen: the value of x is not changed at all and no expression is printer.

How can I modify this to have this toy expression (or any other expression) work in this way?

Of course, a way to just evaluate the expression without writing it in the notebook at all would be also appreciated!

• Use KernelExecute. See for example what Rolf Mertig does here. If you need to work with a notebook but don't want it visible use FrontEndNotebookSuspendScreenUpdates like I do here – b3m2a1 Aug 27 '17 at 19:14
• that is actually what I first tried to do! I must have gotten some detail wrong because now I tried it again following the examples in that package and it works, thanks! If you want to add a brief answer I'll accept it, otherwise I'll selfanswer this one. – glS Aug 27 '17 at 19:21
• @b3m2a1 by the way, do you know why my example didn't work? – glS Aug 27 '17 at 19:22
• Have at it. I think your issue is that you need to encapsulate that in a list. My copy of Mathematica is currently frozen but FrontEndExecute takes a list for multiple commands as it's not Hold* so that should have all evaluated out to FrontEndNotebookDelete[FrontEndInputNotebook[]] before being passed to FrontEndExecute. – b3m2a1 Aug 27 '17 at 19:25
• @b3m2a1 I tried that but it still didn't work – glS Aug 27 '17 at 19:27

## 1 Answer

Following b3m2a1's advise, and the examples found in this package, the answer is to use KernelExecute instead of front end functions.

I added the following to the KeyEventTranslations.tr file in (modify path according to your OS) FileNameJoin@{\$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources", "Windows"}:

Item[KeyEvent["u", Modifiers -> {Control}],
KernelExecute[
x = 1 + 1
],
MenuEvaluator -> Automatic
]
`

and it works as intended (after restarting the front end).