Microsoft Small Basic

Program Listing: TGH751
Example:
TextWindow.Write(LF + "Enter Password -> ")

HIPWMax = 15 ' Max # of accepted typed chars
HiddenCLIInput() ' Calls Hidden Password Input subroutine
Password = HIPWStr ' Copies result

TextWindow.WriteLine(LF + Password) ' Exhibits what was once hidden from view
Goto Example

'==============================================================='
' Hidden CLI Password Input '
'==============================================================='

Sub HiddenCLIInput
'--- Accepts an input from the user w/o displaying what is being typed
'--- Returns HIPWStr as the string typed
'--- Accepts HIPWMax as maximum # of chars which HIPWStr can have

SpecialCharsHeader()
HiddenInputInit()
HiddenInputLoop()

EndSub
'------------------------------------------------------------------------------------------------------------'
Sub HiddenInputInit

InitCursorX = TextWindow.CursorLeft
InitCursorY = TextWindow.CursorTop

If HIPWMax <= 0 Then ' Max # of chars which can be accepted from user
HIPWMax = 255 ' If HIPWMax is undetermined, use default 255
EndIf

HIPWKey = "" ' Typed char to be analysed
HIPWStr = "" ' Password result formed outta accepted HIPWKey chars
HiddenChars = "" ' Which HIPWStrDisplay derives from
HiddenChar = STAR ' Char displayed in place of what is being typed
EnterKey = CR ' Key to confirm the input
DeleteKey = BS ' Key to delete last char stored in HIPWStr

For num = 1 To HIPWMax
HiddenChars = HiddenChars + HiddenChar
EndFor

EndSub
'------------------------------------------------------------------------------------------------------------'
Sub HiddenInputLoop

While HIPWKey <> EnterKey
HIPWKey = TextWindow.ReadKey() ' Waits next key to be hit
HiddenInputParse() ' Decides if it can be accept
HiddenInputHide() ' Replaces typed by HiddenChars
EndWhile

EndSub
'------------------------------------------------------------------------------------------------------------'
Sub HiddenInputParse

HIPWKeyCode = Text.GetCharacterCode(HIPWKey)
If HIPWKeyCode >= 32 Then ' Tests if printable
HIPWStr = Text.Append(HIPWStr HIPWKey) ' Creates PW string
EndIf

HIPWStrLen = Text.GetLength(HIPWStr)
If HIPWStrLen > HIPWMax Then ' Checks if size passed limit
HIPWKey = DeleteKey
EndIf

If HIPWKey = DeleteKey Then ' Deletes last char typed
TextWindow.Write(SPC + BS + BS + SPC + BS + BEL)
HIPWStr = Text.GetSubText(HIPWStr 1 HIPWStrLen-1)
EndIf

EndSub
'------------------------------------------------------------------------------------------------------------'
Sub HiddenInputHide

HIPWStrLen = Text.GetLength(HIPWStr)
HIPWStrDisplay = Text.GetSubText(HiddenChars 1 HIPWStrLen)

TextWindow.CursorLeft = InitCursorX ' Repositions cursor at the beginning
TextWindow.CursorTop = InitCursorY
TextWindow.Write(HIPWStrDisplay) ' to display HiddenChars

EndSub
'------------------------------------------------------------------------------------------------------------'

'==============================================================='
' Constant Header Includes '
'==============================================================='

Sub SpecialCharsHeader
' --- Initializes constants for special control characters in ASCII code:

BEL= Text.GetCharacter ( 7) ' Makes internal PC-Speaker buzz
BS= Text.GetCharacter ( 8) ' Backspace
TAB= Text.GetCharacter ( 9) ' Horizontal Tabulation (HT)
LF= Text.GetCharacter (10) ' Line-Feed (jumps a line)
CR= Text.GetCharacter (13) ' Carriage Return
ESC= Text.GetCharacter (27) ' Escape Key
CRS= Text.GetCharacter (22) ' Cursor
ARW= Text.GetCharacter (26) ' Right Arrow
QUO= Text.GetCharacter (34) ' Double Quotation Mark
SQU= Text.GetCharacter (166) ' Half Vertical Rectangle
APO= "'" ' Apostrophe
DOT= "." ' Period
COMMA= "," ' Comma
STAR= "*" ' Asterisk
SPC= " " ' SPaCe

EndSub
'------------------------------------------------------------------------------------------------------------'