一直按住键盘某个键不放,程序会不断的触发keydown事件,我原来用这样的方法实现游戏的押分。现在居于成本考虑,想换成鼠标左键实现同样的效果,用timer来实现效果不是很理想。不知道各位有什么好的建议没?

解决方案 »

  1.   

           bool pressKey=true;
            System.Threading.Thread thread=null;        private void Press()
            {
                while (pressKey)
                {
                    SendKeys.SendWait("D");
                    System.Threading.Thread.Sleep(100);
                }
            }
    先这样
    然后        private void button1_Click(object sender, EventArgs e)
            {
                if (thread == null)
                {
                    textBox1.Focus();
                    pressKey = true;
                    thread = new System.Threading.Thread(new System.Threading.ThreadStart(Press));
                    thread.Start();
                }
                else
                {//我这用这样比较保险了··
                    pressKey = false;
                    thread.Abort();
                    thread = null;
                }        }最后一个一定要加,要不忘记了就麻烦了
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (thread != null)
                    thread.Abort();
            }