home *** CD-ROM | disk | FTP | other *** search
- unit pw;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit1KeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
- const
- password : string = '';
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- ShowMessage( 'Password = ' + password );
- end;
-
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- { Notes:
- 1) the index of the character to be deleted when
- backspace (#8) is pressed is calculated using the
- current cursor pos in Edit1. This, of course,
- requires that the number of characters in Edit1 is
- the same as in password (as it is in this program).
-
- 2) the password is not built up by appending Key to password, e.g.
- password := password + Key;
- Instead a complicated char-insertion routine is used:
- password := Copy(password,1,Edit1.SelStart) +
- Key +
- Copy(password,Edit1.SelStart, Length(password)-Edit1.SelStart);
- This is to allow for the possibility that the user may have
- moved the insertion point by clicking the mouse at a new position.
- }
- begin
- if Key = #8 then // backspace
- begin // delete char from password matching current cursor pos
- if Length(password) > 0 then // (selStart) in edit box
- begin
- if Edit1.selLength = 0 then
- Delete(password, Edit1.selStart, 1)
- else // delete several selected chars if necessary
- Delete(password, Edit1.selStart+1, Edit1.selLength);
- end;
- end
- else
- if Key in ['a'..'z', 'A'..'Z', '0'..'9'] then
- begin // insert new char at current cursor position
- password := Copy(password,1,Edit1.SelStart) +
- Key +
- Copy(password,Edit1.SelStart, Length(password)-Edit1.SelStart);
- Key := '*';
- end
- else Key := #0;
- Caption := password;
- end;
-
- procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- { Disallow non-alphanumeric chars such as Home and End }
- if not (Chr(Key) in ['a'..'z', 'A'..'Z', '0'..'9'] ) then
- Key := 0;
- end;
-
- end.
-