Catch key presses in a Windows Forms application

Simple enough to do.

  1. Set the “KeyPreview” property of the form to “True”
  2. Create a KeyUp event handler, eg
    private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
      if(e.KeyCode == Keys.F12)
      {
        // Do something here
      }
    }
  3. Wire the event handler up to the event

That is all.