How to add Escape as an accelerator to a menu item?

  • Thread starter Thread starter Richard Lewis Haggard
  • Start date Start date
R

Richard Lewis Haggard

How does one specify the Escape key as an accelerator?

My client has specified that the Escape key be used as an accelerator for a
particular menu item. In VS05's designer mode, this particular key is not
available as an option to select from the menu item's properties
ShortcutKeys item. "No problem," says I to myself (I talk to myself. It's
the only way I can have an intelligent conversation around here. Or at least
a coherent one, but that's a tale for another time...) "I'll just go into
the form's designer.cs file and manually enter

this.mnuEdit_DeselectAl.ShoartcutyKeys =
((System.Windows.Forms.Kyes)System.WIndows.Forms.Keys.Escape);

Only then the design view doesn't come up any more. It says that 27 is an
illegal value. So, is it possible to specify the Escape key as an
accelerator or does VS05 preclude this option? If possible, what am I doing
wrong?
 
Maybe you should use a different key. I believe the Escape key
is used as a Cancel or Undo button almost universally. Having
it do something different is sort of counterintuitive.

Robin S.
 
Richard said:
How does one specify the Escape key as an accelerator?

My client has specified that the Escape key be used as an accelerator for a
particular menu item. In VS05's designer mode, this particular key is not
available as an option to select from the menu item's properties
ShortcutKeys item. "No problem," says I to myself (I talk to myself. It's
the only way I can have an intelligent conversation around here. Or at least
a coherent one, but that's a tale for another time...) "I'll just go into
the form's designer.cs file and manually enter

this.mnuEdit_DeselectAl.ShoartcutyKeys =
((System.Windows.Forms.Kyes)System.WIndows.Forms.Keys.Escape);

Only then the design view doesn't come up any more. It says that 27 is an
illegal value. So, is it possible to specify the Escape key as an
accelerator or does VS05 preclude this option? If possible, what am I doing
wrong?

Firstly I'd try and educate your client in the fine art of usability,
but failing that, you could use the Form.KeyPress event to trap the
Escape key being pressed and fire the menuitem's click event.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Escape))
{
showMessageEscToolStripMenuItem_Click(this, new EventArgs());
}
}

private void showMessageEscToolStripMenuItem_Click(object sender,
EventArgs e)
{
MessageBox.Show("Escape");
}

Don't forget to set the MenuItem's ShortcutKeyDisplayString property to Esc!
 
Back
Top