新建一个窗口,然后放一个BUTTON。
我想做到的 是:在窗口上按下方向左键,则BUTTON向左移动,持续按下不放,则持续向左移动;按下我设置 this.KeyPreview ,在Form3_KeyDown和 Form3_KeyPress 中编程就是做不到。
我想做到的 是:在窗口上按下方向左键,则BUTTON向左移动,持续按下不放,则持续向左移动;按下我设置 this.KeyPreview ,在Form3_KeyDown和 Form3_KeyPress 中编程就是做不到。
调试欢乐多
然后在你的函数中判断这个按键是否按下 如果按下 则执行你的事件.
如果你函数写在OnKeyDown之类事件中的话 有可能不行 所以 最好把处理函数写在一个定时器中
然后在里面判断按键是否按下.你应该明白吧?还有就是可以在按下第一次的时候(系统是能立即接收到的)保存一个有效标识
然后在onkeyup中解除它的有效标识
只要你没有把按键弹起 它就一直有值.
网上找的
{
switch (e.KeyCode)
{
case Keys.A:A(sender, e); break;
case Keys.B: break;
case Keys.Enter: break;
}
}
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;
}
这里我把焦点放到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();
}
看了,我更换了一下代码,但还是有点问题: 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 的移动也有点问题,不知道错误在哪,我正在看
就是一个 FROM 和 BUTTON ,FROM 的 KeyPreview 为TRUE;在Form3_KeyDown 它不接收 Keys.Left
keys.up ,keys.right ,keys.down ,不是控件的问题
{
//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. 不知道如何处理。
{
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,有点怪,哈
{
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;
} }
{
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楼的。