1

So I've been playing with FrontEndTokens recently and discovered that there are some of them that don't seem to have a standard-usage way of being called. For example, the "Tab" token isn't called when the tab key is pressed, but is captured by the {"MenuCommand","Tab"} event binding, which makes it ripe for abuse as a custom event. That is, if it really doesn't have a common usage, it should be possible to use it as a place holder for a function that will be performed by whatever event handlers. Tkinter in python has this sort of thing via a virtual event caller.

That led me to wondering if it's possible to construct personalized FrontEndTokens that I could use as EventHandler callbacks.

I'm afraid the answer to this is probably no, given that some of the simple test I've run (e.g. trying NotebookEventActions -> {{"MenuCommand", "CustomEvent"} :> Print[1]} haven't worked) but if this were possible it would extend the UI capabilities of Mathematica dramatically.

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • So you want an EventHandler to capture custom tokens. How to you want to trigger them? – Kuba Dec 14 '16 at 08:50
  • My thought was to use FrontEndTokenExecute and a {"MenuCommand",token}. The idea is to call the event myself and have it be caught by an event handler, for example, have like "MouseClicked" and "MouseExited" call FrontEndTokenExecute["VirtualEvent"]. Basically I want to emulate the tkinter virtual event / event_generate mechanism. – b3m2a1 Dec 14 '16 at 16:39
  • Ok, but how would event handlers know that they should react or not? Because if every EH should then you can just run event[] and Dynamics can catch that. – Kuba Dec 14 '16 at 16:50
  • Obviously I could just call a function if I have just one event handler, but say there are two in nested focus. The first could do event1[] on the "CustomEvent" call and the second could do event2[]. This is why tkinter allows you to register virtual events. I get what you're saying, though, wondering which event handlers would catch such a call, but I figure it's the same as how EventHandler or CellEventActions behave with "MenuCommand" tokens. Only event handlers with current focus should catch them. – b3m2a1 Dec 14 '16 at 17:23
  • The trick would be to have something like Dynamic[customEventSymbol; action;Spacer[0], TrackedSymbols :> {customEventSymbol}] as a "listner" and FrontEndTokenExecute translated to customEventSymbol++ or something. – Kuba Dec 14 '16 at 18:34
  • Well yeah, that's what I do currently, but this would be more convenient. – b3m2a1 Dec 14 '16 at 18:58
  • By the way, a potentially cleaner listener can be constructed using DynamicWrapper. Just do DynamicWrapper[eventHandlerExpr, customEventSymbol; action, TrackedSymbols :> {customEventSymbol}]. That way you can just put the expression you were going to wrap EventHandler around in there. Obviously it also works with Spacer[0] to make an invisible listener, too. – b3m2a1 Dec 14 '16 at 19:04
  • Yes, I know, I just don't like it ;) I find Dynamic more readable, the more that they changed something and it doesn't behave as Dynamic[expr; Refresh[e, None]], so it is better now, but the fact its documentation sucks repels me. – Kuba Dec 14 '16 at 19:13

0 Answers0