Winform中的Control.KeyPress事件(整理转自MSDN)
来源:互联网 发布:windows系统语言对照表 编辑:程序博客网 时间:2024/05/22 00:47
KeyPress事件会在控件有交点的情况下按下键时触发(注意不是任意键哦,因为某些键KeyPress是不能捕获的,具体请往下看)。
原型:public event KeyPressEventHandler KeyPress
MSDN说明:键事件按以下顺序发生:
KeyDown
KeyPress
KeyUp
KeyPress 事件不能由非字符键引发;但是非字符键能够引发 KeyDown 和 KeyUp 事件。
使用 KeyChar 属性采样运行时的键击,以及使用或修改常用键击的一个子集。
若要仅在窗体级别处理键盘事件而不允许其他控件接收键盘事件,请将窗体的 KeyPress 事件处理方法中的 KeyPressEventArgs.Handled 属性设置为 true。
注:对于这个【非字符键,没有在MSDN上找到具体的说明,这里暂且阁下】
下面的代码示例使用 KeyPress事件来禁止向控件输入字符。
// 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; } } } //If shift key was pressed, it's not a number. if (Control.ModifierKeys == Keys.Shift) { 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; } }
转自MSDN:点击打开链接
事件参数说明(主要是KeyPressEventArgs 参数):为 KeyPress 事件提供数据。
属性
KeyPressEventArgs.Handled 属性:
public bool Handled { get; set; }
如果处理过事件则为True(表示此事件已处理《可以表示有用户自行处理过,Handled 设置为 true,以防止操作系统进一步处理该键》),
否则为false(表示此事件未经过处理,则会交由操作系统进行处理);
可以将 Handled 设置为 true,以取消操作系统对 KeyPress 事件。
KeyPressEventArgs.KeyChar 属性
获取或设置与按下的键对应的字符。
public char KeyChar { get; set; }
(可以转换为int对应ASCII码)
备注
使用 KeyChar 属性在运行时对击键取样,在特定运行时环境下修改击键。
例如,您可以使用 KeyChar 在用户输入邮政编码时禁用非数字按键,将数据输入字段中所有的字母按键都改为大写,或监视键盘或其他按键输入设备的特定组合键。
您可以获取或设置以下键:
a-z,A-Z。
Ctrl。
标点符号。
键盘顶部和数字键盘上的数字键。
Enter。
您不能获取或设置以下键:
Tab 键。
Insert 和 Delete。
Home。
End。
Page Up 和 Page Down。
F1-F2。
Alt。
箭头键。
猜一猜:这些可以获取到的键是不是就是上面说的【非字符键】呢?????
KeyPressEventArgs参数备注:
KeyPressEventArgs 指定在用户按键时撰写的字符。 例如,当用户按 Shift + K 时,KeyChar 属性返回一个大写字母 K。
当用户按下任意键时,发生 KeyPress 事件。 与 KeyPress 事件紧密相关的两个事件为 KeyUp 和 KeyDown。 当用户按下某个键时,KeyDown 事件先于每个 KeyPress 事件发生;当用户释放某个键时发生 KeyUp 事件。 当用户按住某个键时,每次字符重复时,KeyDown 和 KeyPress 事件也都重复发生。 一个 KeyUp 事件在释放按键时生成。
KeyPressEventArgs 随着 KeyPress 事件的每次发生而被传递。 KeyEventArgs 随着 KeyDown 和 KeyUp 事件的每次发生而被传递。 KeyEventArgs 指定是否有任一个组合键(Ctrl、Shift 或 Alt)在另一个键按下的同时也曾按下。 (此修饰符信息也可以通过 Control 类的 ModifierKeys 属性获得。)
将 Handled 设置为 true,以取消 KeyPress 事件。 这可防止控件处理按键。
MSDN连接:
点击打开链接
追加一段自己的代码:
private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar == 24) { e.Handled = false; return; } if ((int)e.KeyChar == 12290) { e.KeyChar = (char)46;//标示如果是“。”则转换为“.” } if (((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) || (int)e.KeyChar == 8 || (int)e.KeyChar == 46) { e.Handled = false;//标示如果是数字或者删除键和“.”,则交由操作系统处理 } else { e.Handled = true; } }
- Winform中的Control.KeyPress事件(整理转自MSDN)
- Winform中Control.KeyDown 事件(整理自MSDN)
- C#,WinForm中的KeyDown,KeyPress 和KeyUp事件相关知识点
- 键盘事件keydown、keypress、keyup随笔整理总结(摘抄)
- 转: C#控件中的KeyDown、KeyPress 与 KeyUp事件
- AOP(转自MSDN)
- [转]整理MSDN中的SqlConnection.ConnectionString 属性
- Winform下KeyDown,KeyPress,KeyUp事件的总结
- Winform下KeyDown,KeyPress,KeyUp事件的总结
- Winform下KeyDown,KeyPress,KeyUp事件的总结
- Msdn中的事件路由
- 使用 WebRequest 类请求数据全过程(整理自msdn)
- 键盘事件中的keypress事件和keyup事件的区别
- 键盘事件keydown、keypress、keyup随笔整理总结
- 键盘事件keydown、keypress、keyup随笔整理总结
- 键盘事件keydown、keypress、keyup随笔整理总结
- keyPress事件中的keyCode和action 两个参数(附测试按键的一个小demo)
- 【c#】键盘事件(keypress keydown keyup)
- “微信”带来的新启示
- easyUI Panel
- 深入理解javascript闭包(二)
- python 等待一定时间后继续执行其后的程序
- Oracle Undo & Redo
- Winform中的Control.KeyPress事件(整理转自MSDN)
- vc++快速使用richedit控件
- linux下mysql 命令
- Unity 移动开发自适应屏幕
- hdu 4300 Clairewd’s message(字符串,可以用kmp)
- IOS与Mac OS X
- DB2 INTERSECT、EXCEPT、UNION集合操作测试
- DECLARE_DYNCREATE和IMPLEMENT_DYNCREATE宏分析
- 自动建表