detecting for how long a key is being pressed

J. Landman Gay jacque at hyperactivesw.com
Tue Sep 6 20:31:08 EDT 2005


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



More information about the use-livecode mailing list