KeyPress事件会在控件有交点的情况下按下键时触发(注意不是任意键哦,因为某些键KeyPress是不能捕获的,具体请往下看)。
原型:public event KeyPressEventHandler KeyPress
MSDN说明:键事件按以下顺序发生:
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;
}
}