【发布时间】:2015-05-05 22:44:48
【问题描述】:
我试图在按下“Ctrl+l”时显示 2 个不同的消息框,在按下“Shift+A" 也被按下。我已经完成了一切,但是当我在程序运行时按下这些按钮时,什么也没有发生。我不确定我做错了什么。
我的代码如下:
public Color()
{
InitializeComponent();
ContextMenuStrip s = new ContextMenuStrip();
ToolStripMenuItem directions = new ToolStripMenuItem();
directions.Text = "Directions";
directions.Click += directions_Click;
s.Items.Add(directions);
this.ContextMenuStrip = s;
this.KeyDown += new KeyEventHandler(Color_KeyDown);//Added
}
void directions_Click(object sender, EventArgs e)
{
MessageBox.Show("Just click on a color chip to see the translation");
}
//Keypress
private void Color_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L && (e.Control))
{
MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
}
else if (e.KeyCode == Keys.A && (e.Shift))
{
MessageBox.Show("This is version 1.0");
}
}
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
对不起,会的
-
看看stackoverflow.com/a/400325/49251,可能有用。
-
键盘事件在具有焦点的控件上引发。如果它有任何控件,那几乎就不是表单。使用 ProcessCmdKey() 实现快捷键,请考虑使用菜单以使其易于发现。
标签: c# winforms keypress keyboard-events