Input devices refer to hardware components through which users interact with a computer or software application. In the context of C# and Windows Forms (WinForms) or WPF applications, input devices such as the keyboard, mouse, touchscreens, and stylus are commonly used to capture user input.
C# provides various ways to handle input from these devices, allowing developers to create interactive user interfaces that respond to user actions.
Keyboard:
KeyPress, KeyDown, and KeyUp in Windows Forms or WPF applications.Mouse:
MouseClick, MouseMove, MouseDown, MouseUp, and MouseWheel.Touch:
Stylus / Pen:
StylusDown, StylusMove, and StylusUp.In Windows Forms, keyboard input is typically captured using key events. The key events allow you to handle when a key is pressed, released, or typed.
KeyDown Event:
Example of using KeyDown event:
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed!");
}
}
KeyUp Event:
Example of using KeyUp event:
private void Form_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Escape key released!");
}
}
KeyPress Event:
Example of using KeyPress event:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
char key = e.KeyChar;
MessageBox.Show($"Key Pressed: {key}");
}
Handling Modifier Keys:
KeyDown or KeyUp events.Example:
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
MessageBox.Show("Ctrl + C pressed!");
}
}
Mouse input events allow you to capture various actions like clicking, moving, and dragging. Common mouse-related events include MouseClick, MouseDown, MouseUp, MouseMove, and MouseWheel.
MouseDown Event:
Example of using MouseDown event:
private void Form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("Left mouse button clicked!");
}
}
MouseUp Event:
Example of using MouseUp event:
private void Form_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Right mouse button released!");
}
}
MouseMove Event:
Example of using MouseMove event:
private void Form_MouseMove(object sender, MouseEventArgs e)
{
MessageBox.Show($"Mouse moved to X: {e.X}, Y: {e.Y}");
}
MouseClick Event:
Example of using MouseClick event:
private void Form_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show($"Mouse clicked at X: {e.X}, Y: {e.Y}");
}
MouseWheel Event:
Example of using MouseWheel event:
private void Form_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
{
MessageBox.Show("Mouse wheel scrolled up");
}
else
{
MessageBox.Show("Mouse wheel scrolled down");
}
}
Handling touch or stylus input typically requires working with WPF (Windows Presentation Foundation), which has built-in support for these types of input. Touch events can be captured using events like TouchDown, TouchMove, and TouchUp.
TouchDown Event:
Example of using TouchDown event in WPF:
private void Window_TouchDown(object sender, TouchEventArgs e)
{
MessageBox.Show("Touch point down at: " + e.GetTouchPoint(this).Position);
}
TouchMove Event:
Example of using TouchMove event in WPF:
private void Window_TouchMove(object sender, TouchEventArgs e)
{
MessageBox.Show("Touch point moved to: " + e.GetTouchPoint(this).Position);
}
TouchUp Event:
Example of using TouchUp event in WPF:
private void Window_TouchUp(object sender, TouchEventArgs e)
{
MessageBox.Show("Touch point lifted from: " + e.GetTouchPoint(this).Position);
}
Stylus Input:
StylusDown, StylusMove, and StylusUp to detect and respond to pen input.Example of using StylusDown in WPF:
private void Window_StylusDown(object sender, StylusDownEventArgs e)
{
MessageBox.Show("Stylus touched down at: " + e.GetPosition(this));
}
Gestures such as swiping, pinching, or tapping are more advanced touch interactions commonly used in touch-based applications. In C#, these are typically handled through Gesture Recognition in more specialized libraries, but you can also manually track touch movement or use touch points for simple gestures.
For example, pinch zooming could be implemented by comparing the distance between two touch points in a TouchMove event.
Input devices in C# refer to hardware tools (keyboard, mouse, touch, and stylus) used to interact with a computer. C# offers various events for capturing and responding to input from these devices, enabling developers to create rich, interactive applications.
KeyDown, KeyUp, and KeyPress.MouseClick, MouseDown, MouseMove, MouseUp, and MouseWheel.TouchDown, TouchMove, TouchUp, and StylusDown.Understanding these events and how to handle input devices efficiently allows you to build interactive and responsive applications in C#.
Open this section to load past papers