Share via


KeyDown Event

Occurs when the key is pressed while the plug-in has focus.

XAML
object KeyDown="eventhandlerFunction" .../>
Scripting
[token = ]object.AddEventListener("KeyDown", eventhandlerFunction)

AddEventListener Parameters

token

integer

A token that is returned from the function, which you can optionally retain as a variable. If you intend to call RemoveEventListener to remove the handler, you will need this token.

eventhandlerFunction

object

The name of your event handler function as it is defined in script. When used as an AddEventListener parameter, quotes around the function name are not required. See Remarks.

Event Handler Parameters

sender

object

Identifies the object that invoked the event.

keyEventArgs

object

keyEventArgs.Key - an integer value that represents the key that is down. This value is the portable Key code, which is not operating system-specific.

keyEventArgs.PlatformKeyCode - an integer value that represents the key that is down. This value is the non-portable key code, which is operating system-specific.

keyEventArgs.Shift - a Boolean value that determines whether the SHIFT key is down.

keyEventArgs.Ctrl - a Boolean value that determines whether the CTRL key is down.

Remarks

The KeyDown event can only be defined for the root Canvas object of a Silverlight plug-in.

Note   For information on portability issues and which keystrokes the Web browser passes on the Silverlight content, see Silverlight Keyboard Support.

Note   Handling keyboard events might vary between browsers. When you create an application that uses keyboard input, make sure to test the application in your target browsers.

You can also add handlers in script using a quoted string for the event handler name:

object.AddEventListener("KeyDown", "eventhandlerFunction")

This syntax also returns a token; however, the token is not an absolute requirement for removing the handler, in cases where the handler was added by using a quoted string. For details, see RemoveEventListener.

In order for a keyboard event to be received, the Silverlight plug-in needs to have focus; otherwise, the events are not generated. A Silverlight plug-in can gain focus by user actions, such as clicking on the plug-in, or by tabbing to it.

Examples

The following XAML example shows a KeyDown event defined for the root Canvas object:

XAML
<Canvas
  xmlns="https://schemas.microsoft.com/client/2007"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  KeyDown="onKeyDown">
  <TextBlock 
    x:Name="myTextBlock" 
    Text="Display KeyDown event arguments" />
</Canvas>

The following JavaScript example shows how to implement a KeyDown event handler function. In this case, the values of the keyEventArgs parameter are displayed for each KeyDown event:

JavaScript
// Set the TextBlock to display the key values.
function onKeyDown(sender, keyEventArgs) 
{        
    var textBlock = sender.findName("myTextBlock");
    // Concatenate the values of the KeyEventArgs parameter.
    var msg  = "key: " + keyEventArgs.key;
        msg += " platformKeycode: " + keyEventArgs.platformKeyCode;
        msg += " shift: " + keyEventArgs.shift;
        msg += " ctrl: " + keyEventArgs.ctrl;
    textBlock.text = msg;
}

Applies To

Canvas

See Also

Silverlight Keyboard Support
Silverlight Events
Key
KeyUp