[英]How to detect multiple keys entered in c# keydown event of textbox?
I want to design a numeric textbox in silverlight. 我想在Silverlight中设计一个数字文本框 。
I have added keydown event of TextBox to handle the keypress . 我添加了TextBox的keydown事件来处理keypress 。
Inside event I validate the key entered in the textbox. 内部事件中,我验证在文本框中输入的密钥。
event as follows 事件如下
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
function Validate as follows 功能验证如下
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;
}
I am not able to detect shift and Key.D0 to Key.D1 ie 我无法检测到shift和Key.D0到Key.D1,即
SHIFT + 1 which returns " ! " SHIFT + 1返回“ ! ”
SHIFT + 3 returns " # " like wise any other special keys. SHIFT + 3会像其他所有特殊键一样返回“ # ”。
I dont want user to enter special character in to textbox. 我不希望用户在文本框中输入特殊字符。
How do i handle this keys event?? 我如何处理此按键事件?
In Silverlight, I don't think there are any Modifier
s in the KeyEventArgs
class but instead in Keyboard
: 在Silverlight中,我认为
KeyEventArgs
类中没有任何Modifier
,而在Keyboard
:
public void KeyDown(KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.D0)
{
ControlZero();
}
}
e
should have a property on it that will tell you if Shift, Control or Alt are pressed. e
应具有一个属性,该属性将告诉您是否按下了Shift,Control或Alt。 e.Modifiers
can also give you additional information about which additional modifier keys have been pressed. e.Modifiers
还可以为您提供有关已按下哪些附加修饰符键的其他信息。
To cancel the character you can set e.Handled
to True
, which will cause the control to ignore the keypress. 要取消字符,可以将
e.Handled
设置为True
,这将导致控件忽略按键。
textBox2.Text = string.Empty;
textBox2.Text = e.Modifiers.ToString() + " + " + e.KeyCode.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.