detecting for how long a key is being pressed

Lynch, Jonathan bnz2 at cdc.gov
Wed Sep 7 08:49:58 CDT 2005


Sorry, I misunderstood the original question.

This script accomplishes what is needed:

on rawkeydown pKey
  global gStartTime
  if pKey = 107 and gStartTime = empty then
    checkkeysdown
  end if
  pass rawkeydown
end rawkeydown

on checkkeysdown
  global gStartTime
  if the keysdown contains 107 then
    put the milliseconds into tMil
    if gStartTime = empty then 
      put tMil into gStartTime
      put "start time: " & tMil into field "feedback"
    end if
    send checkkeysdown to me in 50 milliseconds
  else
    put ((the milliseconds)-gStartTime)/1000 into tDuration
    put Linefeed & "Duration: " & tDuration & " seconds" after field
"feedback"
    put empty into gStartTime
  end if
end checkkeysdown



-----Original Message-----
From: use-revolution-bounces at lists.runrev.com
[mailto:use-revolution-bounces at lists.runrev.com] On Behalf Of Lynch,
Jonathan
Sent: Wednesday, September 07, 2005 9:38 AM
To: jacque at hyperactivesw.com; How to use Revolution
Subject: RE: detecting for how long a key is being pressed

I tested the following script, and it seems to work pretty well. It is
currently set up to test for a lowercase 'k' and put the milliseconds
into a field titled "feedback" and to stop putting updating the
milliseconds when the 'k' is released. I put this script in the stack
script:


on rawkeydown
  checkkeysdown
  pass rawkeydown
end rawkeydown

on checkkeysdown
  if the keysdown contains 107 then
    put the milliseconds into field "feedback"
    send checkkeysdown to me in 50 milliseconds
  end if
end checkkeysdown




-----Original Message-----
From: use-revolution-bounces at lists.runrev.com
[mailto:use-revolution-bounces at lists.runrev.com] On Behalf Of J. Landman
Gay
Sent: Tuesday, September 06, 2005 8:31 PM
To: How to use Revolution
Subject: Re: detecting for how long a key is being pressed

Nicolas Cueto wrote:
> Like Jacqueline, I too was going to suggest "keyDown"
> and "keyUp". However I tried it out first myself and
> found that so long as the targeted key is held conti-
> nuously down:
> 
> 1. the "keyDown" message is also generated continu-
> ously. Thus, if the variable for storing the start time
> is being set by the keyDown handler, then that variable
> will be continuously reset. (To resolve this, I tried "if
> tTimeVar is not empty then" to escape the handler, but
> no luck)
> 
> 2. the "keyUp" message is also being generated. I know
> this because in the keyUp handler I have a "put x in field y"
> line, which results in the field continuously blinking. (I would've
> thought the keyUp message would be generated only after
> the target key was released?)
> 
> Jacqueline also suggests that these key-handlers might be
> system specific. In which case, I'm on Win2K.

It is OS-specific. On Mac OS X, keyup is only sent once when the key is
released. On Mac OS 9 a "keyup" is sent immediately after keydown, sort
of like you report.

Here is one way to possibly work around the problem (I liked Phil's
array suggestion):

local tKeyTimes

on rawkeydown k
  if tKeyTimes[k] = ""
  then put the milliseconds into tKeyTimes[k]
end rawkeydown

on rawkeyup k
  wait 50 milliseconds -- adjust this as needed
  if keysdown() contains k
  then pass rawkeyup -- they are still holding it
  get tKeyTimes[k]
  if it <> "" then
    put the milliseconds - it - 50 into tTimeVar -- holds the timing
data
    put "" into tKeyTimes[k]
  end if
end rawkeyup

I didn't try it on a Windows machine though, so it may need tweaking.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
use-revolution at lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your
subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution


_______________________________________________
use-revolution mailing list
use-revolution at lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your
subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list