Making Enter trigger a button in a .mlapp dialog

3 vues (au cours des 30 derniers jours)
Ken
Ken le 25 Avr 2024
Commenté : Jorik Caljouw le 12 Mar 2025
I am using the multi-window app facility (documented here) to create a dialog box a little fancier than the ones intrinsically provided. It provides an edit field in which the user may type a file name. I would like to arrange it so that if the user presses Enter after typing the file name, it triggers the Load button. I find that, if the keyboard focus is in the dialog generally, a KeyPressFcn callback for that dialog will be triggered. But if the keyboard focus is in a specific edit field, it won't. But that is precisely what I want! Is there a way?
function createComponents(app)
% Create MainDialog and hide until all components are created
app.MainDialog = uifigure('Visible', 'off');
app.MainDialog.Position = [100 100 879 218];
app.MainDialog.Name = 'MATLAB App';
app.MainDialog.KeyPressFcn = createCallbackFcn(app, @LoadSaveButtonPushed, true);
% Create TitleLabel
app.TitleLabel = uilabel(app.MainDialog);
app.TitleLabel.HorizontalAlignment = 'center';
app.TitleLabel.FontSize = 24;
app.TitleLabel.Position = [364 164 154 32];
app.TitleLabel.Text = 'Load Settings';
% Create FileLabel
app.FileLabel = uilabel(app.MainDialog);
app.FileLabel.HorizontalAlignment = 'right';
app.FileLabel.Position = [68 102 27 22];
app.FileLabel.Text = 'File:';
% Create FileEditField
app.FileEditField = uieditfield(app.MainDialog, 'text');
app.FileEditField.Position = [110 102 622 22];
% Create LoadSaveButton
app.LoadSaveButton = uibutton(app.MainDialog, 'push');
app.LoadSaveButton.ButtonPushedFcn = createCallbackFcn(app, @LoadSaveButtonPushed, true);
app.LoadSaveButton.FontSize = 18;
app.LoadSaveButton.Position = [694 27 110 38];
app.LoadSaveButton.Text = 'Load';
% Create BrowseButton
app.BrowseButton = uibutton(app.MainDialog, 'push');
app.BrowseButton.ButtonPushedFcn = createCallbackFcn(app, @BrowseButtonPushed, true);
app.BrowseButton.Position = [763 102 100 23];
app.BrowseButton.Text = 'Browse...';
% Create CancelButton
app.CancelButton = uibutton(app.MainDialog, 'push');
app.CancelButton.ButtonPushedFcn = createCallbackFcn(app, @CancelButtonPushed, true);
app.CancelButton.FontSize = 18;
app.CancelButton.Position = [94 27 110 38];
app.CancelButton.Text = 'Cancel';
% Show the figure after all components are created
app.MainDialog.Visible = 'on';
end

Réponse acceptée

Malay Agarwal
Malay Agarwal le 26 Avr 2024
Modifié(e) : Malay Agarwal le 26 Avr 2024
Hi Ken,
I understand that you want to trigger the “Load” button when you press the enter key after changing the text in an edit field.
This can be done by attaching a “ValueChangedFcn” callback to the edit field: https://www.mathworks.com/help/releases/R2023b/matlab/ref/matlab.ui.control.editfield-properties.html#buh_e24-58_sep_shared-ValueChangedFcn. For example, using the example in the documentation, the following code triggers the “OK” button when the enter key is pressed after editing the “Sample Size” edit field:
function EditFieldValueChanged(app, event)
ButtonPushed(app, event);
end
Note that this callback is triggered either when you press the enter key or when you click outside the edit field, which means the “Load” button might unintentionally be triggered if you click outside the edit field. To make it trigger only when the enter key is pressed, you can use the following:
function EditFieldValueChanged(app, event)
key = get(app.UIFigure, 'CurrentKey');
switch key
case 'return'
LoadSaveButtonPushed(app, event);
end
end
You can then attach this to the edit field as follows:
app.FileEditField.ValueChangedFcn = createCallbackFcn(app, @EditFieldValueChanged, true);
Hope this helps!
  2 commentaires
Ken
Ken le 26 Avr 2024
Wow, that's a neat trick! I'll use it. Just curious, where in the documentation would you go to learn things like that?
Jorik Caljouw
Jorik Caljouw le 12 Mar 2025
That is indeed great. I was looking for the same thing and am glad I found it here, but I must say, I would've expected the App Designer framework to allow something like this more easily.
To me it seems very standard, that you do want certain actions to happen when pressing the Enter/Return key or when pushing a button, but NOT when simply moving out of an Edit field by keyboard or mouse.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Tags

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by