我用C#编写了这个程序:
namespace Spammer
{
public partial class Form1 : Form
{
int delay, y = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
timer1.Interval = delay;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
它在大多数情况下都能正常工作,它可以快速发送密钥。
但是当我插入例如10 MS的延迟时,很难点击“停止”按钮来停止它。停止发送的唯一方法是关闭程序,我不想这样做。
无论如何我可以非常快速地发送密钥,比如5-10 MS,而不会影响我按下程序内按钮的能力吗?在快速发送时我无法点击...
答案 0 :(得分:3)
答案 1 :(得分:1)
我能够重现这个问题。该应用程序每10毫秒发送一次击键。对我来说,应用程序导致冻结并不奇怪。每10毫秒一次击键对于活跃的App来说是一个很大的障碍。线程无济于事。为什么这种行为令人惊讶?
换句话说,当我重载消息泵时,我不希望事情能够很好地发挥作用。
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spammer//your own namesapce
{
public partial class Form1 : Form
{
int delayInMilliseconds, y = 1;
private Timer timer1;
public Form1()
{
InitializeComponent();
//StartTimerWithThreading();
SetupTimer();
}
void StartTimerWithThreading()
{
Task.Factory.StartNew(() =>
{
SetupTimer();
});
}
void SetupTimer()
{
timer1 = new Timer();//Assume system.windows.forms.timer
textBox2.Text = "10";//new delay
timer1.Tick += timer1_Tick;//handler
}
private void button1_Click(object sender, EventArgs e)
{
delayInMilliseconds = int.Parse(textBox2.Text);
timer1.Interval = delayInMilliseconds;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
答案 2 :(得分:0)
简单的解决方案不是将代码添加到按钮的Click
事件处理程序,而是需要MouseDown
事件处理程序:
//MouseDown event handler for the button2
private void button2_MouseDown(object sender, EventArgs e) {
timer1.Enabled = false;
}
或者您可以继续使用Click
事件处理程序,但只有在MouseButtons
不是Left
时才会发送密钥:
private void timer1_Tick(object sender, EventArgs e) {
String textt = textBox1.Text;
if(MouseButtons != MouseButtons.Left) SendKeys.Send(textt);
}
//then you can freely click your button to stop it.