Autocomplete field

Peter Brigham MD pmbrig at gmail.com
Sat Oct 31 00:19:29 EDT 2009


On Oct 29, 2009, at 9:53 PM, capellan wrote:

> Hi Peter,
>
> Many thanks for posting this handler.
> Filtering the search results is a
> great idea! Thanks again.
>
> Looks like your handler was cut-off,
> because the rawKeydown handler
> does not had a closing match.

yes, I only included the first part of the handler. See below for  
complete script.

> While running this handler, i always get
> the word "sEntryBuffer" in the field "enterPt"
> and could not erase the content using
> backspace or delete keys.
>
> Obviously, i am doing something wrong.
> Could you point me where is my error?

Not sure. Have you tried stepping through it in debug mode? That  
should reveal what is going on.

Below is the complete group script that works for me. I changed the  
rawkeyup handler to make deleting work as expected -- before, hitting  
the delete key resulted in very weird behavior. Now at least you can  
delete properly. And I changed the rawkeyup handler to allow exiting  
completely using the escape key, which will empty the field to start  
over. I also added cases at the end of the rawkeydown handler to allow  
scrolling of the popup list field.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

As before, my changes are commented with "--##"
Watch line wraps....

********

global ptList
local sCurrFld, sEntryBuffer

on mouseUp
   if the short name of the target is "enterPt" then
     put the selectedText of fld "enterPt" into fld "enterPt"
     select after fld "enterPt"
     put fld "enterPt" into sEntryBuffer
   end if
end mouseUp

on focusIn # when focus goes to field by a handler
   put fld "enterPt" into sEntryBuffer
   if the selectedText of fld "enterPt" <> empty then
     replace (the selectedText of fld "enterPt") with empty in  
sEntryBuffer
   end if
   pass focusIn
end focusIn

on openField # when focus goes to field by user click
   put fld "enterPt" into sEntryBuffer
   if the selectedText of fld "enterPt" <> empty then
     replace (the selectedText of fld "enterPt") with empty in  
sEntryBuffer
   end if
   pass openField
end openField

on selectionChanged
   put fld "enterPt" into sEntryBuffer
   if the selectedText of fld "enterPt" <> empty then
     replace (the selectedText of fld "enterPt") with empty in  
sEntryBuffer
   end if
   pass selectionChanged
end selectionChanged

on rawKeyDown pWhich
   if the commandKey is down then pass rawKeyDown
   # exclude any functions like copy, paste, etc.
   if pWhich <= 255 then # for anything that's in ascii range
      put the selectedChunk into tSelChunk
      if word 2 of tSelChunk <= word 4 of tSelChunk then
      # adding a char to the end of the entry buffer
         put sEntryBuffer & numToChar(pWhich) into sEntryBuffer
      else # inserting a char inside the entry buffer
         put numToChar(pWhich) after char word 4 of tSelChunk of  
sEntryBuffer
         put sEntryBuffer into fld "enterPt"
      end if
      --##     I added the following two lines
      --##     pList is a global containing the full list of names
      put ptList into selectedPts
      filter selectedPts with sEntryBuffer & "*"
      --##     now selectedPts contains only those entries that match
      --##     this shortens & simplifies the display list
      put lineOffset(cr & sEntryBuffer, cr & selectedPts) into  
currentLine
      if currentLine > 0 then
         put selectedPts into fld "pList"
         --##     instead of putting the whole list into the field
         showUserList
         set the hilitedLines of field "pList" to currentLine
         set the scroll of field "pList" to \
                (currentLine - 1) * the effective textHeight of field  
"pList"
         put the selectedText of fld "pList" into fld "enterPt"
         select char (word 2 of tSelChunk) + 1 to -1 of fld "enterPt"
      else
         hide fld "pList"
         set the hilitedline of fld "pList" to 0
         put sEntryBuffer into fld "enterPt"
         select after char word 4 of tSelChunk + 1 of fld "enterPt"
      end if
   else # handle all other keys
       switch pWhich
          case 65364  # down arrow
             if the visible of fld "pList" then
                put the hilitedLine of field "pList" into currentLine
                if currentLine < the number of lines in fld "pList" then
                   add 1 to currentLine
                   set the hilitedLines of field "pList" to currentLine
                end if
                put the selectedText of fld "pList" into fld "enterPt"
                select after fld "enterPt"
             end if
             pass rawKeyDown
             break
          case 65362 # up arrow
             if the visible of fld "pList" then
                put the hilitedLine of field "pList" into currentLine
                if currentLine > 1 then
                   subtract 1 from currentLine
                   set the hilitedLines of field "pList" to currentLine
                end if
                put the selectedText of fld "pList" into fld "enterPt"
                select after fld "enterPt"
             end if
             pass rawKeyDown
             break
          case 65307  -- escape key
             --##     this allows canceling out entirely
             --##     in case of misspellings, etc
             put empty into fld "enterPt"
             put empty into sEntryBuffer
             hide fld "pList"
             break
          case 65308 -- scroll down
          case 65309 -- scroll up
             --##     these handle scrolling the list field
             pass rawkeydown
             break
          default
             hide fld "pList"
             put fld "enterPt" into sEntryBuffer
             pass rawKeyDown
       end switch
    end if
end rawKeyDown

on rawKeyUp pWhich
    --##     changed to allow deleting properly
   if pWhich is among the items of 65288,65535 then
     # if characters have been deleted with backspace or delete, reset  
the entry buffer
      put word 2 of the selectedChunk into tSelBegin
      put char 1 to tSelBegin of fld "enterPt" into sEntryBuffer
      delete char tSelBegin+1 of fld "enterPt"
   end if
   pass rawKeyUp
end rawKeyUp

# position popup list under fld being typed in
on showUserList
   set the top of fld "pList" to (the bottom of fld "enterPt") - 1
   set the left of fld "pList" to the left of fld "enterPt"
   show fld "pList"
end showUserList

on exitfield
   send closeField to the target
end exitfield

on focusOut
   send closeField to the target
end focusOut




More information about the use-livecode mailing list