Click escape to stop my MouseDownfcn in the GUI?

1 vue (au cours des 30 derniers jours)
Collegue
Collegue le 10 Jan 2020
When you select the "move" button, my GUI sees if the cursor is on my axes and when you press it, you can move the graph in it. What I want is that when the escape button is pressed, the graphic cannot be moved. That is, I want to disable this function but when the button is pressed again it will work again.
Here is the pushbutton
function move_Callback(hObject, eventdata, handles)
% hObject handle to mover_mano (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hold(handles.oxi_Plot,'on');
linkaxes([handles.oxi_Plot handles.TOI_Plot],'x');
if hObject.Value == 1
set(handles.figure1, ...
'WindowButtonDownFcn', @mouseDownCallback, ...
'WindowButtonUpFcn', @mouseUpCallback, ...
'WindowButtonMotionFcn', @mouseMotionCallback);
end
Here is where I want to write the code when escape function is pressed
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
switch eventdata.Key
case 'escape'
disp('Pressed ESC-key');
end

Réponses (1)

Guillaume
Guillaume le 10 Jan 2020
Probably, the simplest thing is to disable whichever callback you don't want to execute once escape is pressed, so in the WindowKeyPressFcn:
switch eventdata.Key
case 'escape'
set(handles.figure1, 'WindowButtonMotionFcn', []); %disable motion callback
Of course, you'll need to set it back once you want it to be enabled again.
Another option is to add a flag to your handles structure that you set/unset on escape and check for in the callback:
%handles.domove is a flag that you created on GUI construction
%in WindowKeyPressFcn
switch eventdata.Key
case 'escape'
set(handles.domove, false); %disable motion callback
guidata(hObject, handles); %don't forget to save handles
%in callback that can be enabled/disabled, e.g WindowButtonMotionFcn
if handles.domove
%normal code
end %else do nothing

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by