callback of multi line edit box

8 次查看(过去 30 天)
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))

采纳的回答

Walter Roberson
Walter Roberson 2022-4-11
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 个评论
Jaeseok
Jaeseok 2022-4-12
I didn't say Mathworks is lying ;-)
Walter Roberson
Walter Roberson 2022-4-12
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by