Message Boards Message Boards

0
|
241 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Time between mouse clicks

Looking for a minimalist stopwatch function that returns the time between two mouse clicks? In other words, I call the function. I click the computer mouse once. I click the computer mouse again. The function returns a real number representing the elapsed time between the mouse clicks.

POSTED BY: Jay Gourley
4 Replies

Have you seen EventHandler[]? And events like "MouseDown" and "KeyDown"? You would also be interested in CurrentValue["EventAbsoluteTime"] or maybe even CurrentValue["EventData"].

POSTED BY: Michael Rogers

Thanks, Michael. I had not seen EventHandler[]. I do not understand all the documentation for it, but I think it only works in a dynamic module. I want a function that returns the interval. I tried the line below without success.

EventHandler[time, {"MouseClicked" :> start = AbsoluteTime[];, "MouseClicked" :> time = AbsoluteTime[] - start}]

POSTED BY: Jay Gourley

I'm not sure what sort of user interface you have in mind, but this might be like it:

time = DialogInput[
  DynamicModule[{data = Null}, 
   EventHandler[
    Graphics[{{Green, Disk[]}, Inset["Click"]}, 
     ImageSize -> 
      100], {"MouseClicked" :> (If[NumberQ[data], 
        DialogReturn[AbsoluteTime[] - data], 
        data = AbsoluteTime[]])}]]]

You may want to play with the option WindowMargins etc. to figure out how to place the dialog window where you want it.

Or maybe you want some other kind of interface.

POSTED BY: Michael Rogers

Thanks again, Michael. I thought there might be a more direct solution, but DialogInput[] works fine the way you wrote it.

POSTED BY: Jay Gourley
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract