Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From an MSDN article:</p> <blockquote> <p>Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. For example, by default, a Button control ignores the arrow keys. Pressing the arrow keys typically causes the focus to move to the previous or next control. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.</p> </blockquote> <p>I am quoting the code I found there. I have not tested it</p> <pre><code>public Form1() { InitializeComponent(); // Form that has a button on it button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown); button1.KeyDown += new KeyEventHandler(button1_KeyDown); button1.ContextMenuStrip = new ContextMenuStrip(); // Add items to ContextMenuStrip button1.ContextMenuStrip.Items.Add("One"); button1.ContextMenuStrip.Items.Add("Two"); button1.ContextMenuStrip.Items.Add("Three"); } // By default, KeyDown does not fire for the ARROW keys void button1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Down: case Keys.Up: if (button1.ContextMenuStrip != null) { button1.ContextMenuStrip.Show(button1, new Point(0, button1.Height), ToolStripDropDownDirection.BelowRight); } break; } } // PreviewKeyDown is where you preview the key. // Do not put any logic here, instead use the // KeyDown event after setting IsInputKey to true. private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { switch (e.KeyCode) { case Keys.Down: case Keys.Up: e.IsInputKey = true; break; } } </code></pre> <p>Here is the link <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx</a></p> <p>Hope this helps you, good luck.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload