--- title: "How to: Handle Keyboard Input at the Form Level" description: Learn how to handle keyboard input for your Windows Forms at the form level, before messages reach a control. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "keyboard input [Windows Forms], at form level" - "Windows Forms, handling keyboard input" - "keyboards [Windows Forms], form-level input" ms.assetid: d7f8b390-dc91-42d2-ae0f-2ffa388127ad --- # How to: Handle Keyboard Input at the Form Level Windows Forms provides the ability to handle keyboard messages at the form level, before the messages reach a control. This topic shows how to accomplish this task. ### To handle a keyboard message at the form level - Handle the or event of the startup form, and set the property of the form to `true` so that keyboard messages are received by the form before they reach any controls on the form. The following code example handles the event by detecting all of the number keys and consuming '1', '4', and '7'. [!code-cpp[System.Windows.Forms.KeyboardInputForm#10](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInputForm/cpp/form1.cpp#10)] [!code-csharp[System.Windows.Forms.KeyboardInputForm#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInputForm/CS/form1.cs#10)] [!code-vb[System.Windows.Forms.KeyboardInputForm#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInputForm/VB/form1.vb#10)] ## Example The following code example is the entire application for the above example. The application includes a along with several other controls that allow you to move focus from the . The event of the main consumes '1', '4', and '7', and the event of the consumes '2', '5', and '8' while displaying the remaining keys. Compare the output when you press a number key while the has focus with the output when you press a number key while focus is on one of the other controls. [!code-cpp[System.Windows.Forms.KeyBoardInputForm#0](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInputForm/cpp/form1.cpp#0)] [!code-csharp[System.Windows.Forms.KeyBoardInputForm#0](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInputForm/CS/form1.cs#0)] [!code-vb[System.Windows.Forms.KeyBoardInputForm#0](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInputForm/VB/form1.vb#0)] ## Compiling the Code This example requires: - References to the System, System.Drawing and System.Windows.Forms assemblies. ## See also - [Keyboard Input in a Windows Forms Application](keyboard-input-in-a-windows-forms-application.md)