Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Up, Down, Left and Right arrow keys do not trigger KeyDown event

I am building an application where all the key input must be handled by the windows itself.

I set tabstop to false for each control witch could grab the focus except a panel (but I don't know if it has effect).

I set KeyPreview to true and I am handling the KeyDown event on this form.

My problem is that sometimes the arrow key aren't responsive anymore:

  • The keydown event is not fired when I pressed only an arrow key.

  • The keydown event is fired if I press an arrow key with the control modifier.

Have you an idea why my arrow key suddenly stop firing event?

like image 222
Martin Delille Avatar asked Oct 29 '09 22:10

Martin Delille


People also ask

Why don't my right and left arrow keys work?

Look at your keyboard and see if you can find a Scroll Lock key (which might be abbreviated, such as SCRLK). If it has an illuminated status light, press the key to turn off the light. Then try Excel again. With luck, this solved your problem.

What do the up and down arrow keys do?

Use the right and left arrow keys to move between columns and up and down arrow keys to move between rows.

What is the difference between KeyDown and keypress events?

The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

What is KeyDown event in Visual Basic?

KeyDown event: The KeyDown event occurred when a user presses a key on the keyboard. It repeats while the user keeps the key depressed. The KeyDown event is provided by the Keyboard event in the VB.NET application. KeyUp event: The KeyUp event is rising when a user releases a key on the keyboard.


2 Answers

I was having the exact same problem. I considered the answer @Snarfblam provided; however, if you read the documentation on MSDN, the ProcessCMDKey method is meant to override key events for menu items in an application.

I recently stumbled across this article from microsoft, which looks quite promising: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx. According to microsoft, the best thing to do is set e.IsInputKey=true; in the PreviewKeyDown event after detecting the arrow keys. Doing so will fire the KeyDown event.

This worked quite well for me and was less hack-ish than overriding the ProcessCMDKey.

like image 177
Rodolfo Neuber Avatar answered Oct 13 '22 09:10

Rodolfo Neuber


    protected override bool IsInputKey(Keys keyData)     {         switch (keyData)         {             case Keys.Right:             case Keys.Left:             case Keys.Up:             case Keys.Down:                 return true;             case Keys.Shift | Keys.Right:             case Keys.Shift | Keys.Left:             case Keys.Shift | Keys.Up:             case Keys.Shift | Keys.Down:                 return true;         }         return base.IsInputKey(keyData);     }     protected override void OnKeyDown(KeyEventArgs e)     {         base.OnKeyDown(e);         switch (e.KeyCode)         {             case Keys.Left:             case Keys.Right:             case Keys.Up:             case Keys.Down:                 if (e.Shift)                 {                  }                 else                 {                 }                 break;                         }     } 
like image 41
alpha Avatar answered Oct 13 '22 08:10

alpha