callback of multi line edit box

5 views (last 30 days)
Please run code below,
Callback of left edit box responds to enter key while right one doesn't.
Once edit box set as multi-line, callback doest not be triggered by 'enter' key.
How can I get multi-line edit box which response to enter key.
hfig = figure('OuterPosition', [100,100,300,200]);
hedit_left = uicontrol('Style','edit', 'Units','normalized','Position',[0, 0,0.5,1]);
hedit_right = uicontrol('Style','edit','Max',2,'Units','normalized','Position',[0.5,0,0.5,1]);
set(hedit_left, 'Callback', @(src,evt)disp(src.String))
set(hedit_right, 'Callback', @(src,evt)disp(src.String))

Accepted Answer

Walter Roberson
Walter Roberson on 11 Apr 2022
This is not inconsistent with the documentation -- which does not explicitly say that enter will trigger the callback.
The edit text box accepts multiple lines of input when Max – Min > 1. Otherwise, the edit text box accepts a single line of input.
Notice there is no discussion about the way in which users enter multiple lines.
I suspect that Mathworks decided it was more sensible to allow the user to use enter to finish individual lines, instead of requiring that the user down-arrow or shift-enter (neither of which are as obvious or natural.)
I think most of the time it does not make sense to have a callback for every line entered -- especially since the user is free to navigate around so enter does not have a one-to-one correspondance with "new data entered".
I think for your purposes you might need to switch to KeyPressFcn callbacks.
  2 Comments
Jaeseok
Jaeseok on 12 Apr 2022
I didn't say Mathworks is lying ;-)
Walter Roberson
Walter Roberson on 12 Apr 2022
You will need to switch to KeyPressFcn and examine the individual key presses.
Note: when you are using traditional figures like you are, then if you get the Value of an edit field in a KeyPressFcn, then it will not include any of the changes since the last time that the Callback function would have been called. The Value property for uicontrol style edit of traditional figures is not updated until the control loses focus (multiline) or loses focus or enter is pressed (single line). You need to keep track of the individual presses yourself, and model which changes are being made as the user moves around (arrows or clicks) or presses delete.
The newer ui edit fields for uifigures have a value changing callback that can access current contents.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!