![](/img/trans.png)
[英]C#: In the keydown event of a textbox, how do you detect currently pressed modifiers + keys?
[英]How to detect multiple keys entered in c# keydown event of textbox?
我想在Silverlight中设计一个数字文本框 。
我添加了TextBox的keydown事件来处理keypress 。
内部事件中,我验证在文本框中输入的密钥。
事件如下
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
功能验证如下
private bool Validate(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) //accept enter and tab
{
return true;
}
if (e.Key == Key.Tab)
{
return true;
}
if (e.Key < Key.D0 || e.Key > Key.D9) //accept number on alphnumeric key
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) //accept number fomr NumPad
if (e.Key != Key.Back) //accept backspace
return false;
return true;
}
我无法检测到shift和Key.D0到Key.D1,即
SHIFT + 1返回“ ! ”
SHIFT + 3会像其他所有特殊键一样返回“ # ”。
我不希望用户在文本框中输入特殊字符。
我如何处理此按键事件?
在Silverlight中,我认为KeyEventArgs
类中没有任何Modifier
,而在Keyboard
:
public void KeyDown(KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.D0)
{
ControlZero();
}
}
e
应具有一个属性,该属性将告诉您是否按下了Shift,Control或Alt。 e.Modifiers
还可以为您提供有关已按下哪些附加修饰符键的其他信息。
要取消字符,可以将e.Handled
设置为True
,这将导致控件忽略按键。
textBox2.Text = string.Empty;
textBox2.Text = e.Modifiers.ToString() + " + " + e.KeyCode.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.