Handle one of the key events and store the time in a dictionary. Later on it should be easy to go through the dictionary and figure out the intervals between each keystroke.
Here's some example code (note that you need to add code to handle keys like Backspace - the example does not do that):
struct CharTime
{
public TimeSpan TimeSpan;
public Char Char;
}
Collection<CharTime> charTimes = new Collection<CharTime>();
DateTime mostRecent = DateTime.MinValue;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (mostRecent == DateTime.MinValue)
{
charTimes.Add(new CharTime()
{ Char = e.KeyChar, TimeSpan = TimeSpan.Zero });
}
else
{
charTimes.Add(new CharTime()
{ Char = e.KeyChar, TimeSpan = DateTime.Now - mostRecent });
}
mostRecent = DateTime.Now;
}
private void buttonReplay_Click(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
this.Invoke((Action)(() => { textBox1.Text = String.Empty; }));
foreach (var item in charTimes)
{
if (item.TimeSpan != TimeSpan.Zero)
{
Thread.Sleep(item.TimeSpan);
}
this.Invoke((Action)(() => { textBox1.Text += item.Char; }));
}
}