1
$\begingroup$

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[
          FrontEnd`SelectionMove[FrontEnd`InputNotebook[], After, Cell];
          FrontEnd`NotebookWrite[
            FrontEnd`InputNotebook[], 
            Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]
          ];
          FrontEnd`SelectionMove[FrontEnd`InputNotebook[], Previous, Cell];
          FrontEnd`SelectionEvaluate[FrontEnd`InputNotebook[]];
          FrontEnd`SelectionMove[FrontEnd`InputNotebook[], Previous, CellGroup, 1];
          FrontEnd`NotebookDelete[FrontEnd`InputNotebook[]]
        ]
    ]

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!

$\endgroup$
  • 1
    $\begingroup$ 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 FrontEnd`NotebookSuspendScreenUpdates like I do here $\endgroup$ – b3m2a1 Aug 27 '17 at 19:14
  • $\begingroup$ 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. $\endgroup$ – glS Aug 27 '17 at 19:21
  • $\begingroup$ @b3m2a1 by the way, do you know why my example didn't work? $\endgroup$ – glS Aug 27 '17 at 19:22
  • $\begingroup$ 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 FrontEnd`NotebookDelete[FrontEnd`InputNotebook[]] before being passed to FrontEndExecute. $\endgroup$ – b3m2a1 Aug 27 '17 at 19:25
  • $\begingroup$ @b3m2a1 I tried that but it still didn't work $\endgroup$ – glS Aug 27 '17 at 19:27
1
$\begingroup$

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).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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