# Make EventHandler work for clicks and keys in a Dynamic display

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])
}]

-
I don't know what firstscreen and secondscreen are but I think what you probably need to use is NotebookEventActions – Mike Honeychurch Apr 12 '12 at 0:18
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. – amr Apr 12 '12 at 3:16
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. – Sjoerd C. de Vries Apr 12 '12 at 21:08
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 – Chirese Apr 13 '12 at 1:53
My code above requires to reevaluate it for key press events to work. It doesn't work for immediately. – Chirese Apr 13 '12 at 1:54

Try using NotebookEventActions
displaying = True;