Winform 按键组合键
设置属性KeyPreview 为True 和form的keydown事件
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Alt)
{
switch (e.KeyCode)
{
case Keys.Right://显示右边
IsShowRigth = true;
lvStatus.Visible = true;
break;
case Keys.Left://隐藏右边
IsShowRigth = false;
lvStatus.Visible = false;
break;
case Keys.Enter://开始和结束
break;
case Keys.Escape://退出程序
System.Environment.Exit(0);
break;
}
}
}
另一个
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Right:
break;
case Keys.Left:
break;
case Keys.Up://方向键不反应
break;
case Keys.Down:
break;
case Keys.Space:
break;
case Keys.Enter:
break;
}
//return false;//如果要调用KeyDown,这里一定要返回false才行,否则只响应重写方法里的按键.
//这里调用一下父类方向,相当于调用普通的KeyDown事件.//所以按空格会弹出两个对话框
return base.ProcessCmdKey(ref msg, keyData);
}