新建一个窗口,然后放一个BUTTON。
我想做到的 是:在窗口上按下方向左键,则BUTTON向左移动,持续按下不放,则持续向左移动;按下我设置 this.KeyPreview ,在Form3_KeyDown和 Form3_KeyPress 中编程就是做不到。

解决方案 »

  1.   

    C#中因该有取得某个按键状态的函数 如果没有 可以引用API函数
    然后在你的函数中判断这个按键是否按下 如果按下 则执行你的事件.
    如果你函数写在OnKeyDown之类事件中的话 有可能不行 所以 最好把处理函数写在一个定时器中
    然后在里面判断按键是否按下.你应该明白吧?还有就是可以在按下第一次的时候(系统是能立即接收到的)保存一个有效标识
    然后在onkeyup中解除它的有效标识
    只要你没有把按键弹起 它就一直有值. 
    网上找的
      

  2.   

    private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
      switch (e.KeyCode)
      {
      case Keys.A:A(sender, e); break;
      case Keys.B: break;
      case Keys.Enter: break;   
      }
      }  
      

  3.   

    http://www.it588.com/openlab/forums/threads/344.aspx
      

  4.   

    我可以在Form3_KeyDown这样,但是使用如果使用Keys.Left right, up ,down 则它不会接收。
                switch (e.KeyCode )
                {
                    case Keys.W :
                        this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y - 2);break;
                    case Keys.S:
                        this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y + 2);break;
                    case Keys.A :
                        this.button1.Location = new Point(this.button1.Location.X - 2, this.button1.Location.Y); break;
                    case Keys.D :
                        this.button1.Location = new Point(this.button1.Location.X + 2, this.button1.Location.Y); break;                //case Keys.Left :
                    //    this.button1.Location = new Point(this.button1.Location.X - 2, this.button1.Location.Y);break;
                    //case Keys.Right :
                    //    this.button1.Location = new Point(this.button1.Location.X + 2, this.button1.Location.Y);break;
                    //default:
                    //    break;
                }
      

  5.   

    试过了 的确 Left这个键值不起作用~~而Keys偏偏有这个属性值~郁闷 谁能解释下!
      

  6.   

    这样是可以的!之所以不能移动,是因为button获取了焦点,你把button焦点移除掉,就可以!
    这里我把焦点放到textbox上就Ok了:        private void Form6_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode.ToString().ToUpper().Equals("LEFT"))
                {
                    this.button1.Left -= 2;
                }
            }        private void Form6_Load(object sender, EventArgs e)
            {
                this.textBox1.Focus();
            }
      

  7.   

    呵呵,这个给我的感觉就是俄罗斯方块的功能么下面这篇文章,就是讲怎么实现的,还有源代码游戏主要有四部分组成:Square类,Block类,gameField类,游戏引擎你所要的button控制功能在游戏引擎代码里,实现的不错你可以看看,文章地址:http://www.cnblogs.com/tuyile006/archive/2007/05/16/748256.html
      

  8.   


    看了,我更换了一下代码,但还是有点问题:        protected override bool ProcessDialogKey(Keys keyData)
            {
                switch (keyData)
                {
                    case Keys.Left: Move(1); break;
                    case Keys.Right: Move(3); break;
                    case Keys.Up: Move(0); break;
                    case Keys.Down: Move(2); break;            }
                return true;            //return base.ProcessDialogKey(keyData);
            }
            void Move(int i)
            {
                this.button1.Location = new Point(this.button1.Location.X - 2, this.button1.Location.Y);
                switch (i )
                {
                   case  0:this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y - 2);break;
                   case 1: this.button1.Location = new Point(this.button1.Location.X - 2, this.button1.Location.Y); break;
                   case 2: this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y + 2); break;
                   case 3: this.button1.Location = new Point(this.button1.Location.X + 2, this.button1.Location.Y); break;
                    default:
                        break;
                }
            }
    出现了一个 Warning :
                //Warning 1 'PersonalABC.Form3.Move(int)' hides inherited member 'System.Windows.Forms.Control.Move'. Use the new keyword if hiding was intended. 并且 BUTTON 的移动也有点问题,不知道错误在哪,我正在看
      

  9.   


    就是一个 FROM 和 BUTTON ,FROM 的 KeyPreview 为TRUE;在Form3_KeyDown 它不接收 Keys.Left 
    keys.up ,keys.right ,keys.down ,不是控件的问题
      

  10.   

    OK ,OK !谢谢大家的回答,解决问题了。我等下再总结。
      

  11.   

    可以这样,按下A,S,D,W 键移动 Button.        private void Form3_KeyDown(object sender, KeyEventArgs e)
            {
                //this.KeyPreview = true;
                //处理启动窗体的 KeyPress 或 KeyDown 事件,将窗体的 KeyPreview 属性设置为 true,使键盘消息在到达窗体上的任何控件之前先被窗体接收。            this.Text = string.Format("e.KeyCode:{0}   e.KeyData: {0}   e.KeyValue: {1}", e.KeyCode, e.KeyData, e.KeyValue);            switch (e.KeyCode)
                {
                    case Keys.W:
                        this.button1.Location = new Point(button1.Location.X, button1.Location.Y - 2); break;
                    case Keys.S:
                        this.button1.Location = new Point(button1.Location.X, button1.Location.Y + 2); break;
                    case Keys.A:
                        this.button1.Location = new Point(button1.Location.X - 2, button1.Location.Y); break;
                    case Keys.D:
                        this.button1.Location = new Point(button1.Location.X + 2, button1.Location.Y); break;                //方向键不能在KeyDown事件中被触发,以下代码无效。
                    //case Keys.Left:
                    //    this.button1.Location = new Point(this.button1.Location.X - 2, this.button1.Location.Y); break;
                    //case Keys.Right:
                    //    this.button1.Location = new Point(this.button1.Location.X + 2, this.button1.Location.Y); break;
                    default:
                        break;
                }
            }以下为按下方向键移动Button:       protected override bool ProcessDialogKey(Keys keyData)
            {
                switch (keyData)
                {
                    case Keys.Up: Move(0); break;
                    case Keys.Left: Move(1); break;
                    case Keys.Down: Move(2); break;
                    case Keys.Right: Move(3); break;
                }            return true; //返回 true 以指示已处理该键。
                //return base.ProcessDialogKey(keyData);
            }        void Move(int i)
            {
                switch (i)
                {
                    case 0: this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y - 2); break;
                    case 1: this.button1.Location = new Point(this.button1.Location.X - 2, this.button1.Location.Y); break;
                    case 2: this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y + 2); break;
                    case 3: this.button1.Location = new Point(this.button1.Location.X + 2, this.button1.Location.Y); break;
                    default:break;
                }
            }
    不过以上的代码它会返回一个警告:Warning 1'PersonalABC.Form3.Move(int)' hides inherited member 'System.Windows.Forms.Control.Move'. Use the new keyword if hiding was intended. 不知道如何处理。
      

  12.   

    form KeyPreview=true protected override bool ProcessDialogKey(Keys keyData)
            {
                keydown(keyData );
                return false ;
            }        private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                keydown(e.KeyData );
            }
            private void keydown(Keys keyData)
            {
                switch (keyData)
                {
                    case Keys.Left:
                        button1.Left -= 1;
                        break;
                    case Keys.Right:
                        button1.Left += 1;
                        break;
                    case Keys.Up:
                        button1.Top  -= 1;
                        break;
                    case Keys.Down:
                        button1.Top += 1;
                        break;
                    default :
                        label1.Text = "other";
                        break;
                }
            }
    很好用阿,当前控件有输入框的触发的是Form1_KeyDown,没有输入框的如Button触发的是ProcessDialogKey,有点怪,哈
      

  13.   

            private void Form1_Load(object sender, EventArgs e)
            {
                this.KeyPreview = true;
            }        private void Form3_KeyPress(object sender, KeyPressEventArgs e)
            {
                switch (e.KeyChar)
                {
                    case 'w': button1.Top  -= 10; break;
                    case 'a': button1.Left -= 10; break;
                    case 's': button1.Top  += 10; break;
                    case 'd': button1.Left += 10; break;
                }        }
      

  14.   

    参考其它一些贴的使用方法后,对代码作了最后一次的改良,呵呵。        protected override bool ProcessDialogKey(Keys keyData)
            {
                switch (keyData )
                {
                    case Keys.Up: Move(0); break;
                    case Keys.Left: Move(1); break;
                    case Keys.Down: Move(2); break;
                    case Keys.Right: Move(3); break;
                    default: break;
                }           return false;   //返回 FALSE 好些,这样还可以在Form3_KeyDown中
               //return base.ProcessDialogKey(keyData);
            }        void Move(int T)
            {
                switch (T )
                {
                    case 0: button1.Top -= 3; break;
                    case 1: button1.Left -= 3; break;
                    case 2: button1.Top += 3; break;
                    case 3: button1.Left += 3; break;
                    default:break;
                }
            }
            private void Form3_KeyDown(object sender, KeyEventArgs e)
            {
                this.Text = string.Format("e.KeyCode:{0}   e.KeyData: {0}   e.KeyValue: {1}", e.KeyCode, e.KeyData, e.KeyValue);
                if (e.KeyCode == Keys.O)
                {
                    MessageBox.Show("ok");
                }        }        private void Form1_Load(object sender, EventArgs e)
            {
                this.KeyPreview = true;
            }如果不需要使用方向键移动BUTTON,可以使用17楼的。
      

  15.   

    http://topic.csdn.net/u/20090602/20/970c8922-1f8a-4005-bc7f-466a189c51c8.html