是否可以保存/存储按键之间的时间? [英] Is it possible to save/store the time between a key press and another?

查看:55
本文介绍了是否可以保存/存储按键之间的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在文本框中大约写了一个字符.每5秒.
我想节省按键之间的时间,以便能够像在视频中一样重播它,在其中我可以以相同的时间间隔显示这些字符.
我不知道自己是否能很好地解释自己,请让我知道.

我试图阅读一些有关计时器和计时器刻度的教程,但我不太了解.也许如果有人知道一个简化的教程并且可以共享一个链接,那就太好了,或者在答案中写下任何内容.非常感谢您的帮助.

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.

推荐答案

处理关键事件之一并将时间存储在字典中.稍后,应该很容易浏览字典并找出每次击键之间的间隔.

这是一些示例代码(请注意,您需要添加代码以处理诸如Backspace之类的键-该示例不会那样做):

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; }));
    }
}


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.


这篇关于是否可以保存/存储按键之间的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆