Displays a key pressed by the user

using System.Drawing;
using System;
using System.Windows.Forms;
public class TryKey : Form {
private char theKey = 'd';
public TryKey() {
Size = new Size(300,200);
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawString(theKey.ToString(), new Font("Arial", 36, FontStyle.Bold), Brushes.Red, 100, 50);
base.OnPaint(e);
}
protected override void OnKeyDown(KeyEventArgs e){
if (e.Control){
Console.WriteLine("Control");
}
if (e.KeyCode == Keys.Right){
Console.WriteLine("Right");
}
else if (e.KeyCode == Keys.Left){
Console.WriteLine("Left");
}
Invalidate();
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e) {
Console.WriteLine("Key Up");
base.OnKeyUp(e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
if (char.IsLetterOrDigit(e.KeyChar)){
theKey = e.KeyChar;
}
Invalidate();
base.OnKeyPress(e);
}
public static void Main() {
Application.Run(new TryKey());
}
}
Related examples in the same category