Articles Archive
Articles Search
Director Wiki
 

Limiting text input

July 19, 2000
by Gary Rosenzweig

A common situation that I run into is that I want to limit the information that users can type into an editable text member. Suppose, for instance, that you want the user to enter their name. What you expect them to do is enter a few letters. However, some users will discover that they can enter all sorts of characters, including characters created with the Alt key in Windows or the Option key on the Mac. If this name will later be transmitted through the Internet as data, you will want to ensure it doesn't contain certain characters that you might use to define data, such as ampersands or percent signs.

Limiting what the user can type is easy with a little bit of Lingo. All you need to do is capture the keystrokes as they occur, test them, and only pass the good ones along to the text member. Here is a movie that does this:

A Director 7 sample movie is available for download in Mac or PC format.

The point of interception happens at on keyDown. Here, you can get the key press by using the key property. Then you can test it. I like to do this by using the contains function. For example, if I want to see if a character, k, is a letter, I do this:

if "abcdefghijklmnopqrstuvwxyz" contains k then

By using contains, I am automatically testing for both upper and lower case letters, since contains is not case sensitive. I can also test for numbers, spaces and punctuation marks simply by adding these characters to the end of the long string above.

Another thing I want to test for is a backspace key. This is represented in Director by the BACKSPACE constant. I can append that to the long string as well.

Once I have tested the keystroke, I want to let it go into the text member if it is an allowed character. I can do this with the pass command. On the other hand, if the character is no good, I can use the stopEvent command to kill the key press event before it reaches the text member. Here is the complete handler.

on keyDown me

  -- define keys allowed
  keyAllowed = "abcdefghijklmnopqrstuvwxyz0123456789-. " & BACKSPACE

  -- check the key pressed
  k = the key

  -- see if it is an allowed character
  if keyAllowed contains k then
    -- allow it
    pass
    
  else if k = RETURN then
    inputText = sprite(me.spriteNum).member.text
    inputHandler(inputText)
    
  else
    -- kill it
    stopEvent
  end if

end

One more thing that I added to this handler is the recognition of the return key. By comparing k with the RETURN constant, I can capture the return key on the Mac and the main keyboard Enter key in Windows. A typical computer user may expect this to perform an action, such as testing or sending the typed data somewhere. Most Director movies, however, do nothing with a return; or worse, they accept it and allow the text member to go to a second line of text entry, even though only one line is intended.

In the handler above, I capture the return and use it to trigger an action. The text of the member is placed in a variable and then sent to another handler. In this sample movie, it is just an alert box. However, in your movie it can be the final action on the screen, or perhaps an action that moves the text focus to the next editable text member.

One final touch is a small on beginSprite handler that clears the contents of the text member. This will ensure that the editable text is empty when the user gets to it. Otherwise, it may contain the last thing you tested in the text member before you built the projector or made the Shockwave file.

on beginSprite me
  sprite(me.spriteNum).member.text = ""
end

If you look at the sample movie, you might also see something else that is different from most Director movies. The text is offset from the left, top and bottom by a few pixels. I did this by choosing Modify, Paragraph and setting the before and after spacing to 3 pixels and the left and right margins to 0.05 inches. If you change your preferences so that text members use inches as units, it is the equivalent to 3 pixels. This extra margin is very important if you want a professional-looking movie, and many developers don't do it.

Gary Rosenzweig's latest book is "Advanced Lingo for Games." In it, you can find the source code for more than 20 complete games. More information about the book can be found at http://clevermedia.com/resources/bookstore/book4.html. It can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.