Page 1 of 1

Waiting for a user to finish typing, then processing

Posted: Sun Jun 14, 2009 4:14 pm
by andyh1234
Im just working on some code to bring up a suggestions box a bit like google does.

Ive got all the code working but its quite slow and processes everytime the rawkeyup message is processed locking the system up until its processed so it slows down the entry speed.

It would be much better if it waited until say 0.2 after a user had stopped typing then popped the suggestions up.

Any ideas how this could be done?

Thanks

Andy

Posted: Sun Jun 14, 2009 4:46 pm
by bn
Andy,

put this into a field

Code: Select all

on keyDown pKey
    if pKey = "tDelete" then
        put char 1 to -2 of me into searchString
        put searchstring into me
        select after text of me
        if searchstring = "" then
            put "" into field "Number of Records"
            put "" into field "Browse List"
            exit keyDown
        end if
    else
        put me & pKey into searchString
        put searchstring into me
        select after text of me
    end if
    get fld "Data"	# 
    repeat for each line thisLine in it
        if searchString is among the words of thisLine
        then
            put thisLine & return after hits--Whole words
            next repeat
        else
            if thisLine contains searchString then put thisLine & return after otherHits
        end if
    end repeat
    put otherHits after hits
    if the last character of hits is return then delete the last character of hits
    put hits into fld "Browse List"
    put 1*the number of lines in hits && "records" into fld "Number of Records"
    -- pass keyDown
end keyDown

on rawKeyDown pKeyCode
    if pKeyCode = 65288 then send "keyDown" && "tDelete" to me
    else  pass rawKeyDown
end rawKeyDown
this is for the field the user types into.
the script uses 3 more fields the way it is now.
One field holds the Date and is incidentally named "Data", this is your dictionary with the words to look up.
One field "Number of records" which shows the number of hits.
One field "browse List" which has shows the actual hits.
For a word list of about 1500 no problem with the responsiveness.
the user can delete characters and that is reflected in the expanding list of hits.

To wait for the user to stop typing is difficult, in my opinion. The whole thing is about responsivness, i.e. while the user types.
You could speed it up if you would put the dictionary into a global variable of a script local variable, since the dictionary usually does not change that much.

regards
Bernd

Posted: Sun Jun 14, 2009 5:01 pm
by andyh1234
Thanks, the problem I have is the database has 40,000 entries and each of the results (which Im limiting to 15 to keep things moving at all!) then needs to be cross references in a second database to check it is there before being presented as a suggestion to the user.

At the moment on my mac its not too slow, but im a little worried on slower machines it might just grind to a halt.

Posted: Sun Jun 14, 2009 7:36 pm
by bn
andy,

if you want to do the dictionary after a time delay try something like this:

Code: Select all

local sTimer
on keyUp pKey
   -- here you reset the timer, each time the user types it start anew
   put the milliseconds into sTimer
   send doDictLookUp to me in 50 milliseconds
   -- do whatever
end keyUp 

on doDictLookUp
   -- adjust for typing speed
   -- after >200 milliseconds the dictionary look up is started
   if the milliseconds - sTimer > 200  then
      do my dictionary stuff
   else 
     if doDictLookUp is not in the pendingmessages then
      send "doDictLookUp" to me in 100 milliseconds
     end if
   end if
end doDictLookUp
at each keyUp event the timer is reset, if there is not a keyUp after 200 milliseconds you would start your dictionary lookup
adjust to taste.
regards
bernd

regards

Bernd

edited for the pendingmessages

Posted: Mon Jun 15, 2009 10:09 am
by andyh1234
Thanks, thats perfect for what I need now.

Appreciated!

Andy