How to activate a toggle/pushbutton as long as the mouse button is pressed, and deactivate it when released ?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Geoff Hayes
2015-3-1
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 个评论
Geoff Hayes
2015-3-3
Yes, it should be possible to activate a specific toggle button by checking the coordinate of the mouse cursor when clicked.
David
2015-3-3
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 个评论
Mandeguz
2017-6-5
编辑:Mandeguz
2017-6-5
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!