Winform中Control.KeyDown 事件(整理自MSDN)

事件说明:

在控件有焦点的情况下按下键时发生(这里也没说是任意键,因为有些键在KeyDown中也是处理不了的)。

原型:

public event KeyEventHandler KeyDown

 

相关键事件按下列顺序发生:

  1. KeyDown

  2. KeyPress

  3. KeyUp

注意:

若要仅在窗体级别处理键盘事件而不允许其他控件接收键盘事件,请将窗体的 KeyPress 事件处理方法中的 KeyPressEventArgs.Handled 属性设置为 true。某些键,如 Tab、Return、Esc 和箭头键,由控件自动处理。为使这些键引发 KeyDown 事件,必须在窗体上的每个控件中重写 IsInputKey 方法。用于重写 IsInputKey 的代码需要确定是否按下了某个特殊键,并且需要返回一个 true 值。

 

下面的代码示例使用 KeyDown 事件来确定输入到控件中的字符类型:

// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}


事件参数说明:

KeyEventArgs

 KeyDown 或 KeyUp 事件提供数据。

参数说明:

KeyEventArgs(指定用户曾按下的键以及是否曾同时按下修改键:Ctrl、Alt 和 Shift 键)随着 KeyDown 或 KeyUp 事件的每一次发生而被传递。

当用户按下任意键时,发生 KeyDown 事件。当用户释放该键时,发生 KeyUp 事件。如果键被按住,那么,每次键重复时,KeyDown 事件都重复发生;但当用户释放该键时,仅发生一次 KeyUp 事件。

按下某个键时,还发生 KeyPress 事件。KeyPressEventArgs 随着 KeyPress 事件的每一次发生而被传递,并指定每次按键结果所形成的字符。

 

下面的代码示例演示如何检测按键的状态:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the key entered is the F1 key. If it is, display Help.
    if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
    {
        // Display a pop-up Help topic to assist the user.
        Help.ShowPopup(textBox1, "Enter your name.", new Point(textBox1.Bottom, textBox1.Right));
    }
    else if(e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
    {
        // Display a pop-up Help topic to provide additional assistance to the user.
        Help.ShowPopup(textBox1, "Enter your first name followed by your last name. Middle name is optional.",
            new Point(textBox1.Top, this.textBox1.Left));
    }
}


 

private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}


只是拷贝了部分代码。

 

参数属性的说明:

KeyData 属性说明:

一个 Keys,表示按下的键的键代码以及修饰符标志(指示同时按下的 Ctrl、Shift 和 Alt 键的组合)。

可使用来自 Keys 的常数从 KeyData 属性提取信息。使用位 AND 运算符将由 KeyData 返回的数据与 Keys 中的常数进行比较,从而获得有关用户按下的键的信息。若要确定是否已按了特定的修改键,请使用 ControlShift 和 Alt 属性。

KeyValue 属性说明:

KeyCode 属性的整数表示形式。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值