2
$\begingroup$

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?

$\endgroup$

1 Answer 1

4
$\begingroup$

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

DynamicModule[{flag = True},
    EventHandler["text", 
        {"MouseMoved" :> If[flag, Print[1], ## &[]], 
         "MouseDown" :> (flag = False; Print[2])
    }]
]
$\endgroup$
4
  • $\begingroup$ I thought it was Rojo that uses guard and not you? Or were you referring to him? $\endgroup$
    – rm -rf
    Dec 23, 2012 at 9:13
  • $\begingroup$ Rojo, certainly. I think his guard use is a great trick. $\endgroup$
    – Mr.Wizard
    Dec 23, 2012 at 9:16
  • $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Dec 23, 2012 at 9:32
  • $\begingroup$ Works fine in v8.0.4 $\endgroup$
    – m_goldberg
    Dec 23, 2012 at 14:11

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.