我有一个应用程序,它使用一个循环在时间间隔中键入文本。现在,我想要在每个字符之间输入小空格来输入文本。
我尝试过这段代码但是当我启动计时器时,键入会继续在第二个计时器内部继续。我的第一次没有空间来划定我的线路。
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}");
}
编辑,我试过使用线程睡眠版本,所以我删除了时间。我现在所拥有的只是关注但它停止了我的电脑。
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}");
}
}
答案 0 :(得分:1)
如果你想在每个角色之间留出空格,我会使用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));
}
对这样的事情使用计时器是过度的。
答案 1 :(得分:0)
使用计时器比暂停线程更好。我就是这样做的:
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()));
}
}