Skip to content

Commit 6d1560d

Browse files
committed
Use event 'FocusOnInputField' for R2023a and newer
1 parent 40af6ec commit 6d1560d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

widgets/+wt/PasswordField.m

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,20 @@ function onPasswordChanged(obj,evt)
136136

137137
function focus(obj)
138138

139-
% Gets keyboard focus to the UI component and brings parent figure to front
139+
% Brings parent figure to front
140140
focus(obj.PasswordControl)
141141

142-
% Setting 'DoFocus' to TRUE will trigger DataChanged event in HTML
143-
% component, selecting the Input Field.
144-
% The HTML component reverts 'DoFocus' back to FALSE.
145-
obj.PasswordControl.Data.DoFocus = true;
142+
% What MATLAB version?
143+
if ~isMATLABReleaseOlderThan("R2023a")
144+
% Fire event 'FocusOnInputField' that will trigger the HTML
145+
% component to select the Input Field directly.
146+
sendEventToHTMLSource(obj.PasswordControl, 'FocusOnInputField', '')
147+
else
148+
% Setting 'DoFocus' to TRUE will trigger DataChanged event in HTML
149+
% component, selecting the Input Field.
150+
% The HTML component reverts 'DoFocus' back to FALSE.
151+
obj.PasswordControl.Data.DoFocus = true;
152+
end
146153

147154
end %function
148155

@@ -162,6 +169,9 @@ function focus(obj)
162169
' // Code response to data changes in MATLAB '
163170
' htmlComponent.addEventListener("DataChanged", dataFromMATLABToHTML); '
164171
' '
172+
' // Code response to FocusOnInputField event in MATLAB '
173+
' htmlComponent.addEventListener("FocusOnInputField", focusInputField); '
174+
' '
165175
' // Update the Data property of the htmlComponent object '
166176
' // This action also updates the Data property of the MATLAB HTML object'
167177
' // and triggers the DataChangedFcn callback function '
@@ -192,6 +202,16 @@ function focus(obj)
192202
' } '
193203
' } '
194204
' '
205+
' function focusInputField(event) { '
206+
' let domValue = document.getElementById("value"); '
207+
' '
208+
' // Focus on the input element '
209+
' if (domValue) { '
210+
' domValue.focus(); '
211+
' domValue.select(); '
212+
' } '
213+
' } '
214+
' '
195215
' function dataFromHTMLToMATLAB(event) { '
196216
' let newValue = event.target.value; '
197217
' let newData = htmlComponent.Data; '

0 commit comments

Comments
 (0)