Share via

KeyDown Event

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

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

AddEventListener Parameters



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.



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



Identifies the object that invoked the event.



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.


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.


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

    Text="Display KeyDown event arguments" />

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:

// 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


See Also

Silverlight Keyboard Support
Silverlight Events