新建一个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”,我只让程序对这个进程操作,可以么?就相当于,只在这个记事本里面模拟键盘的输入。网站找了很久了,说要用什么钩子,但是点都不熟悉,请大家帮帮我,我对这方面不熟悉

解决方案 »

  1.   

    把模拟键盘写到另一个线程里SendKeys.SendWait(); 可以向任意一个 当前进程里写东西,也就是 记事本在有焦点的时候可以
      

  2.   

    http://www.programmersheaven.com/mb/windows/80197/80197/sending-messages-to-other-windows/?S=B20000
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 
    http://feiyun0112.cnblogs.com/
      

  3.   

    private void button1_Click(object sender, EventArgs e) 
            { 
                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
      

  4.   

    加一个timer控件,设置Interval为1000,然后设置button1事件函数和timer响应函数即可。
            private void button1_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                textBox1.Focus();
                SendKeys.SendWait("a");            
            }
      

  5.   

    while (true)

        Thread.Sleep(2000);
    }
    这是死循环
      

  6.   

    private void btnStart_Click(object sender, EventArgs e)
            {
                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%了,太吓人了,请大家再指点指点我啊
      

  7.   

    用线程试试
    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); 
                } 
    }
      

  8.   

    http://topic.csdn.net/u/20080311/18/8886fa0b-e91e-4ed0-b4b5-a092bc71d9f9.html
    全局Hook键盘
      

  9.   

    界面上就一个btnStart和一个btnStop,代码如下:
    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的记事本,并打开,就看到效果了,唯一的缺点是,这个键盘时间不是在后台激活的,总是要把那个记事本焦点激活,才会有效果。怎么弄成后台隐藏,等它自己点击的啊?
      

  10.   

    在线程中操作控件需要用委托,
    如下private delegate void DlgateText();private void SetTextBoxFocus()
    {
        if(this.InvokeRequired)
        {
            this.Invoke(new DlgateText(SetTextBoxFocus), new object[]{});
            Thread.Sleep(500) ;
        }
        else
        {
            textBox1.Focus();
        }
    }