3
$\begingroup$

I just found a behaviour of an EventHandler that I cannot explain. Here is a highly simplified version. I will present two simple EventHandlers. The first one shows the behaviour that I would have expected from the second one as well.

eventlist=Table[Invisible["10 pressed"], {10}];
eventnr=1;
aux[arg_String]:= (
  eventlist=Take[Append[eventlist, ToString[eventnr]<>arg],-10];
  eventnr=eventnr+1
);

Row[{
  EventHandler[Framed[Style[x^2, 30]],{{"KeyDown","1"}:>aux[" pressed"]}],
  Spacer[10], 
  Framed[Dynamic[Column[eventlist]]]
}]

When we click in the left box and then press the key 1 a number of times, we see in the right box how often we pressed the key. This works fine.

Now I place the Dynamic expression in the EventHandler:

eventlist = Table[Invisible["10 pressed"], {10}];
eventnr = 1;
aux[arg_String] := (
  eventlist = Take[Append[eventlist, ToString[eventnr] <> arg], -10]; 
  eventnr = eventnr + 1
);

EventHandler[
  Framed[Dynamic[Column[eventlist]]]
, {{"KeyDown", "1"} :> aux[" pressed"]}
]

When I click in the box and press 1, it is recognized. But further pressing 1 has no effect, so it seems that the selection must have moved outside the box. When we click again in the box and press 1, it works, but only once. Can someone explain what is happening here?

$\endgroup$

1 Answer 1

5
$\begingroup$

It is not an EventHandler thing.... You can think of Dynamic as a placeholder. Whenever this Dynamic triggers, the current visual tree from that point down is replaced with a completely new one. When that happens, it is not always possible, desirable, etc... to restore the selection back to an equivalent position.

You can see this for yourself. Evaluate your second example, click immediately to the right of the column and hit the left arrow twice and hit 1 a couple of times. Now hit the left arrow again and hit 1 a couple of times.

To prevent the selection from diving into the underlying Grid, you can use something like Dynamic@Style[Column[eventlist], Selectable -> False] instead.

$\endgroup$
1
  • $\begingroup$ Very illuminating! $\endgroup$ Commented Oct 26, 2018 at 7:03

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.