-
Notifications
You must be signed in to change notification settings - Fork 1
/
passcode.m
105 lines (104 loc) · 3.15 KB
/
passcode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function pass = passcode(charset);
%PASSCODE password input dialog box.
% passcode creates a modal dialog box that returns user password input.
% Given characters are substituted with '*'-Signs like in usual Windows dialogs.
if nargin==0
charset = default;
else
if any(strcmp({'letter' 'digit'},charset))
charset = eval(charset);
elseif ~isa(charset,'char')
error('string expected. Check input parameters.')
end% if
end% if
%
ScreenSize = get(0,'ScreenSize');
hfig = figure('Menubar','none', ...
'Units','Pixels', ...
'Resize','off', ...
'NumberTitle','off', ...
'Name',['password required'], ...
'Position',[ (ScreenSize(3:4)-[300 75])/2 300 75], ...
'Color',[0.8 0.8 0.8], ...
'WindowStyle','modal');
hedit = uicontrol('Parent',hfig, ...
'Style','Edit', ...
'Enable','inactive', ...
'Units','Pixels','Position',[49 28 202 22], ...
'FontSize',15, ...
'String',[], ...
'BackGroundColor',[0.7 0.7 0.7]);
hpass = uicontrol('Parent',hfig, ...
'Style','Text', ...
'Tag','password', ...
'Units','Pixels','Position',[51 30 198 18], ...
'FontSize',15, ...
'BackGroundColor',[1 1 1]);
hwarn = uicontrol('Parent',hfig, ...
'Style','Text', ...
'Tag','error', ...
'Units','Pixels','Position',[50 2 200 20], ...
'FontSize',8, ...
'String','character not allowed',...
'Visible','off',...
'ForeGroundColor',[1 0 0], ...
'BackGroundColor',[0.8 0.8 0.8]);
%
set(hfig,'KeyPressFcn',{@keypress_Callback,hedit,hpass,hwarn,charset}, ...
'CloseRequestFcn','uiresume')
%
uiwait
pass = get(hpass,'userdata');
delete(hfig)
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function keypress_Callback(hObj,data,hedit,hpass,hwarn,charset)
%--------------------------------------------------------------------------
pass = get(hpass,'userdata');
%
switch data.Key
case 'backspace'
pass = pass(1:end-1);
%
case 'return'
uiresume
return
%
otherwise
try
if any(charset == data.Character)
pass = [pass data.Character];
else
set(hwarn,'Visible','on')
pause(0.5)
set(hwarn,'Visible','off')
end% if
end% try
%
end% switch
%
set(hpass,'userdata',pass)
set(hpass,'String',char('*'*sign(pass)))
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function charset = default
%--------------------------------------------------------------------------
% charset = [letter digit '<>[]{}()@!?*#=~-+_.,;:§$%&/|\'];
charset = char(1:255);
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function charset = digit
%--------------------------------------------------------------------------
charset = '0123456789';
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function charset = letter
%--------------------------------------------------------------------------
charset = char([65:90 97:122]);
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% I LOVE MATLAB %%%