How to activate a toggle/pushbutton as long as the mouse button is pressed, and deactivate it when released ?

12 visualizzazioni (ultimi 30 giorni)
To be specific, I need to implement this behavior:
Please help me.
I want to send 'Max' value as long as I press the Toggle button (mouse button pressed), and the value should change to 'Min' when the mouse button is released.
I am exploring on using some options, but not sure whether I get the expected behavior. So wanted to post a question while I continue working on that. Please help me.

Risposte (2)

Geoff Hayes
Geoff Hayes il 1 Mar 2015
Eshwar - you can create function callbacks for the mouse down and mouse up events (within a figure) and have them fire whenever the user presses or releases the button when the mouse pointer is within the figure. The following test function demonstrates this
function MouseButtonEvents
hFig = figure;
set(hFig,'WindowButtonDownFcn', @mouseDownCallback, ...
'WindowButtonUpFcn', @mouseUpCallback);
function mouseDownCallback(~,~)
fprintf('mouse button is down!\n');
end
function mouseUpCallback(~,~)
fprintf('mouse button is up!\n');
end
end
Save the above to a file named MouseButtonEvents.m and then run it to see how the different callbacks respond to the mouse button events.
From your question, you seem to want to "do something" so long as the mouse button is down. I suppose what you could do is create a periodic timer in the mouse button down callback and have it do that something so long as the button is down. The above code would become
function MouseButtonEvents
hFig = figure;
myTimer = timer('Name','MyMouseButtonTimer', ...
'Period',1.0, ...
'StartDelay',0.001, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',@timerCallback);
set(hFig,'WindowButtonDownFcn', @mouseDownCallback, ...
'WindowButtonUpFcn', @mouseUpCallback);
function mouseDownCallback(source,event)
fprintf('mouse button is down!\n');
start(myTimer);
end
function mouseUpCallback(source,event)
stop(myTimer);
fprintf('mouse button is up!\n');
end
function timerCallback(source,event)
fprintf(' mouse button is still down!\n');
end
end
We create a timer object that will fire every one second. Since we nest all of the callbacks within the MouseButtonEvents function, then each of the mouse button callbacks has access to the timer to either start or stop it.
  2 Commenti
Eshwar Gowda M N
Eshwar Gowda M N il 3 Mar 2015
Thank you very much for the detailed answer, Geoff Hayes. I will try this out.
But can I make it specific to each toggle button. Say, I have more than one toggle button, then I need the whole behavior explained above to work independently for each toggle button. As you have said, it is for mouse button click anywhere in the figure. But that shouldn't be like that in my case.
One solution I can think of, is to assign specific area for each toggle button in figure, and check the X-Y co-ordinate of mouse cursor when clicked, and activate corresponding toggle button.
Can you please tell me if that is possible, or is there any other way out ?
Thanks again for the reply!
Geoff Hayes
Geoff Hayes il 3 Mar 2015
Yes, it should be possible to activate a specific toggle button by checking the coordinate of the mouse cursor when clicked.

Accedi per commentare.


David
David il 3 Mar 2015
The button object itself should have the buttonUp/butttonDown functions. You should be able to register your callbacks with each individual callback. You can get the handle to the button from the uicontrol function that generates the button.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by