Pull String out of Edit Text without user hitting enter
Show older comments
Hi,
I am building a GUI with an EditText box and I want to pull the String out of it in my program, essentially as follows:
s = get(handles.edittext, 'String');
However, I want to get whatever the user currently has typed in there, which appears only to update after the user hits enter, etc.
I have tried updating the edittext box programmatically by calling its callback, but it still does not give the currently typed String (it gives the prior-updated string). Example:
edittext_Callback(handles.edittext, [], handles);
I have also tried updating the focus using the 'uicontrol' function. Example:
uicontrol(handles.edittext);
But again, it only gives the string that was in the box when the user caused it to update.
I am at a loss. Is there any way to snag what the user currently has typed in the EditText without the user having to cause it to update manually?
Accepted Answer
More Answers (2)
Jim Hokanson
on 1 Apr 2013
Here's another approach involving changing control focus.
Here's some relevant documentation, with summaries for 2013a:
edit text boxes don't update their strings until you click somewhere else, press enter (single line) or ctl+enter (multiline)
huh?
for multiline editable text boxes, the string property is set when the control loses focus
oh!
With some help from the always helpful Jan:
My approach involves setting the focus to a small text box and then setting the focus back. I haven't played with the extent to which the static text box needs to be visible.
uicontrol(obj.h.static__busy_status) %Set focus to another control
uicontrol(obj.h.edit__cmd_window) %Set it back
get(obj.h.edit__cmd_window,'String') %Returns updated string
For me with 2013a the focus switch is imperceptible, returning the cursor to where I was typing without any noticeable blinking.
1 Comment
Bhushan N
on 1 Dec 2016
Jim,
This kind of works cause when the focus returns to edit text box, it selects all the text. So if you are typing like normal it keeps replacing the text.
Bhushan
Pooya89
on 17 Nov 2013
Using Yair's FingJObj and by accessing the java properties it is possible: (As he explains in this example)
jEditbox = findjobj(hEditbox); % get java object
set(jEditbox, 'KeyPressedCallback', @Key_Press);
function Key_Press(jEditbox, eventData)
char(jEditbox.getText)
end
Categories
Find more on String Parsing 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!