Learning Silverlight 2 Series (6): Keyboard event handling

Source: Internet
Author: User
Tags silverlight

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.