Reputation: 6480
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}");
}
}
Upvotes: 0
Views: 172
Reputation: 6480
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()));
}
}
Upvotes: 0
Reputation: 16747
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.
Upvotes: 1