08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

400<br />

CHAPTER 16 ■ PROGRAMMING SILVERLIGHT WITH XAML AND JAVASCRIPT<br />

The keyUp event will fire when the key has been released. It exposes the same parameters<br />

as the keyDown event, <strong>and</strong> can be h<strong>and</strong>led in the same manner. When processing<br />

input, it is often better to capture it on keyUp instead of keyDown—that way, you can more<br />

accurately assess the state of the Shift <strong>and</strong> Ctrl keys. For example, when the user types<br />

Shift+S for an uppercase S, you’ll get two keyDown events: one for the initial Shift, <strong>and</strong> then<br />

one for the S. However, for keyUp, you’ll only get an event for Shift+S.<br />

H<strong>and</strong>ling the Loading of Controls with the Loaded Event<br />

Every XAML element exposes a Loaded event, which can be captured to do some processing<br />

once the element is loaded. In most examples, you’ll see a Loaded event being captured on<br />

the control itself, but if you want finer-grained processing, you can do it for each element.<br />

This is very useful when you want to add event listeners at runtime. If you have a<br />

lot of controls that you want to set up event listeners for, <strong>and</strong> you do them all in the control’s<br />

Load event, you’ll end up with a lot of code in there that is difficult to maintain.<br />

Note that if you specify the Load event for an element <strong>and</strong> that element’s child, the<br />

child event will fire first, followed by the parent.<br />

H<strong>and</strong>ling Mouse Events<br />

Controls can be interacted with using the mouse, <strong>and</strong> several events are provided that<br />

allow you to control their behavior in different circumstances. Each of these events provide<br />

a reference to the control as their first argument, <strong>and</strong> a MouseEventArgs object as their<br />

second argument.<br />

The MouseEventArgs object exposes a getPosition method that returns an object containing<br />

x <strong>and</strong> y properties that are used to get the coordinates of the mouse on the screen.<br />

In addition, it exposes a shift property that is used to determine whether the Shift key<br />

was pressed, <strong>and</strong> a ctrl property that is used to determine if the Ctrl key was pressed.<br />

The events that can be captured are as follows:<br />

MouseEnter: This fires when the mouse enters the bounds of this control.<br />

MouseLeave: This fires when the mouse leaves the bounds of this control.<br />

MouseLeftButtonDown: This fires when the left mouse button is held down over the<br />

control.<br />

MouseLeftButtonUp: This fires when the left mouse button is released over the control<br />

after being held down over the control.<br />

MouseMove: This fires when the mouse is moved within the bounds of the control.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!