5
$\begingroup$

I am trying to write a way that will do certain events depending on what screen is showing.

What happens so far is that the screen will change in accordance to a click, but when you try to do the key presses associated with that screen, they won't respond. I think the events are being passed to the old screen that is being shown. Is there an option or way to fix this?

My code thus sees if you click within certain coordinates (works, but not shown in simplified code below), then changes what it is showing. The arrow key pressing that corresponds to the new screen does not happen, unless I reevaluate the EventHandler block of code. Code is below:

displaying = True;

 start[] :=
 If[displaying,
  Show[firstScreen],
  Show[secondScreen]
  ]

EventHandler[
 Dynamic[start[]], {"UpArrowKeyDown" :> (If[!displaying, Print["up press"]]), 
  "DownArrowKeyDown" :> (If[!displaying, Print["down press"]]), 
  "MouseClicked" :> (If[displaying, 
     displaying = False])
  }]
$\endgroup$
5
  • $\begingroup$ I don't know what firstscreen and secondscreen are but I think what you probably need to use is NotebookEventActions $\endgroup$ Apr 12, 2012 at 0:18
  • $\begingroup$ Could you possibly use a "capture screen" whose only purpose is to collect events? Possibly using Overlay, you might be able to set up a non-dynamic transparent graphics pane on top of your dynamic one. $\endgroup$
    – amr
    Apr 12, 2012 at 3:16
  • 2
    $\begingroup$ If I use the above code, and assign the screens to some pics of my own, clicking the displayed picture changes it to the second. Using the arrow keys gives me printed "up press"/"down press". This looks like that's what you intended. $\endgroup$ Apr 12, 2012 at 21:08
  • $\begingroup$ The answer code below though rewrites what happens when you click the mouse within the whole notebook. So after running it, it worked, but I could not selected other code, or select where to type, etc. The click would only do what it was assigned to, even when I would quit kernel, it would still do this. The Overlay idea may work $\endgroup$
    – Chirese
    Apr 13, 2012 at 1:53
  • $\begingroup$ My code above requires to reevaluate it for key press events to work. It doesn't work for immediately. $\endgroup$
    – Chirese
    Apr 13, 2012 at 1:54

1 Answer 1

3
$\begingroup$

Try using NotebookEventActions

displaying = True;

SetOptions[EvaluationNotebook[], 
 NotebookEventActions :> {"UpArrowKeyDown" :> (If[! displaying, 
      Print["up press"]]), 
   "DownArrowKeyDown" :> (If[! displaying, Print["down press"]]), 
   "MouseClicked" :> (If[displaying, displaying = False])}]
$\endgroup$
1
  • $\begingroup$ I added a comment above that would be a response to this. $\endgroup$
    – Chirese
    Apr 13, 2012 at 1:29

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.