4
$\begingroup$

I'd like to make a dynamic module where a user types in text and Mathematica records the typed text and time at which each key is pressed. The time could be relative to the start or relative to the time of the previous keypress.

For instance, typing "Hello" could lead to the output

{{"H",0},{"e",100},{"l",107},{"l",25},{"o",125}} 

where the times are relative to the previous keypress and measured in milliseconds.

I found this blog post that specifically measures the time spent typing "wolfram". That DynamicModule does way more than what I need, including having a very nice interface. I'm having a hard time figuring out what I can remove and how to extend the functionality beyond the letters of "wolfram" to arbitrary-length text.

Any help is appreciated.

$\endgroup$

1 Answer 1

10
$\begingroup$

Here is a minimal implementation using EventHandler.

DynamicModule[{keys = {}, starttime = Now}, Grid[{{EventHandler[ InputField["", String], {"KeyDown" :> (AppendTo[keys, {CurrentValue["EventKey"], Now - starttime}])}, PassEventsDown -> True]}, {Dynamic[Grid@keys]}}, Alignment -> Left]] 

enter image description here

$\endgroup$
5
  • $\begingroup$ This works well. I modified it so that keys is a global variable that I can access outside of the dynamic module to analyze; this got me so much farther along than I would have on my own. Thanks. $\endgroup$ Commented Feb 22, 2024 at 19:31
  • $\begingroup$ @GregH how did you get keys out as a global variable? $\endgroup$ Commented May 15, 2024 at 21:05
  • $\begingroup$ @Charlie, just omit the DynamicModule, which otherwise serves to localize the symbols. $\endgroup$ Commented May 15, 2024 at 22:02
  • 1
    $\begingroup$ @Charlie I put the keys={} line outside the DynamicModule, as in keys={}; DynamicModule[.... $\endgroup$ Commented May 16, 2024 at 13:02
  • $\begingroup$ wow that's cool thanks. $\endgroup$ Commented May 17, 2024 at 13:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.