Winform中Control.KeyDown 事件(整理自MSDN)
来源:互联网 发布:http请求json数据 编辑:程序博客网 时间:2024/06/08 08:34
事件说明:
在控件有焦点的情况下按下键时发生(这里也没说是任意键,因为有些键在KeyDown中也是处理不了的)。
原型:
public event KeyEventHandler KeyDown
相关键事件按下列顺序发生:
KeyDown
KeyPress
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 中的常数进行比较,从而获得有关用户按下的键的信息。若要确定是否已按了特定的修改键,请使用 Control、Shift 和 Alt 属性。
KeyValue 属性说明:
KeyCode 属性的整数表示形式。
- Winform中Control.KeyDown 事件(整理自MSDN)
- Winform中的Control.KeyPress事件(整理转自MSDN)
- C# winform中keyDown事件用法
- 键盘事件keydown、keypress、keyup随笔整理总结(摘抄)
- C#中Control的KeyDown,KeyPress 和KeyUp事件相关知识点
- C# WinForm窗体中有控件时keyDown事件问题处理方式
- C# WinForm窗体中有控件时keyDown事件问题处理方式
- C#,WinForm中的KeyDown,KeyPress 和KeyUp事件相关知识点
- Winform下KeyDown,KeyPress,KeyUp事件的总结
- Winform下KeyDown,KeyPress,KeyUp事件的总结
- Winform下KeyDown,KeyPress,KeyUp事件的总结
- 使用 WebRequest 类请求数据全过程(整理自msdn)
- keydown事件
- KeyDown事件中定义组合键
- KeyDown 事件中引发System.ExecutionEngineException异常
- 键盘事件keydown、keypress、keyup随笔整理总结
- 键盘事件keydown、keypress、keyup随笔整理总结
- 键盘事件keydown、keypress、keyup随笔整理总结
- MongoDB集成Hadoop进行统计计算
- hdu 4314 Save the dwarfs
- 二.redis 数据类型
- 设计模式(1):单例模式
- Oracle unable to extend table in tablespace
- Winform中Control.KeyDown 事件(整理自MSDN)
- cmd一些命令详解
- SQL 语句修改字段名称
- 学习PHP面向对象(二)构造方法与析构方法
- Java如何将两个数组的元素按顺序合并
- 原来继承可以这样用的
- SGU 397
- 三.redis 排序
- 设计模式(2):工厂方法模式