Making Enter trigger a button in a .mlapp dialog
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on App Building 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!