Creating a Password Field
Learn how to create a password entry field using a a custom font and
start using font. You can download the PasswordEntry.ttf font here.
Password entry fields require that you mask the text in the field so it cannot be read or copied from the field. The easiest way to mask the text in a field is to use a font that draws all characters as bullets. This allows you to still access the text property of the field in order to get the actual password but the user only sees bullets.
Loading The Font
LiveCode enables a developer to load a font at runtime using
start using font. The code below shows how you can load the PasswordEntry.ttf font file that resides in the same directory as a stack file.
on preOpenStack set the itemdelimiter to slash put the effective filename of this stack into theFontFile delete the last item of theFontFile put theFontFile & "/PasswordEntry.ttf" into theFontFile start using font file theFontFile put the result into theError if theError is not empty then answer "Error loading password font:" && theError & "." end if end preOpenStack
After adding this code to your card script, trigger it so that the font is loaded into the LiveCode IDE. If the stack is the topmost stack then run this in the message box:
dispatch "preOpenStack" to this card of this stack
Assign Font To Field
Now that you are loading the font you can assign it as the textFont of the password field. The name of the font is PasswordEntry. Select the font from the mneu or manually type PasswordEntry into the Font field using the Property Inspector.
Filter Input and Don't Allow Copy/Cut
The last thing to do is write some code that prohibits cut/copy and filters input. This example allows characters between ascii numbers 33 and 126. Just put the code in the script of the password field.
on rawKeyDown pKeyCode ## Keypad keycodes for 0 - 9 put "65456,65457,65458,65459,65460,65461,65462,65463,65464,65465" into theKeyPadKeyCodes put "65361,65362,65363,65364" into theArrowKeys put 65288 into theDeleteKeyCode put 65535 into theBackspaceKeyCode put 65289 into theTabKeyCode put 65293 into theReturnKeyCode put "65505,65513" into theModifierKeys # shift, alt/option, put 97 into theAKeyCode put 118 into thePasteKey put the commandkey is "up" and the shiftkey is "up" and the altkey is "up" into noModifiersAreDown put the commandkey is "down" and the shiftkey is "up" and the altkey is "up" into commandModifierIsDown set the wholematches to true if pKeyCode is among the items of theArrowKeys then pass rawKeyDown else if noModifiersAreDown and pKeyCode is among the items of theKeyPadKeyCodes then pass rawkeydown else if noModifiersAreDown and (pKeyCode is theTabKeyCode or pKeyCode is theReturnKeyCode) then pass rawkeydown else if pKeyCode is thePasteKey and commandModifierIsDown then ## paste pass rawkeydown else if pKeyCode is theAKeyCode and commandModifierIsDown then ## select all pass rawkeydown else if (pKeyCode >= 33 and pKeyCode <= 126 or \ pKeyCode is theDeleteKeyCode or pKeyCode is theBackspaceKeyCode) and \ (the commandkey is "up") then pass rawkeydown else if pKeyCode is not among the items of theModifierKeys then ## Shiftkey sends msg on Windows, not Mac beep end if end rawKeyDown on cutKey end cutKey on copyKey end copyKey