There's a much easier way to do this that doesn't require over-riding 'OnKeyPress. Assume the KeyDown and KeyUp EventHandlers shown below are wired-up to your MDIChildForm, and that its KeyPreview Property is set to 'true.
private bool IsAltDown = false;
private bool IsToolTipsShown = false;
private List<Keys> keyList = new List<Keys> { Keys.K, Keys.L, Keys.U };
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
IsAltDown = Control.ModifierKeys == Keys.Alt;
if (IsAltDown)
{
toolTip1.Show("U", this, pictureBox1.Location);
toolTip2.Show("K", this, pictureBox2.Location);
toolTip3.Show("L", this, pictureBox3.Location);
IsToolTipsShown = true;
}
if (IsAltDown && keyList.Contains(e.KeyCode))
{
switch (e.KeyCode)
{
case Keys.K:
textBox1.Text = "K";
break;
case Keys.L:
textBox1.Text = "L";
break;
case Keys.U:
textBox1.Text = "U";
break;
}
textBox1.Text += " pressed with Alt Key Down";
}
}
private void Form2_KeyUp(object sender, KeyEventArgs e)
{
if(IsToolTipsShown && ! (Control.ModifierKeys == Keys.Alt))
{
toolTip1.Hide(this);
toolTip2.Hide(this);
toolTip3.Hide(this);
IsToolTipsShown = false;
}
}
You'll note that I wrote a message to a TextBox on the Form rather showing a MessageBox: that's done to avoid the sometimes strange side-effects of "going modal" (suspending the current thread) when developing and intercepting events like Key Events.
I also used the Controls.ModifierKeys Property to determine whether the Alt Key is down, or up.