How to activate a toggle/pushbutton as long as the mouse button is pressed, and deactivate it when released ?
9 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Geoff Hayes
on 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 Comments
Geoff Hayes
on 3 Mar 2015
Yes, it should be possible to activate a specific toggle button by checking the coordinate of the mouse cursor when clicked.
David
on 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.
1 Comment
Mandeguz
on 5 Jun 2017
Edited: Mandeguz
on 5 Jun 2017
There is no buttonUp for uicontrol: https://www.mathworks.com/help/matlab/ref/uicontrol-properties.html
I think the only way to do this is as Geoff and Eshwar discussed.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!