Introduction : This blog introduces how to handle the keys in Silverlight.
If we want to which key is pressed,we can identify with the help of keys, ModifierKeys in silverlight.
Explantion :
1.Handle Shift case :On Textbox KeyDown Event.
Here we don't allow the user to enter the Special characters in textbox.
private void usertext_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Shift)
{
e.Handled = true;
MessageBox.Show("Please Enter Numbers Only");
}
}
Handle other cases :
if (!e.Handled && (e.Key < Key.D0 || e.Key > Key.D9))
{
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
{
if (e.Key != Key.Back)
{
e.Handled = true;
}
}
}
Suppose we want to handle tab key on textbox keydown event.From that we can select full text in textbox by pressing tab.
private void usertext_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
passbox.SelectAll();
}
}
2. ModifierKeys Enumeration
Specifies the set of modifier keys.
1. None: No modifiers are pressed.
2. Alt: The ALT key is pressed.
3. Control: The CTRL key is pressed.
4. Shift: The SHIFT key is pressed.
5. Windows: The Windows logo key is pressed.
6. Apple: The Apple key (also known as the Open Apple key) is pressed.