[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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

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.
0
JoeD77
Asked:
JoeD77
1 Solution
 
Umar Topia.Net Full Stack DeveloperCommented:
0
 
AndyAinscowCommented:
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.

0
 
saraganiCommented:
Winform or WPF?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
JoeD77Author 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?
0
 
saraganiCommented:
I will assume that you are using Winforms since you have not replied my question on my previous post.
With that, I will now suggest you to use the event: PreviewKeyDown

This is the event that comes before KeyDown and therefore gets the "KeyDown" for the arrows.
0
 
JoeD77Author 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 !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now