Click escape to stop my MouseDownfcn in the GUI?
    9 views (last 30 days)
  
       Show older comments
    
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
0 Comments
Answers (1)
  Guillaume
      
      
 on 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
0 Comments
See Also
Categories
				Find more on Graphics Object Properties 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!