主题:键盘事件,在silverlight如何实现
类似flash,存在鼠标事件,当然也会存在键盘事件。
在silverlight中,类似于鼠标的down和up事件,键盘也存在这两种事件,keyup和keydown。前台页面如下:
前台添加完事件,可以跳转到后台去看到相应的事件,这里写在松开按键的时候改变文本框的文字。
键盘路由事件
键盘事件KeyDown和KeyUp都支持路由事件,如下面的示例。
<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>
<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>
为Canvas注册了一个KeyUp事件,编写事件处理程序。
private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Unknown)
{
String msg = "The key " + e.Key.ToString();
msg += " was pressed while focus was on " + (e.Source as FrameworkElement).Name;
statusTextBlock.Text = msg;
}
}
private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Unknown)
{
String msg = "The key " + e.Key.ToString();
msg += " was pressed while focus was on " + (e.Source as FrameworkElement).Name;
statusTextBlock.Text = msg;
}
注意:在silverlight中,能获取焦点的控件,才可以有相应的键盘事件,意思是说,需要有能显示光标的地方,才可以出发相应的键盘事件,键盘事件也是可以上溯到最顶级的容器的。更多也可参考中睿it服务
www.itlead.com.cn/article/html/150/2012-04-09/content-8293.shtml
在silverlight中,类似于鼠标的down和up事件,键盘也存在这两种事件,keyup和keydown。前台页面如下:
前台添加完事件,可以跳转到后台去看到相应的事件,这里写在松开按键的时候改变文本框的文字。
键盘路由事件
键盘事件KeyDown和KeyUp都支持路由事件,如下面的示例。
<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>
<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>
为Canvas注册了一个KeyUp事件,编写事件处理程序。
private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Unknown)
{
String msg = "The key " + e.Key.ToString();
msg += " was pressed while focus was on " + (e.Source as FrameworkElement).Name;
statusTextBlock.Text = msg;
}
}
private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Unknown)
{
String msg = "The key " + e.Key.ToString();
msg += " was pressed while focus was on " + (e.Source as FrameworkElement).Name;
statusTextBlock.Text = msg;
}
注意:在silverlight中,能获取焦点的控件,才可以有相应的键盘事件,意思是说,需要有能显示光标的地方,才可以出发相应的键盘事件,键盘事件也是可以上溯到最顶级的容器的。更多也可参考中睿it服务
www.itlead.com.cn/article/html/150/2012-04-09/content-8293.shtml