We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Not detecting UP or DOWN arrow not working in buttons, but working in textboxes

JoeD77
JoeD77 asked
on
Medium Priority
332 Views
Last Modified: 2012-05-11
Hello.
I'm trying to allow a user to specify desired hotkeys, and all is well but this one problem: I'm not able to detect when UP arrow or DOWN arrow are pressed when using a button event (textbox events work fine). I think it has to do with the focus changing when these keys are pressed, and consequently not executing the KeyDown event.

Here's some example code:
WORKS:
       private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
           {
               MessageBox.Show("Found");
           }
       }

Open in new window


DOES NOT WORK:
        public void button1_KeyDown(object sender, KeyEventArgs e)
        {
                if (e.KeyCode == Keys.Up)
                {
                    MessageBox.Show("Found Up");
                }

Open in new window


Any ideas?

Thanks for the time.
Comment
Watch Question

Umar Topia.Net Full Stack Developer
CERTIFIED EXPERT

Commented:
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
From the help files about KeyDown (I've highlighted the answer to your question):

To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true. Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.

Commented:
Winform or WPF?

Author

Commented:
I now have a working solution when using only one button:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
      if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right) {
        button1_KeyDown(this, new KeyEventArgs(keyData));
        return true;
      }
      return base.ProcessCmdKey(ref msg, keyData);
    }

Open in new window


but I have many buttons in my form, and this piece of code will only handle events from button1... is there a way I can make it send back out the new KeyEventArgs to the button it's triggered by?
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
yeah sorry I wasn't sure why you were asking that so I forgot to address it...

anyway, changing it to the PreviewKeyDown event worked great, thanks for your persistence in making sure I understood that !
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.