I would like to let a boolean variable switch when I press a key on the keyboard.
For example, I write
activateE := Block[{}, edown = True;];
SetOptions[EvaluationNotebook[], NotebookEventActions -> {{"KeyDown", "e"} :> activateE}]
and then try to monitor the key presses via
edown = False;
Dynamic[edown]
While the key e
now stops producing any text input into the notebook, unfortunately the Dynamic
always keeps showing False
, regardless how often I press the key e
. What am I doing wrong, or how to do this properly?
EDIT:
Alternatively, I am trying the CurrentValue
function. For example,
Dynamic[CurrentValue["ModifierKeys"]]
properly shows e.g. when the Ctrl
key is being pressed. But if I try
Dynamic[CurrentValue["EventKey"]]
the output is None
no matter which key I press. Is there an option of CurrentValue
that would register a regular key press, like letter e
?
EDIT2:
What seems to work, is
EventHandler[InputField[], {"KeyDown", "e"} :> activateE]
but only if I first click on the InputField to bring it into focus.
{{"KeyDown", "e"} :> activateE
instead. $\endgroup$ – Carl Woll Aug 16 '20 at 16:16NotebookEventActions -> {{"KeyDown", "e"} :> activateE, PassEventsUp -> True}
? $\endgroup$ – kglr Aug 16 '20 at 16:40EventHandler[InputField[], {"KeyDown", "e"} :> activateE]
, but only if I first click on theInputField
to bring it into focus. $\endgroup$ – Kagaratsch Aug 16 '20 at 16:44