Input

Mouse Input

Input from the mouse can be obtained in an easy manner, through the use of the static Mouse class. The following three methods get the position of the mouse cursor.

  1. Point mousePosition = Mouse.Position;
  2.  int X = Mouse.X;
  3.  int Y = Mouse.Y;

The above properties have set methods also, so the mouse position can be changed. You can check them every frame, or if you prefer you can attach to the Mouse.MouseMove event, as in the next example.

  1. Mouse.MouseMove += new InputEventHandler(myHandler);

The Mouse also has a MouseDown and a MouseUp event that you can attach to in order to deal with mouse clicks.

Keyboard Input

Input from the keyboard is obtained in a similar manner to mouse input, through the static Keyboard class. You can check the state of any key with the Keys member, as in the following example.

  1. if (Keyboard.Keys[KeyCode.Enter]) // User pressed enter
  2. if (Keyboard.Keys[KeyCode.F5])  // User pressed F5

You can also attach to the Keyboard.KeyDown and Keyboard.KeyUp events.