Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For instance I write a character in a textbox approx. every 5 seconds.
I want to save the time between the keys so be able to replay it like in a video, where i can display those characters with the same time intervals.
I don't know if I'm explaining myself well enough, please let me know.

I tried to read some tutorials about timer and timer ticks, but I didn't quite get the hang of it. Maybe if someone knows of a simplified tutorial and can share a link it would be great or write anything in an answer. Any help is greatly appreciated.
Posted
Updated 11-Feb-11 3:10am
v2

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):

C#
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; }));
    }
}
 
Share this answer
 
v3
Comments
LighthouseCall 11-Feb-11 10:15am    
Collection<chartime> charTimes = new Collection<chartime>();

gives an error:

Error 1 The non-generic type 'TimeCapturing.Collection' cannot be used with type arguments.
Nish Nishant 11-Feb-11 10:21am    
You need to use the generic version. You are using the old non-generic one.
Sergey Alexandrovich Kryukov 11-Feb-11 14:08pm    
This is correct, my 5, but you see: you use DataTime yourself (I refer to my previous comment) :-)
So, will you explain the accuracy issue?
Thank you.
--SA
Nish Nishant 11-Feb-11 17:57pm    
Well I did not say never use DateTime. I said don't use DateTime when you are measuring really accurate time measurements :-)
Sergey Alexandrovich Kryukov 11-Feb-11 23:53pm    
I know, I know. Just kidding :-)
And thanks again for pointing out the accuracy problem and the reference.
--SA
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TimerTicks
{
    public partial class Form1 : Form
    {
        long CurTickValue, Difference, CompTime;
        public Form1()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            CurTickValue = Environment.TickCount;
            Difference = CurTickValue - CompTime;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            CompTime = Environment.TickCount;
        }


        private void Grid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A)
            {
                //CurTickValue = Environment.TickCount;
                textBox1.Text = (Difference.ToString());
                listBox1.Items.Add(Difference.ToString());
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            CurTickValue = Environment.TickCount;
               
        }

    }
}



This works after setting the properties of the Form, Button and Timer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900