C# SendKeys.Send continue非停止循环
检查下面的代码。这是一个windows窗体应用程序。我的目标是继续键入一个随机字符,然后按BackSpace,然后再次继续不停地执行相同的操作。我已经写了下面的代码,但在1/2分钟后,这个代码没有键入任何内容,也没有出现错误。如何在连续不间断循环中完成? 有什么想法吗C# SendKeys.Send continue非停止循环,c#,C#,检查下面的代码。这是一个windows窗体应用程序。我的目标是继续键入一个随机字符,然后按BackSpace,然后再次继续不停地执行相同的操作。我已经写了下面的代码,但在1/2分钟后,这个代码没有键入任何内容,也没有出现错误。如何在连续不间断循环中完成? 有什么想法吗 private void textBox1_TextChanged(object sender, EventArgs e) { SendKeys.Send(RandomString(1)); Thread.
private void textBox1_TextChanged(object sender, EventArgs e)
{
SendKeys.Send(RandomString(1));
Thread.Sleep(2000);
SendKeys.Send("{BACKSPACE}");
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
designer.cs
namespace SendKeys
{
sealed partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(26, 30);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(481, 20);
this.textBox1.TabIndex = 0;
//
// timer2
//
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(530, 292);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer2;
}
}
名称空间SendKeys
{
密封部分类表格1
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.components=new System.ComponentModel.Container();
this.textBox1=new System.Windows.Forms.TextBox();
this.timer2=新系统.Windows.Forms.Timer(this.components);
这个.SuspendLayout();
//
//文本框1
//
this.textBox1.Location=新系统.图纸.点(26,30);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(481,20);
this.textBox1.TabIndex=0;
//
//定时器2
//
this.timer2.Tick+=新的System.EventHandler(this.timer2\u Tick);
//
//表格1
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(530292);
this.Controls.Add(this.textBox1);
this.Name=“Form1”;
this.Text=“Form1”;
this.Load+=new System.EventHandler(this.Form1\u Load);
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer2;
}
}
每2秒它就会滴答作响。它将交替发送密钥和删除密钥
e、 g.发送密钥,2秒通过,删除密钥,2秒通过,发送密钥
已更新,使其运行30分钟,然后将停止
designer.cs
namespace SendKeys
{
sealed partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(26, 30);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(481, 20);
this.textBox1.TabIndex = 0;
//
// timer2
//
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(530, 292);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer2;
}
}
名称空间SendKeys
{
密封部分类表格1
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.components=new System.ComponentModel.Container();
this.textBox1=new System.Windows.Forms.TextBox();
this.timer2=新系统.Windows.Forms.Timer(this.components);
这个.SuspendLayout();
//
//文本框1
//
this.textBox1.Location=新系统.图纸.点(26,30);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(481,20);
this.textBox1.TabIndex=0;
//
//定时器2
//
this.timer2.Tick+=新的System.EventHandler(this.timer2\u Tick);
//
//表格1
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(530292);
this.Controls.Add(this.textBox1);
this.Name=“Form1”;
this.Text=“Form1”;
this.Load+=new System.EventHandler(this.Form1\u Load);
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer2;
}
}
每2秒它就会滴答作响。它将交替发送密钥和删除密钥
e、 g.发送密钥,2秒通过,删除密钥,2秒通过,发送密钥
已更新,使其运行30分钟,然后将停止。“不停止”的建议与此不同,但我认为密钥发送者应始终能够在时间段过去之前安全停止。在上面的回答示例中,不能通过使用窗体上的“暂停”按钮来停止操作,因为单击Windows画布上的任何内容都会将焦点放在那里,并且键会到达那里
下面是我的KeySender类。我的版本只针对在密钥发送器启动时具有焦点的控件。每当焦点丢失时,计时器就会停止。我为关键数据介绍了一位代表
public delegate string KeysProducer();
public class KeySender
{
[DllImport("user32.dll")]
static extern IntPtr GetFocus();
private IntPtr initialFocus = IntPtr.Zero;
private DateTime _startTime = DateTime.Now;
private Timer _timer = null;
private KeysProducer keysProducer = null;
public KeySender(KeysProducer source)
{ keysProducer = source; initialFocus = GetFocus(); }
public KeySender(IntPtr Focussed, KeysProducer source)
{ keysProducer = source; initialFocus = Focussed; }
public bool Sending()
{ return _timer!=null; }
public void StopSendingKeys()
{
if (_timer != null) _timer.Enabled = false;
_timer = null;
}
public void StartSendingKeys(int minutes, int intervalsec)
{
if (_timer == null)
{
_timer = new Timer() { Interval = intervalsec };
_timer.Tick += (s, e) =>
{
if (DateTime.Now - _startTime >= TimeSpan.FromMinutes(minutes))
{ _timer.Enabled = false; _timer = null; }
else
if (initialFocus != GetFocus())
{ _timer.Enabled = false; _timer = null; }
else
if (keysProducer!=null) SendKeys.Send(keysProducer());
};
_timer.Start();
}
}
}
测试:
public class KeySenderDemo
{
private static Random random = new Random();
private KeySender ks;
private bool _deleting = true;
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
public string DefaultKeysToSend()
{
return (_deleting = !_deleting) ? "{BACKSPACE}" : RandomString(1);
}
public void Stop()
{
if (ks != null) ks.StopSendingKeys();
ks = null;
}
public KeySenderDemo()
{
Form f = new Form();
TextBox tb = new TextBox();
tb.Parent = f;
tb.Location = new Point(10, 10);
tb.Size = new Size(200, 16);
f.Load += (s, e) =>
{
tb.Focus();
ks = new KeySender(tb.Handle, DefaultKeysToSend);
ks.StartSendingKeys(1, 200);
};
f.Click += (s, e) =>
{
if (ks.Sending()) ks.StopSendingKeys();
else
{
tb.Focus();
ks.StartSendingKeys(1, 200);
}
};
Application.Run(f);
}
}
单击表单以停止并重新启动。电话:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
KeySenderDemo ksd = new KeySenderDemo();
//Application.Run(new Form1());
}
}
“不停止”的含义不同,但我认为密钥发送者应该始终能够在时间段过去之前安全停止。在上面的回答示例中,动作c