0

I am having a app which use one loop to type the text in the time intervals. Now, I want the text to be typed with small spaces between each character.

I have tried this code but when I start the timers the typing keeps continuing inside of second timer. There is no spaces in my first times which timed my lines.

private List<char> charList = new List<char>();

public AutoTyper()
{
    InitializeComponent();

    tmrInterval.Tick += new EventHandler(Interval);
    tmrDelay.Tick += new EventHandler(Delay);
    tmrSpace.Tick +=new EventHandler(charSpaces);
    txtText.TextChanged += new EventHandler(TextChanged);
    tbType.SelectedIndexChanged += new EventHandler(IndexChanged);
}

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)
    {
        tmrSpace.Enabled = true;
    }
}

private void charSpaces(object sender, EventArgs e)
{
    Random random = new Random();
    tmrSpace.Interval = random.Next(200, 400);

    foreach (char character in charList)
        SendKeys.Send(character.ToString());
    SendKeys.Send("{enter}");
}

Edit, I have tried to use thread sleep version so I have deleted the times. All I have now is following but it halts my PC.

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(random.Next(400, 500));
        }

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

2 Answers 2

1

If you want short spaces between each character I would just use a Thread.Sleep().

Random r = new Random();

foreach (char character in charList)             
{
    SendKeys.Send(character.ToString()); 
    // sleep for between 200 and 400 milliseconds
    Thread.Sleep(r.Next(200, 400));
}

Using a timer for something like this is overkill.

Sign up to request clarification or add additional context in comments.

8 Comments

There are some who will downvote any use of Thread.Sleep(). Nevetheless, I have used it many times and the world has failed to end.
Not seeing what the issue is with a timer either. Maybe the down-voter should comment. In the meantime, however... +1!
@Norla: Timers are normally used asynchronously. In this case the requirement is a synchronous delay. Using a timer to interrupt a process in this way seems overly complicated to me. (And clearly it was overly complicated to the OP, because his/her code is a mess that clearly won't work!)
Oh snap! Yeah, I actually just did something like this. Tried making text type to the screen like a typewriter. It worked fine by running (basically) your code in a new thread.
The thread sleep didn't seem to work causing my winform thread to be put on halt.
|
0

Using timer is much better option than pausing thread. This is how I have done it:

    private void Space(object sender, EventArgs e)
    {
        SendKeys.Send(txtText.Text.Substring(b++, 1));

        tmrSpace.Interval = random.Next(50, 150);

        if (b == txtText.TextLength)
        {
            tmrSpace.Enabled = false;
            SendKeys.Send("{enter}");
        }
    }

    private void Interval(object sender, EventArgs e)
    {
        if (cbPause.Checked)
        {
            b = 0;

            tmrSpace.Interval = random.Next(50, 150);
            tmrSpace.Enabled = true;
        }
        else
        {
            SendKeys.Send(txtText.Text + "{enter}");

            if (tbType.SelectedTab == tbRange) tmrInterval.Interval = random.Next(int.Parse(nudMin.Value.ToString()), int.Parse(nudMax.Value.ToString()));
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.