This article, sixth of the series, describes the keyboard handling events in Silverlight, and supports KeyDown and KeyUp two events in Silverlight 2.
declaring events
All event declaration procedures are the same, registered in XAML or in code.
<Canvas x:Name="LayoutRoot" Background="#46461F">
<Ellipse x:Name="ellipse" Width="120" Height="120" Fill="Orange"
Canvas.Top="50" Canvas.Left="160"
Stroke="White" StrokeThickness="2"
KeyUp="ellipse_KeyUp"
KeyDown="ellipse_KeyDown"/>
</Canvas>
or register in code:
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
this.ellipse.KeyUp += new KeyEventHandler(ellipse_KeyUp);
this.ellipse.KeyDown += new KeyEventHandler(ellipse_KeyDown);
}
private void ellipse_KeyUp(object sender, KeyEventArgs e)
{
}
private void ellipse_KeyDown(object sender, KeyEventArgs e)
{
}
}
Using event arguments KeyEventArgs
You can use event parameters to get event data, and the properties you can use are key, Platformkeycode, Handled, Source.
private void ellipse_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.R)
{
//......
}
else if(e.Key == Key.Ctrl && e.Key == Key.U)
{
//......
}
}
In the event data, handled is sometimes useful to determine if an event has been processed.