Detecting arrow keys in winforms C# and vb.net

arrow keys

Most Windows Forms applications process keyboard input exclusively by handling the keyboard events. You can detect most physical key presses by handling the KeyDown or KeyUp events. Key events occur in the following order:

  1. KeyDown
  2. KeyPress
  3. KeyUp

Handle arrow key events in c# and vb.net

Arrow Key movement on Forms c# and vb.net

left arrow key

In most cases, the standard KeyUp, KeyDown, and KeyPress events are enough to catch and handle keystrokes. However, not all controls raise these events for all keystrokes under all conditions. For example. The KeyPress event is raised for character keys while the key is pressed and then released. But This event is not raised by noncharacter keys, unlike KeyDown and KeyUp, which are also raised for noncharacter keys

Handle arrow key events in Windows Forms in C# and VB.Net

up arrow key

In order to capture keystrokes in a Forms control, you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().

Syntax:

C#:



VB.Net:



down arrow key

This ProcessCmdKey is called during message preprocessing to handle command keys, also it is called only when the control is hosted in a Windows Forms application or as an ActiveX control.

How to detect which Arrow Key is pressed ?

C# code for capture arrow keys.



VB.Net code for capture arrow keys.



KeyPress events in C# , VB.Net

Windows Forms processes keyboard input by raising keyboard events in response to Windows messages. Most Windows Forms programs process keyboard input by handling the keyboard events. You can detect most physical key presses by handling the KeyDown or KeyUp events. From the following link you can learn more about.... KeyPress events in C# and .. KeyPress events in VB.Net



NEXT.....Enum with Switch.Case