Articles Archive
Articles Search
Director Wiki

User entry key filtering

June 6, 1999
by Pat McClellan

Dear Multimedia Handyman,

I am a system specialist for Xerox Corporation and have been using Director for over 1 year. In the CBT I am building, I have a place where the users enter their employee number. (Which can be from 4 - 6 digits. If it is only 4, then I need the field to "Zero fill" on the left. so the number is 001234. (and only excepts a maximum of six digits - alpha or numerical)

At the end of the CBT, I write out a text file with the score etc. to the users hard drive - to a file. (This is working great) However, I need to change the name of that file - so that it becomes the employee number above, with a number 16 in front of it.

Robert Stock


You'll need to create a keyDownScript to intercept the keystrokes during entry, process them and spit out the right results. Let's start with some assumptions.

Download a sample movie in either Mac or PC format

Start with a behavior attached to the field sprite which assigns the keyDownScript.

on beginSprite me
  set the keyDownScript to "filter"
  put "" into field "empNum"
on endSprite me
  set the keyDownScript to EMPTY

That's it for that behavior. Next you need to write a Movie script handler called "on filter". It needs to check to make sure that only the letter and number keys and BACKSPACE get passed through for entry in the editable field. The ENTER, RETURN, and TAB keys don't get passed to the field, but rather call a handler to process the employee number (we'll get to that shortly.) Everything else is stopped with a stopEvent command.

on filter
  case the key of
    "0", "1", "2", "3", "4", "5", "6", "7", ¬
      "8", "9": pass
      ,"k","l","m": pass
      "x","y","z": pass
    BACKSPACE: pass
    otherwise stopEvent
  end case

When the RETURN, TAB, or ENTER key is pressed, a handler named "processEmpNum" gets called. It puts the entry into a global variable, then checks to see how many chars there are. If there are only 4, it puts "00" in front of it & puts that back into the field. For 5 chars, it puts "0" in front, and again inserts that back into the field. If there are 6 chars, then nothing happens, but if there are any other number, an alert message is called and the field is cleared.

on processEmpNum
  global gEmpNum
  set gEmpNum = the text of field "empNum"
  set howManyChars = the number of ¬
    chars in gEmpNum
  case howManyChars of
      put "00" before gEmpNum
      put gEmpNum into field "empNum"
      put "0" before gEmpNum
      put gEmpNum into field "empNum"
      alert "Invalid employee number. Please try again."
      put "" into field "empNum"
      set gEmpNum to EMPTY
  end case  

At the end of your program, you'll simply need to create the filename based on the global variable gEmpNum. For example, something like this...

global gEmpNum
set fileName = gEmpNum & ".txt"

Good luck with your program.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

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