Text Auto-Completion
by Gary Rosenzweig
|
Here is something that I recently developed for a Lingo-based Xtra. You know when you are using a browser and you begin to type the URL and the browser fills in the rest of the name? Like if I were to type "http://clev", the browser would fill in "http://clevermedia.com" without me having to type in the other letters. This is called auto-completion and it is a very nice interface technique. If you are allowing the user to type in something and you have a pretty good idea of what they might be trying to type, you can use this to speed up their experience. For instance, what if you asked the user to choose their favorite color? There might be too many to list for them to choose from, so you leave a blank text entry area instead. But, instead of having them type "magenta", you can fill in the word "magenta" once they have typed "ma". Try it in this Shockwave example. You may have to click on the Shockwave movie to assign keyboard focus first.
So, how to do this? First, you need a list of words. Place that in a text member. One word per line. Then, you need a text member on the Stage. Don't make it editable. Instead, just make it a plain, non-editable, text member. The frame behavior we will write will do all the work. This frame behavior will accept key presses with an on keyUp handler and use them to build the word. The text member will simply be used to display it. The behavior starts with an on beginSprite handler that reads in the words from the word list and converts it to a Lingo list. It will also set two properties to empty: pKeyword and pCompleteWord. The first is a record of what keys the user has typed. The second is the complete word that the behavior believes the user is trying to type. property pKeyword, pCompleteWord, pData, pWordList As each key is pressed, a character is added to pKeyword. If the backspace key is pressed, the last letter is removed. The "on findCompleteWord" handler is called to do the auto-completion. on keyUp me To find a word, the pKeyword property is compared to every word in the list. Once a word is found that starts with pKeyword, the search is done. Therefore, if you type "p", the auto-complete will first return "pink." If the user types "pu" then it will get more specific and return "purple." on findCompleteWord me The "on showWord" handler will place the results of the typing and search into a single text member. It will color the letters typed black and the rest of the word grayish blue. This helps the user understand where their typing ends and the auto-completion begins. on showWord me After the user has entered the word, they can press Return, hit a button, or move on to do other things on the same screen. That is up to you. You could even check to make sure that pCompleteWord is not empty and insure that the user has typed a valid response. This way, a long pop-up menu can be converted to a small text member very easily. With a little work, you could convert this to a behavior that has a pFocus property and enables the user to tab through more than one auto-completing field on a screen.
|