SilverLight中键盘的基本操作
1.键盘的路由事件
//键盘事件KeyDown和KeyUp都支持路由事件
//注意:当焦点在按钮上时,按Enter键好像不会路由事件,什么原因呢???
前台代码:
<Canvas x:Name="LayoutRoot" Background="#46461F" KeyUp="LayoutRoot_KeyUp">
<TextBox x:Name="textbox" Width="200" Height="40"
Canvas.Top="80" Canvas.Left="80"/>
<Button x:Name="button" Width="100" Height="40"
Canvas.Top="80" Canvas.Left="280"
Background="Red" Margin="20 0 0 0" Content="Submit"/>
<TextBlock x:Name="Status" Foreground="White" Text="Status"
Canvas.Left="80" Canvas.Top="200"/>
</Canvas>
后台代码:
protected void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Unknown)
{
String msg = "The key " + e.Key.ToString();
msg += " was pressed while focus was on " + (e.OriginalSource as FrameworkElement).Name;
Status.Text = msg;
}
}