How to activate a toggle/pushbutton as long as the mouse button is pressed, and deactivate it when released ?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Geoff Hayes
el 1 de Mzo. de 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 comentarios
Geoff Hayes
el 3 de Mzo. de 2015
Yes, it should be possible to activate a specific toggle button by checking the coordinate of the mouse cursor when clicked.
David
el 3 de Mzo. de 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 comentario
Mandeguz
el 5 de Jun. de 2017
Editada: Mandeguz
el 5 de Jun. de 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.
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!