Hi all,
I need to create a Button that behaves like a key on a MIDI keyboard: it is ON when it is actively being pushed down, and off when released. In other words:
- It has only two states, “off” and “on”
- It goes to “on” at mouseDown click, and stays there only as long as user is holding the mouse down
- Upon release of the mouse (mouse up), button switches back to “off” state.
I was able to create this with the code below, but if feels like cheating:
(
w = Window.new;
b = Button(w, Rect(100, 100, 200, 200))
.states_([["off", Color.black, Color.white]])
.mouseDownAction_({
"ON while mouse is down".postln;
b.states_([["on", Color.black, Color.red]])
})
.action_({
"OFF when mouse is released".postln;
b.states_([["off", Color.black, Color.white]]);
});
w.front;
)
It feels like cheating because the button above has really just one state, and I’m just changing its color and label at the right moment. So I don’t get the benefit of having a button.value of 0 and 1 for the ‘off’ and ‘on’ respectively as it would be the case if I really had two states defined.
Is there a simpler way to achieve this? I couldn’t figure out just by reading the Button help file.
Thanks for any hints!
Bruno