Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Pull String out of Edit Text without user hitting enter

Asked by Michael on 7 Jun 2011
Latest activity Answered by Pooya89 on 17 Nov 2013

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?

0 Comments

Michael

Products

No products are associated with this question.

3 Answers

Answer by Matt Fig on 7 Jun 2011
Accepted answer

The only way I know to do this (that doesn't involve Java), is to capture the user's input using the keypressfcn. As the user types, store the string using the keypressfcn and concatenation, then if the user does hit return clear the stored variable from the callback.

Here is a small example. You may want to add more checks, but I tried to cover enough to make it functional.

function [] = gui_keypress()
% Typing in the editbox puts the string in a textbox.
S.fh = figure('units','pixels',...
              'position',[500 500 200 100],...
              'menubar','none',...
              'name','keypress',...
              'numbertitle','off',...
              'resize','off');        
S.ed = uicontrol('style','edit',...
                 'unit','pix',...
                 'position',[10 10 180 30],...
                 'fontsize',14,...
                 'callback',@ed_call,...
                 'keypressfcn',@ed_kpfcn);
S.tx = uicontrol('style','text',...
                 'units','pix',...
                 'position',[10 60 180 30]);
S.STR = [];             
guidata(S.fh,S); 
uicontrol(S.ed)
function [] = ed_call(H,E)
% Callback for editbox.
S = guidata(gcbf);  % Get the structure.
set(S.tx,'str',get(S.ed,'string'));  
S.STR = [];
guidata(gcbf,S)
function [] = ed_kpfcn(H,E)
% Keypressfcn for editbox
S = guidata(gcbf);  % Get the structure.
if strcmp(E.Key,'backspace')
    S.STR = S.STR(1:end-1);
elseif isempty(E.Character)
    return
else
    S.STR = [S.STR E.Character];
end
set(S.tx,'string',S.STR)
guidata(gcbf,S)

3 Comments

Michael on 7 Jun 2011

Thanks, I think I can get that to work actually.

However, I need to use the text I'm capturing and there's a small problem:

I tried concatenating all the characters, but the invisible characters (backspace, etc.) are there too. Is there a relatively painless way to clean up the string?

Example of problem: I type 'this', then hit backspace twice, then type 'at' for a visible result of 'that'. However, the total length of the string is 8 characters because of 8 total key presses.

I need to be able to test just the visible components of the string against other strings.

Much appreciated :)

Matt Fig on 7 Jun 2011

You're welcome!

Matt Fig on 7 Jun 2011

I changed the engine to make it more robust than my first post...

Matt Fig
Answer by Jim Hokanson on 1 Apr 2013

Here's another approach involving changing control focus.

Here's some relevant documentation, with summaries for 2013a:

http://www.mathworks.com/help/matlab/ref/uicontrol.html

edit text boxes don't update their strings until you click somewhere else, press enter (single line) or ctl+enter (multiline)

huh?

http://www.mathworks.com/help/matlab/ref/uicontrol_props.html

for multiline editable text boxes, the string property is set when the control loses focus

oh!

With some help from the always helpful Jan:

http://www.mathworks.com/matlabcentral/answers/26951

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.

0 Comments

Jim Hokanson
Answer by 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

0 Comments

Pooya89

Contact us