Form1 form = new Form1();
form.KeyPress +=new KeyPressEventHandler(form.form_Press);
...
private void form_Press(object sender,KeyPressEventArgs e)
{
//在这里面如何才能获取到方向键呢,望高手赐教.谢谢!
}
2 个解决方案
#1
KeyPress捕获的是可接受输入的字符,不包括方向键
得用KeyDown捕获
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
//..
}
}
得用KeyDown捕获
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
//..
}
}
#2
得重写一个基类方法,把这一段加入form
protected override bool ProcessCmdKey(ref Message msg, Keys e)
{
if (e == Keys.Up || e == Keys.Down || e == Keys.Left || e == Keys.Right)
{
你得代码
}
return true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys e)
{
if (e == Keys.Up || e == Keys.Down || e == Keys.Left || e == Keys.Right)
{
你得代码
}
return true;
}
#1
KeyPress捕获的是可接受输入的字符,不包括方向键
得用KeyDown捕获
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
//..
}
}
得用KeyDown捕获
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
//..
}
}
#2
得重写一个基类方法,把这一段加入form
protected override bool ProcessCmdKey(ref Message msg, Keys e)
{
if (e == Keys.Up || e == Keys.Down || e == Keys.Left || e == Keys.Right)
{
你得代码
}
return true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys e)
{
if (e == Keys.Up || e == Keys.Down || e == Keys.Left || e == Keys.Right)
{
你得代码
}
return true;
}