Winform下KeyDown,KeyPress,KeyUp事件的总结

原文链接:http://www.cnblogs.com/xiashengwang/archive/2011/09/15/2578798.html

在winform程序中,经常会用到这几个事件用于控制数字输入,按键动作等操作,但一直没有完全弄清楚他们之间的区别和联系,到底什么时候用哪一个事件合适,闲暇无事,做了一个小小的总结,以免以后犯糊涂。

1) 这三个事件调用的先后顺序(MSDN)

     1. KeyDown    :在控件有焦点的情况下按下键时发生

     2. KeyPress   :在控件有焦点的情况下按下键时发生。

     3. KeyUp         :在控件有焦点的情况下释放键时发生。

2) KeyDown和KeyPress在MSDN上的解释完全一样,都是在按下键的时候发生,那区别是什么呢?

    textBox1_KeyDown(object sender, KeyEventArgs e)  
    textBox1_KeyPress(object sender, KeyPressEventArgs e)  
    textBox1_KeyUp(object sender, KeyEventArgs e)  

从时间函数传入的事件参数可以看出,KeyDown和KeyUp用的是KeyEventArgs,KeyPress 用的是KeyPressEventArgs。

查阅MSDN,KeyEventArgs 提供了KeyCode,KeyData等System.Windows.Forms.Keys里定义的枚举。如下:

    using System;  
    using System.ComponentModel;  
    using System.Drawing.Design;  
    using System.Runtime.InteropServices;  
      
    namespace System.Windows.Forms  
    {  
        // 概要:  
        //     Specifies key codes and modifiers.  
        [Flags]  
        [ComVisible(true)]  
        [TypeConverter(typeof(KeysConverter))]  
        [Editor("System.Windows.Forms.Design.ShortcutKeysEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]  
        public enum Keys  
        {  
            // 概要:  
            //     The bitmask to extract modifiers from a key value.  
            Modifiers = -65536,  
            //  
            // 概要:  
            //     No key pressed.  
            None = 0,  
            //  
            // 概要:  
            //     The left mouse button.  
            LButton = 1,  
            //  
            // 概要:  
            //     The right mouse button.  
            RButton = 2,  
            //  
            // 概要:  
            //     The CANCEL key.  
            Cancel = 3,  
            //  
            // 概要:  
            //     The middle mouse button (three-button mouse).  
            MButton = 4,  
            //  
            // 概要:  
            //     The first x mouse button (five-button mouse).  
            XButton1 = 5,  
            //  
            // 概要:  
            //     The second x mouse button (five-button mouse).  
            XButton2 = 6,  
            //  
            // 概要:  
            //     The BACKSPACE key.  
            Back = 8,  
            //  
            // 概要:  
            //     The TAB key.  
            Tab = 9,  
            //  
            // 概要:  
            //     The LINEFEED key.  
            LineFeed = 10,        
            //  
            // 概要:  
            //     The CAPS LOCK key.  
            Capital = 20,  
            //内容太多了,略去。。。。。。  
            // 概要:  
            //     The ALT modifier key.  
            Alt = 262144,  
        }  
    }  

KeyPressEventArgs里自只定义了个KeyChar,并且是char型的。因此:

----要处理与按键相关的操作只能在KeyDown里处理,如是否按了Ctrl键或是同时按下了Ctrl+A键,诸如此类的按键判断都应该在KeyDown里处理。(KeyUp也能获得按键信息,不过是在按键上升是触发


    private void textBox1_KeyDown(object sender, KeyEventArgs e)  
    {  
       //是否安了键盘的A键  
        if (e.KeyCode == Keys.A)  
        {  
        }  
    }  

----要处理具体按下后的输出字符要用KeyPresss事件,比如验证是不是输入了数字,判断Ascii码即可。


    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
    {  
        if (e.KeyChar >= '0' && e.KeyChar <= '9')  
        {  
        }  
    }  

在KeyDown里可以判断是不是按了数字键来判断,但是要同时判断键盘上的数字和小键盘的数字,不太可取。

    private void textBox1_KeyDown(object sender, KeyEventArgs e)  
    {  
        if((e.KeyCode >= Keys.D0 && e.KeyCode <=Keys.D9) ||  
            (e.KeyCode>= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))  
        {  
        }  
    }  

3)取消用户输入应该用什么方法?

不要用户输入数字以外的字符,已经输入的字符如何取消呢?答案是可以在KeyPress里取消,也可以在KeyDown里取消。

1, KeyPress里取消输入 (A不能输入)

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
    {  
        if (e.KeyChar == 'A')  
        {  
            e.Handled = true;  
        }  
    }  

2,KeyDown里取消输入 (A不能输入)

    private void textBox1_KeyDown(object sender, KeyEventArgs e)  
    {  
        if (e.KeyCode == Keys.A)  
        {  
            e.SuppressKeyPress();  
        }  
    }  

SuppressKeyPress方法可以取消KeyPress事件,注意此时KeyUp事件也被取消了(实验得知)。

4)关于KeyDown和KeyUp

 KeyDown触发几次KeyUp就触发几次。

1,按键盘的A键,KeyDwon和KeyUp各触发一次。

KeyDown里的KeyEventArgs的值如下:(注意:KeyCode,Keydata,KeyValue里的值是伪代码,不一定和程序吻合,只是为了说明问题)

KeyCode:Keys.A

KeyData:Keys.A

KeyValue:65

KeyUp里的KeyEventArgs的值和KeyDown相同。

2,按键盘的Ctrl+A,KeyDwon和KeyUp个触发两次。

第一次KeyDown里的KeyEventArgs的值如下:

KeyCode:Keys.ContrlKey

KeyData:Keys.ContrlKey

KeyValue:17

第二次KeyDown里的KeyEventArgs的值如下:

KeyCode:Keys.A

KeyData:Keys.ContrlKey + Keys.A

KeyValue:65

第一次KeyUp里的KeyEventArgs的值如下:

KeyCode:Keys.A

KeyData:Keys.ContrlKey + Keys.A

KeyValue:65

第二次KeyUp里的KeyEventArgs的值如下:

KeyCode:Keys.ContrlKey

KeyData:Keys.ContrlKey

KeyValue:17

可以看出,KeyUp的顺序和KeyDown顺序是相反的,类似于栈,先进后出。




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值