Files
docs-desktop/dotnet-desktop-guide/framework/winforms/how-to-handle-keyboard-input-at-the-form-level.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

3.2 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Handle Keyboard Input at the Form Level Learn how to handle keyboard input for your Windows Forms at the form level, before messages reach a control. 03/30/2017
csharp
vb
cpp
keyboard input [Windows Forms], at form level
Windows Forms, handling keyboard input
keyboards [Windows Forms], form-level input
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

Example

The following code example is the entire application for the above example. The application includes a xref:System.Windows.Forms.TextBox along with several other controls that allow you to move focus from the xref:System.Windows.Forms.TextBox. The xref:System.Windows.Forms.Control.KeyPress event of the main xref:System.Windows.Forms.Form consumes '1', '4', and '7', and the xref:System.Windows.Forms.Control.KeyPress event of the xref:System.Windows.Forms.TextBox consumes '2', '5', and '8' while displaying the remaining keys. Compare the xref:System.Windows.Forms.MessageBox output when you press a number key while the xref:System.Windows.Forms.TextBox has focus with the xref:System.Windows.Forms.MessageBox output when you press a number key while focus is on one of the other controls.

[!code-cppSystem.Windows.Forms.KeyBoardInputForm#0] [!code-csharpSystem.Windows.Forms.KeyBoardInputForm#0] [!code-vbSystem.Windows.Forms.KeyBoardInputForm#0]

Compiling the Code

This example requires:

  • References to the System, System.Drawing and System.Windows.Forms assemblies.

See also