如何在间隔中键入字符?

时间:2012-01-07 03:22:16

标签: c# winforms

我正在编写一个程序,它将以5秒的间隔键入一行文本。现在,我要做的是添加函数,它将逐个字符地键入文本字符,每个字符之间有小空格。我正在尝试使用Thread.Sleep,但我的程序崩溃,我不知道为什么。

如何正确地做到这一点?

    private void Interval(object sender, EventArgs e)
    {
        if (cbPause.Checked == false)
        {
            SendKeys.Send(txtText.Text + "{enter}");

            if (tbType.SelectedTab == tbInterval) tmrInterval.Interval = int.Parse(nudInterval.Value.ToString());
            if (tbType.SelectedTab == tbRange) tmrInterval.Interval = random.Next(int.Parse(nudMin.Value.ToString()), int.Parse(nudMax.Value.ToString()));
        }
        else if (cbPause.Checked == true)
        {
            Random random = new Random();

            foreach (char character in charList)
            {
                SendKeys.Send(character.ToString());
                Thread.Sleep(1000);
            }

            SendKeys.Send("{enter}");
        }
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        if (txtText.TextLength < 1 || txtText.Text == string.Empty)
        {
            lblMessage.Text = "You must type text to proceed!";
            btnStart.Enabled = false;
        }
        else
        {
            foreach (char character in txtText.Text)
            {
                charList.Add(character);
            }

            if (tbType.SelectedTab == tbInterval)
            {
                tmrDelay.Enabled = true;

                txtText.Enabled = false;
                nudInterval.Enabled = false;
                nudMin.Enabled = false;
                nudMax.Enabled = false;
                btnStart.Enabled = false;
                btnStop.Enabled = true;
            }

            if (tbType.SelectedTab == tbRange)
            {
                tmrDelay.Enabled = true;
                tbType.Enabled = false;
                txtText.Enabled = false;
                nudInterval.Enabled = false;
                nudMin.Enabled = false;
                nudMax.Enabled = false;
                btnStart.Enabled = false;
                btnStop.Enabled = true;
            }

            lblMessage.Text = "Starting auto typing in: 3";
        }
    }

1 个答案:

答案 0 :(得分:3)

如果你致电Thread.Sleep,这将会挂起你的GUI线程。

我要做的是,使用System.Windows.Forms.Timer,每Tick,让它输入下一个字符。

public partial class Form1 : Form
{
    Timer timer;
    string str;
    int char_num;

    public Form1()
    {
        InitializeComponent();
        timer = new Timer();
        timer.Tick += timer_Tick;
    }

    void timer_Tick(object sender, EventArgs e)
    {
        SendKeys.Send(str.Substring(char_num++, 1));
        if (char_num == str.Length)
            timer.Stop();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Focus();
        str = "Jonathon";
        char_num = 0;
        timer.Interval = 1000;
        timer.Start();
    }
}

注意:如果不明显,此代码需要一个带有textBox1的表单和一个Click事件连接到button1_Click()的按钮。

如果你想立即发送第一个字符,你可以通过添加以下行来启动计时器时立即调用Tick处理程序来“欺骗”:

timer_Tick(null, EventArgs.Empty);