When a ContextMenuStrip
is opened, if the user types the first letter of a possible selection - it's as if he clicked on it. I want to intercept that and get the character that he clicked.
The following code does that, but with hard-coding the possible character. I want a generic way to do this, either by disabling the automatic selection by key stroke (leaving only the mouse clicks) or some way to intercept the character.
The following simplified code assumes I want to have a winform's Text
to be the character typed, and the ContextMenuStrip
has one option which is "A".
public Form1()
{
InitializeComponent();
contextMenuStrip1.KeyDown += contextMenuStrip1_KeyDown;
}
private void button1_Click(object sender, EventArgs e)
{
contextMenuStrip1.Show();
}
void contextMenuStrip1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
if (e.KeyCode == Keys.A)
{
if (e.Shift) Text = "A";
else Text = "a";
}
}
Using the KeyPress
event and checking e.KeyChar
doesn't work because it doesn't get fired. (the "A" click-event gets fired instead.)
Using one of these: e.KeyCode
, e.KeyData
, or e.KeyValue
doesn't work (without further hard-coding) because they accept a Shift
as a Key.