新建一个window的application,Form里面一个textbox,一个开始按钮,点击,就自动模拟键盘,往textbox输入东西了
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
while (true)
{
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("b");
Thread.Sleep(500);
SendKeys.SendWait("b");
Thread.Sleep(2000);
}
}
但是问题是,运行后,什么都不能点击了,就像死机了一样,我想做个停止按钮都不行我只能Ctrl+Alt+Del,结果,进程界面出来了,(明明当前窗口焦点都不在我哪个Form上了)都还在点a和b,害我想结束进程(进程的首字母是K)都不行现在想请教哈大家改成,比如我打开了一个记事本,进程名是“notepad.exe”,我只让程序对这个进程操作,可以么?就相当于,只在这个记事本里面模拟键盘的输入。网站找了很久了,说要用什么钩子,但是点都不熟悉,请大家帮帮我,我对这方面不熟悉
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
while (true)
{
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("b");
Thread.Sleep(500);
SendKeys.SendWait("b");
Thread.Sleep(2000);
}
}
但是问题是,运行后,什么都不能点击了,就像死机了一样,我想做个停止按钮都不行我只能Ctrl+Alt+Del,结果,进程界面出来了,(明明当前窗口焦点都不在我哪个Form上了)都还在点a和b,害我想结束进程(进程的首字母是K)都不行现在想请教哈大家改成,比如我打开了一个记事本,进程名是“notepad.exe”,我只让程序对这个进程操作,可以么?就相当于,只在这个记事本里面模拟键盘的输入。网站找了很久了,说要用什么钩子,但是点都不熟悉,请大家帮帮我,我对这方面不熟悉
解决方案 »
- 后台为什么取不到前台动态创建的input text值
- C#异步调用中的begininvoke第一个参数callDuration到底有什么用?
- 如何使窗体显示在特定窗体的前面?
- 一个Winform如何只能产生一个实例?
- System.IO.Directory.GetDirectories
- Remoting的问题,高手请进
- 属性显示问题
- c# 养路费滞纳金的计算公式
- 新年提的第一个问题:我要对在datagrid中输入的值进行验证!如果数据错误!则提示,并将焦点定位到相应的Cell中!
- 安装了vs.net 2003 beta后,因为没有安msdn,使原来的vs.net2002中的Msdn也没法用了
- datagridview拖拽导致的问题
- 关于hashtable的问题
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
{
textBox1.Focus();
SendKeys.SendWait("a");
//Thread.Sleep(500);
SendKeys.SendWait("a");
// Thread.Sleep(500);
SendKeys.SendWait("b");
//Thread.Sleep(500);
SendKeys.SendWait("b");
//Thread.Sleep(2000);
}
直接这样可以吗,按一下按钮输入aabb
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
} private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.SendWait("a");
}
{
Thread.Sleep(2000);
}
这是死循环
{
tbInfo.Focus();
timer1.Enabled = true;
} private void btnStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
} private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(500);
SendKeys.Send("a");
//Thread.Sleep(500);只要把这个一加上,就假死了,鼠标都点不到btnStop按钮了。
//SendKeys.Send("b");
}我用了Timer了,设置的1000毫秒,就是一秒,模拟键盘在文本框输入a,b,还是不行啊?要死机啊,有一下哈把cpu弄到80%了,太吓人了,请大家再指点指点我啊
button1_Click(**********)
{
Thread thread=new Thread (new ThreadStart (asdada));
thread.Start();
}
public void asdada()
{
textBox1.Focus();
while (true)
{
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("b");
Thread.Sleep(500);
SendKeys.SendWait("b");
Thread.Sleep(2000);
}
}
全局Hook键盘
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd); private void btnStart_Click(object sender, EventArgs e)
{
IntPtr calculatorHandle = FindWindow(null,"aaa.txt - 记事本"); if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
} timer1.Interval = 3000;//3秒 SetForegroundWindow(calculatorHandle);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.SendWait("a");
Thread.Sleep(500);
SendKeys.SendWait("b");
SendKeys.Flush();
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
Application.Exit();
}
}帅哥些,我已经很接近了,我新建一个叫做AAA的记事本,并打开,就看到效果了,唯一的缺点是,这个键盘时间不是在后台激活的,总是要把那个记事本焦点激活,才会有效果。怎么弄成后台隐藏,等它自己点击的啊?
如下private delegate void DlgateText();private void SetTextBoxFocus()
{
if(this.InvokeRequired)
{
this.Invoke(new DlgateText(SetTextBoxFocus), new object[]{});
Thread.Sleep(500) ;
}
else
{
textBox1.Focus();
}
}