[comp.sys.mac.hypercard] Double Clicking on Button

krona@nada.kth.se (Kjell Krona) (09/20/90)

In <1562taylorj@yvax.byu.edu>, taylorj@yvax.byu.edu (Jim Taylor) writes:

< There are a couple of ways to recognize double clicks. 
 (stuff deleted )
< If you want to get fancy and do a good job,
< try something like this

<  on mouseUp
<   put the ticks into start
<    repeat until the ticks-start >= 50  -- or whatever time you want to allow
<      if the mouseClick then
<        -- do doubleclick stuff here or set a flag
<        exit repeat
<      end if
<    end repeat
<  end mouseUp
 
 The script as it is cannot distinguish between single clicks and double
clicks.  By substituting  "exit mouseUp" for "exit repeat" , the single
click case can be handled after the repeat loop.

 Another case which might be nice to handle is "dragging"; perhaps to
track the  mouse cursor with a button. The following scripts distinguishes
between dragging,  single clicks, and double clicks. Two global variables,
 "theDragWait"  &  "theDoubleClickWait", are used for time values. These
could be set on  "opencard" or "openstack"; it might also be nice to let the
user change the defaults on a  "Preferences" card. A useful value for
"theDragWait"   is 5 - 10 ticks,  for the "theDoubleClickWait" 10 - 20 ticks.

on mouseDown
  global theDownTime
  -- Reset the mouse down timer
  put the ticks into theDownTime
end mouseDown

on mouseStillDown
  global theDragWait, theDownTime
  if (the ticks - theDownTime) > theDragWait then
    -- Do what you would like to do if dragging
    doDrag
    -- Then, reset the timer
    put empty into theDownTime
  end if
end mouseStillDown

on mouseUp
  global theDoubleClickWait, theDownTime
  if theDownTime<> empty then
    -- The "dragging" timer has not been reset => no dragging has occurred
    put the ticks into theUpTime
    -- This will wait until either the mouse is clicked again OR until
    -- _theDoubleClickWait _ time has elapsed, whatever comes first
    repeat until (the ticks - theUpTime > theDoubleClickWait)
      if the MouseClick then
        -- It was a double click
        doDoubleClick
        exit mouseUp
      end if
    end repeat
    -- It was a single click
    doSingleClick
  else
    -- We have been dragging; mouseUp should normally be ignored
  end if
end mouseUp

Good luck!

Kjell Krona                      krona@nada.kth.se
Dept. of Architecture/Dept. of Numerical Analysis and Computer Science
Royal Institute of Technology
S-100 44 Stockholm, Sweden