my editbox does not update to '*' when I type my login password, the same code run in matlab 2015a mask my password to '*' but not in 2019a

6 ビュー (過去 30 日間)
Zeshan Ali
Zeshan Ali 2022 年 5 月 20 日
回答済み: Rahul 2025 年 6 月 16 日
function Password_KeyPressFcn(hObject, eventdata, handles)
global pass;
pass = get(handles.Password,'UserData');
v = double(get(handles.figure1,'CurrentCharacter'));
if v == 8
pass = pass(1:end-1);
set(handles.Password,'string',pass)
elseif any(v == 65:90) || any(v == 97:122) || any(v == 48:57)
pass = [pass char(v)];
else
msgbox({'Invalid Password Character';'Can''t use Special Character'},'warn','modal')
return
end
set(handles.Password,'UserData',pass);
set(handles.Password,'String',char('*'*sign(pass)));

回答 (1 件)

Rahul
Rahul 2025 年 6 月 16 日
I understand that you wish to implement the Password functionality to an Edit Field in GUIDE.
According to my understand in MATLAB 2019a the 'KeyPressFcn' does not work similarly to the previous versions. Hence you can make use of the 'evenData.key' and 'eventData.Character' to obtain the key pressed and the character typed in the Edit Field.
Here is an example of some changes:
function Password_KeyPressFcn(hObject, eventdata, handles)
persistent pass
if isempty(pass)
pass = '';
end
% Handle Backspace
if strcmp(eventdata.Key, 'backspace')
if ~isempty(pass)
pass = pass(1:end-1);
end
% Handle regular characters (letters and numbers)
elseif ~isempty(eventdata.Character) && ...
(ismember(eventdata.Character, ['a':'z', 'A':'Z', '0':'9']))
pass = [pass eventdata.Character];
else
msgbox({'Invalid Password Character';'Can''t use Special Character'},'warn','modal')
return;
end
% Store and mask
set(handles.Password, 'UserData', pass);
set(handles.Password, 'String', repmat('*', 1, numel(pass)));
end
The following MATLAB Answer can also be referred:
The following MathWorks documentations can be referred to know more:
Thanks.

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by