Lingo in a Nutshell and Director in a Nutshell banner
Chapter 10 - Keyboard Events

Lingo in a Nutshell

Corrections to Example 10-9: Password Entry Field on page 290.

The password example didn't correctly handle the delete key. It would delete the asterisks, but not the underlying password. Also, it wouldn't properly clear the password following an erroneous attempt to log in. Here is a corrected version that addresses these issues:


property pPassword

property pMyMember



on beginSprite me

  set pMyMember = the member of sprite (the spriteNum of me)

  -- Clear the password field

  set pPassword = EMPTY

  put EMPTY into member pMyMember

end



on keyDown me

  -- Mask the field with asterisks.

  set maskChar = "*"



  if the key = RETURN or the key = ENTER then

    -- Check the password when they hit RETURN or ENTER

    if verifyPassword (pPassword) = TRUE then

      -- Use this to jump to an appropriate frame in your movie

      go frame "Top Secret"

    else

      alert "Invalid Password"

      -- This is NEW to clear the field when the user entered

      -- the wrong password

      set pPassword = EMPTY

      put EMPTY into member pMyMember

    end if



  else if isArrowOrTabKey() then

    -- This is changed from the book. Allow the arrow or tab keys through.

    pass



  else if isDeleteKey() then

    -- Handle the delete and backspace keys specially

    if the selStart = 0 and the selEnd = 0 then

      -- If the cursor is already at the left edge, there is nothing

      -- to delete, so beep at the user.

      beep

      exit

    end if



    set insertPoint = max(1, the selStart)

    set endPoint    = max(1, the selEnd)



    -- Delete the hilighted characters or the character to the left

    -- of the insertion point

    if insertPoint = endPoint then

      delete char insertPoint of pPassword

    else

      delete char insertPoint to endPoint of pPassword

    end if



    pass



  else

    -- Determine which portion of the field to replace

    set insertPoint = max(1, the selStart + 1)

    set endPoint = max(1, the selEnd)



    -- Insert the text at the cursor location

    if insertPoint = endPoint then

      -- In this case, no characters are hilighted.

      -- Add the keystroke to the secret password

      put the key before char insertPoint of pPassword

      -- But display the masking character (asterisk) in its place

      put maskChar before char insertPoint of member pMyMember

    else

      -- In this case, replace all the hilighted characters

      put the key into char insertPoint to endPoint of pPassword

      put maskChar into char insertPoint to endPoint of member pMyMember

    end if

    -- Update the cursor insertion point manually

    set the selEnd = insertPoint

    set the selStart = insertPoint

  end if

end keyDown





-- This handler is new. It detects the delete key, backspace key or 

-- keypad delete key. It replaces the "isEditingKey" handler from

-- the example which is no longer needed in this context

on isDeleteKey

  case (the key) of

    BACKSPACE:

      return TRUE

  end case



  case (the keyCode) of

    51, 117:

      return TRUE

    otherwise:

      return FALSE

  end case

end isDeleteKey





-- This handler is new. It detects the arrow keys and the Tab key

on isArrowOrTabKey

  case (the key) of

    TAB:

      return TRUE

  end case



  case (the keyCode) of

    123, 124, 125, 126:

      return TRUE

    otherwise:

      return FALSE

  end case

end isArrowOrTabKey





-- Verify the password

on verifyPassword password

  -- Store your password here

  if password = "platypus" then

    return TRUE

  else

    return FALSE

  end if

end verifyPassword

 



Zeus Home Page | LIAN TOC | DIAN TOC | Links | E-Mail

Place an Order | Downloads | FAQ | GuestBook | Glossary

[End of Page]

Copyright © 1996-1999 Bruce A. Epstein. All Rights Reserved.

(The page last revised November 4, 1999)