Partager via


KeyDown

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs when a keyboard key is pressed while the Silverlight plug-in or a specific control has focus.

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

Arguments

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, quotation marks around the function name are not required. (See the "Remarks" section.)

Event Handler Parameters

sender

object

The object that invoked the event.

keyEventArgs

object

KeyboardEventArgs

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.

keyEventArgs.Handled: A Boolean value that determines whether event routing should continue. (Silverlight 2)

keyEventArgs.Source: Reports the object that raised the event. (Silverlight 2)

Managed Equivalent

KeyDown

Remarks

NoteNote:

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 by using a quoted string for the event handler name, as follows:

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.

For a keyboard event to be received, the Silverlight plug-in or a specific control within it needs to have focus. Otherwise, the events are not generated. A Silverlight plug-in can gain focus through user actions, such as clicking on or tabbing to the plug-in area. Alternatively, you can force focus to the Silverlight plug-in by calling the focus() method in the browser Document Object Model (DOM) when you handle the OnLoad event from instantiation script.

For more information on basic concepts, see Events Overview for Silverlight or Keyboard Support. Note that these topics are written primarily for users of the managed API, and may not have code examples or specific information that address the JavaScript API scenarios.

KeyDown is a bubbling event, so you can handle KeyDown on the control that received focus, or on any parent object (the parent object itself does not have to be focusable to receive the routed event).

Example

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

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  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.

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

Border (Silverlight 2)

Canvas)

Grid (Silverlight 2)

PasswordBox (Silverlight 2)

Popup (Silverlight 2)

StackPanel (Silverlight 2)

TextBox (Silverlight 2)

See Also

Reference