The following method is a Key event handler for a custom entryfield that executes a SEEK in the current workarea as keys are pressed—an incremental search control.

function Key( nChar, nPosition )

   if nextkey( ) # 0 // If keys pending 

      return ( nChar # 255 ) // do nothing, and suppress keystroke if value is 255 

   endif 

   if nChar == 255 // If keystroke is special value 255 

      if not isblank( this.value ) 

         this.seek( ) // call control's seek( ) method to do actual SEEK 

      endif 

   else 

      if nChar >= 32 and nChar < 255 

         keyboard "{255}" // For alphanumeric keystrokes, stuff special key 255 

      endif 

   endif 

return nChar 

Like all Key event handlers, this one receives two parameters: the value of the keystroke, and the current position in the control. This method does not use the position parameter. It does use a specific keystroke, which is chosen so that it does not conflict with typical keys. That keystroke has the INKEY( ) value 255.

The method first checks if there are keystrokes pending in the keyboard typeahead buffer—which would happen for a fast typist—and if so, no further action is taken, since those pending keystrokes would immediately cause the Key event handler to fire again, and any action at this point would be wasted.

For an alphanumeric keystroke—that is, one that is not a control or function key—the special key is stuffed into the keyboard buffer by the KEYBOARD command. This causes the Key event to be fired again, after the key that was actually typed goes into the entryfield as usual. The Key method detects the special key and performs the SEEK, which is coded in a separate seek( ) method for modularity; that is, the SEEK behavior may be modified without changing the Key method.

The KEYBOARD command is used instead of the keyboard( ) method because the keyboard( ) method inserts keystrokes directly into the control, causing the Key event to fire immediately from within the Key event handler, which in turn would cause the stuffed keystroke to be handled before the key that was actually typed. The KEYBOARD command uses the general typeahead buffer instead, and the keys would be handled in sequence.