I have a uieditfield field for text input.
There seem to be at least three ways to indicate to the app that we are finished entering information into this field. The ValueChangedFcn is called when (1) the user presses Enter, (2) the user presses Tab, or (3) the user changes from the app to another program running on the computer.
Is there a way to figure out which of these three events occured? In particular, I'd like to catch (3) and ignore processing the current value of the field for the moment.

 Respuesta aceptada

Antonio Hortal
Antonio Hortal el 24 de En. de 2021
Editada: Antonio Hortal el 24 de En. de 2021
I might be wrong, but you could try getting the CurrentKey property of the figure
function editFieldValueChangedCallback(src,evt)
fig = ancestor(src,'figure');
switch get(fig,'CurrentKey')
case 'return'
disp('enter was pressed!')
otherwise
disp('the user changed focus!')
end

3 comentarios

David Aronstein
David Aronstein el 25 de En. de 2021
Thank you, Antonio! I implemented what you suggested and the result of get(fig, 'CurrentKey') seems to be an empty character (= []) in either the case that Return was pressed or in the case that the text field lost focus.
Hmmm I am using 2020b.. maybe in 2019a the CurrentKey property of the uifigure was not fully supported.
I am using this simple code btw
f = uifigure();
ef = uieditfield(f, 'ValueChangedFcn', @(src,evt) valueChangedCallback(src,evt));
function valueChangedCallback(src,~)
% Find the figure and get the last key pressed when the ValueChangedFcn is triggered
f = ancestor(src,'figure');
key = get(f,'CurrentKey');
switch key
case 'return'
disp('User pressed enter')
otherwise
disp('User changed focus')
end
end
Maybe CurrentCharacter works in 2019a? (Im a bit lazy to check release notes haha)
function valueChangedCallback(src,~)
f = ancestor(src,'figure');
key = get(f,'CurrentCharacter');
switch key
case {char(10),char(13)} % char(10) for windows and (13) for macs I think
disp('User pressed enter')
otherwise
disp('User changed focus')
end
end
If not, you could still attach a WindowKeyPressFcn callback to the uifigure. Both ValueChangedFcn and WindowKeyPressFcn will trigger when you press enter, and you just have to think on some code something so that the 2 callbacks exchange info.
I hope this helps :)
David Aronstein
David Aronstein el 26 de En. de 2021
I beg your pardon, Antonio! I tried your original solution again today in both R2019a and R2020a and it worked perfectly, thank you!! I don't know what I did wrong when I tried this and wrote back yesterday.

Iniciar sesión para comentar.

Más respuestas (1)

Bhargavi Maganuru
Bhargavi Maganuru el 18 de Feb. de 2020
After typing in the edit field, if you click anywhere outside the edit field, editFieldValueChanged callback will be triggered.
Add the following line of code in the callback function.
disp("exited from editfield");
So whenever this message is displayed, user has clicked outside the app.
Hope this helps!

3 comentarios

David Aronstein
David Aronstein el 18 de Feb. de 2020
Thank you, Bhargavi!
I believe the editFieldValueChanged callback will be triggered when any of the following occur:
1) I click outside the edit field
2) I press Enter
3) I press Tab
I was wondering if there is any programmatic way to tell which of these three things occured?
Bhargavi Maganuru
Bhargavi Maganuru el 19 de Feb. de 2020
HI David,
As of now there is no programmatic way to tell which of the events has triggered the callback.
David Aronstein
David Aronstein el 19 de Feb. de 2020
Thank you! That's disappointing.

Iniciar sesión para comentar.

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by