// keyboard.cs
// C:>csc /t:winexe keyboard.cs
using
System.Drawing; // need this for Point e.g. new
Point(10,10)
// Well we want our program to be in a GUI window, so we need to inherit
// from Form, which is in the library 'System.Windows.Forms'.
class
SimpleWindow : System.Windows.Forms.Form
{
int m_xPos=100;
int m_yPos=100;
private void SimpleWindow_KeyDown(object
sender, System.Windows.Forms.KeyEventArgs e)
{
string result =
e.KeyData.ToString();
switch (result)
{
case "Left":
m_xPos -= 5;
break;
case "Right":
m_xPos += 5;
break;
case "Up":
m_yPos -= 5;
break;
case "Down":
m_yPos += 5;
break;
default:
break;
}
// Move our window based on its new
coordinates
this.Location =
new Point (m_xPos,m_yPos);
}
// Window constructor
SimpleWindow()
{
this.KeyDown +=
new System.Windows.Forms.KeyEventHandler(this.SimpleWindow_KeyDown);
}
// Our program entry point.
static void
Main()
{
System.Windows.Forms.Application.Run(new
SimpleWindow());
}
} |