I am trying to implement a search bar in a uifigure based app, for which I am using a uieditfield. The behaviour that I want to achieve is that when I press enter while on the uieditfield, a function is called that takes the value of edit field and does something with it. Using the ValueChangedFcn property of the editfield looked initially like the way to go, but I am finding some issues along the way.
The documentation says that the ValueChangedFcn callback "executes when the user changes text in the edit field and either presses Enter or clicks outside the edit field". Two points outstand from this:
- I want only the search to start on an 'Enter' keypress, and not by clicking outside the searchbar
- If the text has not changed and the user presses the enter button, the function should trigger anyways. Imagine the case in which the user is typing on the searchbar, changes focus to another component (to view an axes for example) and then goes back to the searbar and presses enter to search what he had previously written.
My initial idea was something easy like:
fH = ancestor(src,'figure');
key = get(fH,'CurrentKey');
disp('Execute the search function :D')
If the user presses the Enter key, the function is triggered (in this case, the disp()). If the user clicks outside, nothing happens. So far, so good. But what happens now if the user clicks outside and then reenters the editfield and presses Enter? Nothing because the ValueChangedFcn is not executed if the Value of the editfield has not changed. This is where the problem lies...
I could add a WindowKeyPressFcn to the ancestor figure that checks for an Enter, but that would trigger even if the editfield is not being focused. And, as far as I know, there is currently no way to detect the component being currently focused on Matlab 2020b (>>gco does not work for uifigure based apps and the hidden property of the uifigure 'CurrentObject' is always empty).
All in all, I find the following 2 issues blocking me:
- The ValueChangedFcn is only triggered if the Value property of the edit field has changed. I understand the logics behind this (a value that has not changed does not trigger a ValueChanged listener) but it would be great if there was KeyPressFcn callback on the editfield to register keys even if the Value doesnt change.
- There is no way to detect the component on focus. gco would be the ideal solution but its not there yet (and it looks like in 2021a it wont arrive either)
Does anybody have an alternative solution? Or do you think I have reached a limitation that can't be bypassed?. I have tried going the undocumented way but I have not managed to find a solution
Thanks to anybody that can help :)