Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following code. After evaluating it, when I move the mouse over the output text, it prints 1, and when I click the mouse on text, it prints 2.

EventHandler["text", {"MouseMoved" :> (Print[1]), "MouseDown" :> (Print[2])}]

I want to modify this so when I move the mouse over the output text, it prints 1, and when I click on text, it prints 2 and stops printing 1. How do I do this?

share|improve this question
up vote 4 down vote accepted

One possible way is by simply using a variable to flip MouseMoved's action to none when MouseDown is triggered:

DynamicModule[{flag = True},
        {"MouseMoved" :> If[flag, Print[1], ## &[]], 
         "MouseDown" :> (flag = False; Print[2])
share|improve this answer
I thought it was Rojo that uses guard and not you? Or were you referring to him? – R. M. Dec 23 '12 at 9:13
Rojo, certainly. I think his guard use is a great trick. – Mr.Wizard Dec 23 '12 at 9:16
I get Function::slotn: Slot number 1 in Hold[#1]& cannot be filled from (Hold[#1]&)[]. >> errors from this when clicking the text. Do you do too or is this a v7 issue? – Mr.Wizard Dec 23 '12 at 9:32
Works fine in v8.0.4 – m_goldberg Dec 23 '12 at 14:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.