WinForm开发中使用快捷键需要在Form的属性栏中将Keypreview设为true
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
Application.Exit();
break;
case Keys.Delete:
btnClearLine_Click(null, null);
break;
case Keys.F1:
AboutBox aboutBox = new AboutBox();
aboutBox.ShowDialog();
break;
}
if (e.Control && e.KeyCode == Keys.O)//按下组合键ctrl+o
{
btnOpenData_Click(null, null);
}
if (e.Control && e.KeyCode == Keys.Delete)//按下组合键ctrl+delete
{
btnAllClear_Click(null, null);
}
}
如何判断组合键
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
{
((TextBox)sender).SelectAll();
}