Page 1 of 1

Detecting a prolonged button press

Posted: Thu Feb 07, 2013 10:11 am
by grovecat
I need to ignore a button press unless it is held for more than a couple of seconds and I tried the following code, which does not work.

Code: Select all

local tDown

on mouseDown
   send checkMouse to me in 3 secs
end mouseDown

on checkMouse
   put false into tDown
   wait 1 sec
   if tDown is true then answer "It was held down"
end checkMouse

on mouseStillDown
   put true into tDown
end mouseStillDown
Help appreciated.

TIA
Don

Re: Detecting a prolonged button press

Posted: Thu Feb 07, 2013 11:00 am
by bn
Hi Don,

try this

Code: Select all

local sStartTime
on mouseDown
  put the milliseconds into sStartTime
end mouseDown

on mouseUp
   put the milliseconds - sStartTime into tTimeDiff
   if tTimeDiff > 3000 and tTimeDiff < 6000 then -- between 3 and 6 seconds
      -- process your mouseUp
      answer "right time delay"
   end if
end mouseUp
Kind regards

Bernd

Re: Detecting a prolonged button press

Posted: Thu Feb 07, 2013 11:14 am
by grovecat
Thanks Bernd

An elegant solution that works fine. However, I would prefer the processing to start after the 3 secs or whatever, while the mouse is still being held down. Can you see a way to do that?

Cheers
Don

Re: Detecting a prolonged button press

Posted: Thu Feb 07, 2013 11:25 am
by bn
Hi Don,

try this

Code: Select all

local sStartTime
on mouseDown
   put the milliseconds into sStartTime
   send checkTime to me in 5 milliseconds
end mouseDown

on checkTime
   put the milliseconds - sStartTime into tTimeDiff
   if tTimeDiff < 3000 and the mouse is down then
      send checkTime to me in 50 milliseconds
      exit checkTime
   end if
   if tTimeDiff > 3000 and the mouse is down then
      -- process your code
      answer " right timeDelay"
   end if
end checkTime
Kind regards

Bernd

Re: Detecting a prolonged button press

Posted: Thu Feb 07, 2013 11:28 am
by grovecat
That is exactly what I needed.

Thanks again Bernd.

Cheers
Don