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.

回答 (2 件)

Geoff Hayes
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 件のコメント
Eshwar Gowda M N
Eshwar Gowda M N 2015 年 3 月 3 日
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 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
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
Mandeguz 2017 年 6 月 5 日
編集済み: Mandeguz 2017 年 6 月 5 日
I think the only way to do this is as Geoff and Eshwar discussed.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by