Created
September 8, 2010 19:18
-
-
Save anonymous/570659 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit PassText; | |
interface | |
procedure ConsoleGetPassword (const caption: String; var Password : string); | |
implementation | |
uses | |
SysUtils, Windows; | |
var | |
consoleHandle: THandle; | |
ConsoleAttr: _CONSOLE_SCREEN_BUFFER_INFO; | |
function EnableEcho(const ShowText: Boolean): Cardinal; | |
begin | |
GetConsoleMode(consoleHandle, Result); | |
if ShowText then | |
SetConsoleMode(consoleHandle, Result or ENABLE_ECHO_INPUT) | |
else | |
SetConsoleMode(consoleHandle, Result and not ENABLE_ECHO_INPUT); | |
end; | |
function KeyPressed: Char; | |
var | |
oldMode: Cardinal; | |
bufSize: Cardinal; | |
begin | |
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), OldMode); | |
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode and not | |
(ENABLE_LINE_INPUT or ENABLE_ECHO_INPUT)); | |
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), @Result, 1, bufSize, | |
nil); | |
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode); | |
end; | |
procedure GotoXY(X, Y: Word); | |
var | |
pos: COORD; | |
begin | |
pos.X := X; | |
pos.Y := Y; | |
SetConsoleCursorPosition(STD_INPUT_HANDLE, pos); | |
end; | |
function GetPassword(const caption: String; const MaskChar: Char = '*'): string; | |
var | |
c: Char; | |
begin | |
Write(caption); | |
EnableEcho(False); | |
while True do | |
begin | |
c := KeyPressed(); | |
case c of | |
#13: Break; | |
#8: | |
begin | |
if Length(Result) > 0 then | |
begin | |
Delete(Result, Length(Result), 1); | |
GotoXY(ConsoleAttr.dwCursorPosition.X - 1, | |
ConsoleAttr.dwCursorPosition.Y); | |
Write(' '); | |
GotoXY(ConsoleAttr.dwCursorPosition.X - 1, | |
ConsoleAttr.dwCursorPosition.Y); | |
end; | |
end; | |
else | |
Result := Result + c; | |
Write(MaskChar); | |
end; | |
end; | |
EnableEcho(True); | |
end; | |
// main | |
procedure ConsoleGetPassword(const caption: String; var Password : string); | |
begin | |
consoleHandle := GetStdHandle(STD_OUTPUT_HANDLE); | |
GetConsoleScreenBufferInfo(consoleHandle, ConsoleAttr); | |
Password := GetPassword(caption); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment