1

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!

glS
  • 7,623
  • 1
  • 21
  • 61
  • 1
    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 – 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 FrontEnd`NotebookDelete[FrontEnd`InputNotebook[]] 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
  • Unclear to me then. One thing you might be interested in, just as an aside, is that you can move the selection to the written content with NotebookWrite. That cuts out one selection move, so that's nice. – b3m2a1 Aug 27 '17 at 19:33
  • @b3m2a1 that is very nice indeed, I didn't notice that functionality, thanks! – glS Aug 27 '17 at 19:35
  • At least somewhat related: (680), (29258) – Mr.Wizard Aug 28 '17 at 01:14

1 Answers1

1

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

glS
  • 7,623
  • 1
  • 21
  • 61