MATLAB: Can I execute a specific callback process when I lose the focus of edit text

MATLAB

On the Uicontrol's editable text box, I would like to perform specific callback at the timing when the mouse is moved to another object. I would like to know if I can detect that the focus shifted.

Best Answer

The callback of the editable text box will be executed when
· the focus moves to another component from the edit text
· with edit text in focus, the enter key (Enter + Ctrl in case of multiple line input support) is entered
There is no callback function that operates only when the focus shifts, but as an alternative, you can use the way which identifying the last key entered in the Callback function. If it is not the "Enter" key, you can define that the focus has shifted.
The sample code below is an example.
In the editable text box Callback function, identify the last entered key using the Figure's CurrentCharacter property. This will change the processing of the callback when pressing the Enter key in the editable text box and moving the focus.
 
(edit_off_samp.m)
function edit_off_samp
h_figure = figure;
h_edit = uicontrol('Style','edit','Callback',@edit_callback); % create editbox
function edit_callback(src,event)
% define callback
CK = get(h_figure, 'CurrentCharacter'); % get the last key entered
if isequal(double(CK),13) % If it was "Enter" key
disp('Pressed Enter Key')
else
disp('Focus off')
end
end
end