0

I have to catch when the user is pressing the up arrow on the keyboard, while the button has the focus. I have written this code to handle the KeyUp event for the button:

private void btnValider_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        //do stuff
    }
}

but this function didn't handle pressing the up arrow key.

I don't know if it what i want to do is possible or if i have to handle this event from the form ?

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
ucef
  • 557
  • 3
  • 10
  • 27
  • In your form add `PreviewKey = true;` while initialized. Then you can check for input in your form like `void meForm_KeyUp(object sender, KeyEventArgs e) { if(btnValidaer.HasFocus) { /*logic here*/ } }` Or you can add `IMessageFilter` interface, or you can override `WndProc(ref Message msg)` method – mrogal.ski Nov 29 '16 at 11:50
  • 2
    See the [documentation for `KeyUp`:](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup(v=vs.110).aspx) - _"Certain keys, such as the TAB, RETURN, ESC, and **arrow keys** are handled by controls automatically. To have these keys raise the KeyUp event, you must override the IsInputKey method in each control on your form. The code for the override of IsInputKey would need to determine if one of the special keys is pressed and return a value of true."_ – stuartd Nov 29 '16 at 11:52
  • @m.rogalski PreviewKey is available only on Form and it doesn't help catching arrow keys – Kodre Nov 29 '16 at 11:55
  • http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event/ – stuartd Nov 29 '16 at 11:56
  • @Kodre that's why i posted 3 methods. Besides I dont know what he was able to research till now. So answer will be unclear since it's a guess game. – mrogal.ski Nov 29 '16 at 11:59
  • 3
    The arrow keys and Tab are used for navigation. So you'll never get that event, it is intercepted before it can reach the button. Using KeyPreview is not a fix either. It isn't very clear what Keys.Up needs to do so the best solution is not obvious. Override ProcessCmdKey() for the universal fix. – Hans Passant Nov 29 '16 at 12:14
  • @hans : What i put on ProcessCmdKey. I have already override it to for keys.Tab : if (keyData == Keys.Tab). Should i have to put if (keyData == Keys.Tab) also ? – ucef Nov 29 '16 at 13:19
  • `// do stuff`. Why SO users refuse to document their code and insist on only getting guesses instead of answers is something I'll never understand. – Hans Passant Nov 29 '16 at 13:21
  • @Hans: i have not yet write the code // do stuff, because i am wondering how to solve my problem. But it's not relevant to the problem itself – ucef Nov 29 '16 at 13:26

2 Answers2

1

As Hans Passant suggested,

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Up && btnValider.Focused)
        {
            MessageBox.Show("hit");

            return true;
        }
        else
            return base.ProcessCmdKey(ref msg, keyData);
    }
Jeff Click
  • 889
  • 9
  • 20
  • 1
    it is not obliged to add the '&& btnValider.Focused'. 'if (keyData == Keys.Up)' is enough. I have test it. – ucef Nov 29 '16 at 13:38
-1

handle key down event for the button.

farrukh aziz
  • 162
  • 1
  • 2
  • 9
  • Same problem: _" Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the **KeyDown event** … "_ – stuartd Nov 29 '16 at 11:54