Skip to content

Commit

Permalink
Esc now returns empty char
Browse files Browse the repository at this point in the history
  • Loading branch information
serg3y committed May 23, 2023
1 parent dbcc757 commit 52e7621
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 12 additions & 5 deletions uilogin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
%
%Remarks:
%-WindowStyle='modal' blocks user interaction with other UI elements.
%-If Esc is pressed empty char is returned.
%
%Example:
% [user,pass] = uilogin([],'My Program Login')
Expand All @@ -31,7 +32,7 @@
hFig = figure(WindowStyle='modal' ,Position=[(sz(3:4)-[350 100])/2 350 100],Name=lbl,Resize='off',NumberTitle='off',Menubar='none',Color=[0.9 0.9 0.9],CloseRequestFcn=@(~,~)uiresume);
hUser = uicontrol(hFig,Style='edit',Position=[80 60 250 20],KeyPressFcn=@userKeyPress,FontSize=10,BackGroundColor='w',String=user);
hPass = uicontrol(hFig,Style='text',Position=[80 30 250 20],ButtonDownFcn=@passClick,FontSize=10,BackGroundColor='w',Enable='Inactive');
hDumy = uicontrol(hFig,Style='push',Position=[ 0 0 0 0],KeyPressFcn=@KeyPress); %button represents password text box, to simulate tab behaviour
hDumy = uicontrol(hFig,Style='push',Position=[ 0 0 0 0],KeyPressFcn=@passKeyPress); %button represents password text box, to simulate tab behaviour
annotation(hFig,'textbox',Units='pixels',Position=[00 60 80 20],String='Username',EdgeColor='n',VerticalAlignment='middle',HorizontalAlignment='right')
annotation(hFig,'textbox',Units='pixels',Position=[00 30 80 20],String='Password',EdgeColor='n',VerticalAlignment='middle',HorizontalAlignment='right')
pass = ''; %init password
Expand All @@ -45,15 +46,21 @@ function passClick(~,~)
uicontrol(hDumy)
end
function userKeyPress(~,event)
if contains(event.Key,{'return' 'escape'})
uiresume, return %finish
if event.Key=="return"
uiresume, return %done
elseif event.Key=="escape"
hUser.String = ''; pass = '';
uiresume, return %abort
end
end
function KeyPress(~,event)
function passKeyPress(~,event)
if event.Key=="backspace"
pass = pass(1:end-1); %shorten password
elseif contains(event.Key,{'return' 'escape'}) %Enter or ESC was pressed
elseif event.Key=="return"
uiresume, return %done
elseif event.Key=="escape"
hUser.String = ''; pass = '';
uiresume, return %abort
elseif contains(event.Character,num2cell(char(str)))
pass(end+1) = event.Character; %append key to password
end
Expand Down
8 changes: 6 additions & 2 deletions uipassword.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
%
%Remarks:
%-WindowStyle='modal' blocks user interaction with other UI elements.
%-If Esc is pressed empty char is returned.
%
%Example:
% pass = uipassword('0123456789','Enter PIN')
% pass = uipassword('0123456789','Enter PIN')
%
%See also: uilogin, strencrypt

Expand All @@ -34,8 +35,11 @@
function KeyPress(~,event)
if event.Key=="backspace"
pass = pass(1:end-1); %shorten password
elseif contains(event.Key,{'return' 'escape'}) %Enter or ESC was pressed
elseif event.Key=="return"
uiresume, return %done
elseif event.Key=="escape"
pass = '';
uiresume, return %abort
elseif contains(event.Character,num2cell(char(str)))
pass(end+1) = event.Character; %append key to password
end
Expand Down

0 comments on commit 52e7621

Please sign in to comment.