Control.KeyDown Event and KeyEventArgs

本文介绍如何使用 C# 在 Windows Forms 应用程序中处理键盘事件,包括 KeyDown 和 KeyPress 事件。通过示例代码展示了如何限制文本框仅接受数字输入,并提供了使用 Help 类为用户提供上下文敏感的帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PropertyDescription
AltGets a value indicating whether the ALT key was pressed.
ControlGets a value indicating whether the CTRL key was pressed.
HandledGets or sets a value indicating whether the event was handled.
KeyCodeGets the keyboard code for a KeyDown or KeyUp event.
KeyDataGets the key data for a KeyDown or KeyUp event.
KeyValueGets the keyboard value for a KeyDown or KeyUp event.
ModifiersGets the modifier flags for a KeyDown or KeyUp event. This indicates which combination of modifier keys (CTRL, SHIFT, and ALT) were pressed.
ShiftGets a value indicating whether the SHIFT key was pressed.

Control.KeyDown:

Occurs when a key is pressed while the control has focus.

 

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

 

Remarks


Key events occur in the following order:

  1. KeyDown

  2. KeyPress

  3. KeyUp

To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true. Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.

For more information about handling events, see Consuming Events.

Examples

The following code example uses the KeyDown event to determine the type of character entered into the control.

 

 

 

 

Gets the modifier flags for a KeyDown or KeyUp event. This indicates which combination of modifier keys (CTRL, SHIFT, and ALT) were pressed.

 

[C#]
public Keys Modifiers {get;}

 

Property Value

A Keys value representing one or more modifier flags.

Remarks

To determine whether a specific modifier key was pressed, use the Control, Shift, and Alt properties. Modifier flags can be combined with bitwise OR.

Example

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值