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
Robert,
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.
- your entry field is an editable field member named "empNum"
- you only want to allow keys 0-9 and a-z as entry
- only entries of 4, 5 or 6 chars is valid
- a ENTER or RETURN or TAB will complete the entry procedure
- if the number is not valid, an alert message will appear and the field will be cleared
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" end on endSprite me set the keyDownScript to EMPTY end
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 "a","b","c","d","e","f","g","h","i","j"¬ ,"k","l","m": pass "n","o","p","q","r","s","t","u","v","w",¬ "x","y","z": pass BACKSPACE: pass RETURN, ENTER, TAB: processEmpNum stopEvent otherwise stopEvent end case end
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 4: put "00" before gEmpNum put gEmpNum into field "empNum" 5: put "0" before gEmpNum put gEmpNum into field "empNum" 6: nothing otherwise alert "Invalid employee number. Please try again." put "" into field "empNum" set gEmpNum to EMPTY end case end
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.
Copyright 1997-2024, Director Online. Article content copyright by respective authors.