2
$\begingroup$

I tried to write a function to "Evaluate in Place", as I've shown in: How to get the output of evaluating in place and append the output to the place.

But now I have encountered a new problem. I wrote the following code and executed it, which caused the G key on my keyboard to be unusable for letter input in MMA Notebook.

How can I clear NotebookEventActions or unbind keydown event?

evaluateSelectedText[] := 
 Module[{nb = EvaluationNotebook[], sel}, sel = NotebookRead[nb];
  If[NotebookRead[nb] === {}, NotebookWrite[nb, Cell["", "Input"]]];
  SelectionEvaluate[nb]] 
CurrentValue[$FrontEndSession, {InputNotebook[], InputAliases, 
    "EvaluateSelectedText"}] = 
  "Control+Shift+G" -> 
   FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], 
      Cell["", "Input"]]}];
SetOptions[EvaluationNotebook[], 
 NotebookEventActions :> {{"KeyDown", "G"} :> 
    If[CurrentValue[InputNotebook[], StyleDefinitions] === 
      "Evaluation", evaluateSelectedText[], True]}]
$\endgroup$
3
  • $\begingroup$ All shortcuts are defined in the file "KeyEventTranslations.tr" . Search this file in the MMA installation. $\endgroup$ Commented May 10 at 7:21
  • $\begingroup$ @DanielHuber, Thanks for your reply, but I have checked the KeyEventTranslations.tr in the directory ~/Mathematica/14.0/SystemFiles/FrontEnd/TextResources/X/KeyEventTranslations.tr, I did not fine anything about the event banding about "Shift+G"; Additionally, it should be noted that this inexplicable key binding is only valid in files where I have executed the above command. Even if I restart the file, the default key function cannot be restored $\endgroup$
    – Yao Li
    Commented May 10 at 9:23
  • $\begingroup$ If the wrong key binding only affects files in which you executed the command and no other files and if restarting MMA does not help, then I think the wrong binding must be stored somewhere in the file. Look at "Format/Option Inspector" and then "Notebook Options" $\endgroup$ Commented May 10 at 19:59

1 Answer 1

3
$\begingroup$

You key has become unusable because, as explained in the documentation for NotebookEventActions:

By default, any action specified for an event by NotebookEventActions will override any default action that the Wolfram System would take for that event.

If you want to retain the behaviour of the key, you have to allow passing the events with PassEventsDown and PassEventsUp:

SetOptions[EvaluationNotebook[], 
 NotebookEventActions :> {{"KeyDown", "G"} :> 
    If[CurrentValue[InputNotebook[], StyleDefinitions] === 
      "Evaluation", evaluateSelectedText[], True], 
   PassEventsDown -> True, PassEventsUp -> True}]

You can see currently set event actions with:

Options[EvaluationNotebook[]]
(* {..., NotebookEventActions :> {{"KeyDown", "G"}, ...}} *)

To remove the actions, simply use:

SetOptions[EvaluationNotebook[], NotebookEventActions :> {}]
$\endgroup$
1
  • $\begingroup$ Thank you so much. You have been very helpful to me twice in a row $\endgroup$
    – Yao Li
    Commented May 10 at 9:54

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.