Handle Keyboard Input at the Form Level and Control Level

2011-02-17


When you handle keyboard input in Windows Form, you have to control them in at least two level: Form level and Control level.

At Form level, you MUST set the **KeyPreview **property of the form to true so that keyboard messages are received by the form before they reach any controls on the form when Handle the KeyPress or KeyDown event of the form.

Firstly I show you our sample for the hotkey F4:

(Do not use KeyPress event because KeyPress event can not use e.KeyCode == Keys.F4 kinds of code)

        #region Hot Keys
        private void Main_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4)
            {
                StateView pointMonitorView = new StateView();
                pointMonitorView.ShowDialog(this);
            }
        }
        #endregion

The following sample from Microsoft:

_The application includes a TextBox along with several other controls that allow you to move focus from the TextBox. The KeyPress event of the main Form consumes '1', '4', and '7', and the KeyPress event of the TextBox consumes '2', '5', and '8' while displaying the remaining keys. Compare the MessageBox output when you press a number key while the TextBox has focus with the MessageBox output when you press a number key while focus is on one of the other controls. _

using System;
using System.Drawing;
using System.Windows.Forms;

namespace KeyboardInputForm
{
    class Form1 : Form
    {
        TextBox TextBox1 = new TextBox();

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.AutoSize = true;

            FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.AutoSize = true;
            panel.FlowDirection = FlowDirection.TopDown;
            panel.Controls.Add(TextBox1);
            this.Controls.Add(panel);

            this.KeyPreview = true;
            this.KeyPress +=
                new KeyPressEventHandler(Form1_KeyPress);
            TextBox1.KeyPress +=
                new KeyPressEventHandler(TextBox1_KeyPress);
        }

        // Detect all numeric characters at the form level and consume 1, 
        // 4, and 7. Note that Form.KeyPreview must be set to true for this
        // event handler to be called.
        void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Form.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)49:
                    case (char)52:
                    case (char)55:
                        MessageBox.Show("Form.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }

        // Detect all numeric characters at the TextBox level and consume  
        // 2, 5, and 8.
        void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Control.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)50:
                    case (char)53:
                    case (char)56:
                        MessageBox.Show("Control.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }
    }
}