Creating a Password Field
The Goal
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.
Loading The Font
Revolution enables a developer to load a font at runtime using revFontLoad. The code below shows how you can load the PasswordEntry.ttf font file that resides in the same directory as a stack file.
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 "Password Entry". Manually type "Password Entry" 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 "65438,65436,65433,65435,65430,65437,65432,65429,65431,65434" 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 the commandkey is "up" and the shiftkey is "up" and the altkey is "up" into noModifiersAreDown
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 theAKeyCode and the commandkey is down) and \
not (the altkey is down and not the shiftkey is down) then ## select all
pass rawkeydown
else if (pKeyCode >= 33 and pKeyCode <= 126 or \
pKeyCode is theDeleteKeyCode or pKeyCode is theBackspaceKeyCode) and \
(the altkey is "up" 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
Comments (0)
Add your comment