Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

1 Answer 1

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},
    EventHandler["text", 
        {"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? –  rm -rf 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

 
discard

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.