8
$\begingroup$

I'm trying to create a demonstration in Mathematica where a checkbox will show an answer, but I only want the answer to be shown while the mouse button is pressed. There seems to be a "Momentary" option, but I can't figure out how it's used.

Checkbox[Momentary -> True]

seems to do what I want, but I need it in the Manipulate function.

Here's a simple example of what I'm trying to do:

Manipulate[
  ToString[x] <> " + " <> ToString[y] <> " = " <> If[a, ToString[x + y], ""], 
  {x, 1, 10, 1},
  {y, 1, 10, 1}, 
  {{a, False}, {True, False}, Momentary -> True}]

Doesn't work as I want. Replacing the last line with any of the following

 {{a, False}, {True, False}, CheckboxBoxOptions -> {Momentary -> True}}]

or

 Control[{{a, False}, {True, False}, Momentary -> True}]]

or

 Control[{{a, False}, {True, False}, CheckboxBoxOptions -> {Momentary -> True}}]]

doesn't do it either.

The documentation is very light in how Checkbox options are to be used.

Any ideas?

$\endgroup$
  • 1
    $\begingroup$ Where did you get that Option from? It does not seem to work standalone Checkbox[Dynamic@x, Momentary -> True]. $\endgroup$ – Kuba Nov 17 '14 at 13:14
  • 3
    $\begingroup$ @Kuba It is an option of the underlying CheckboxBox and despite the red color it works for Checkbox[Momentary->True], without an argument or with argument True of False, but not if Dynamic is used as an argument. Therefore, my feeling is that this problem cannot be solved with a Checkbox alone and that you need an EventHandler or something like that, as in your solution. $\endgroup$ – Fred Simons Nov 17 '14 at 13:55
8
$\begingroup$

Here's a walkaround:

    Manipulate[
                x,
                Row @ List @ EventHandler[ Checkbox[Dynamic[x]], 
                                           {"MouseDown" :> (x = True), 
                                            "MouseUp" :> (x = False)}]
              ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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