KeyRoutedEventArgs KeyRoutedEventArgs KeyRoutedEventArgs KeyRoutedEventArgs Class

Definition

Provides event data for the KeyUp and KeyDown routed events.

public : sealed class KeyRoutedEventArgs : RoutedEventArgs, IKeyRoutedEventArgs, IKeyRoutedEventArgs2, IKeyRoutedEventArgs3public sealed class KeyRoutedEventArgs : RoutedEventArgs, IKeyRoutedEventArgs, IKeyRoutedEventArgs2, IKeyRoutedEventArgs3Public NotInheritable Class KeyRoutedEventArgs Inherits RoutedEventArgs Implements IKeyRoutedEventArgs, IKeyRoutedEventArgs2, IKeyRoutedEventArgs3// This API is not available in Javascript.
Inheritance
KeyRoutedEventArgsKeyRoutedEventArgsKeyRoutedEventArgsKeyRoutedEventArgs
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Inherited Members

Inherited properties

Examples

The following example code demonstrates the use of this type. For the complete code listing, see the XAML WebView control sample.

void SDKSample::WebViewControl::Scenario1::NavigateButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    ProgressRing1->IsActive = true;

    // Provide an indication as to where we are trying to navigate to
    String^ str = "Navigating to: {0}" + Address->Text;
    rootPage->NotifyUser(str, NotifyType::StatusMessage);

    // Hook the LoadCompleted event for the WebView to know when the URL is fully loaded
    WebView1->LoadCompleted += ref new LoadCompletedEventHandler(this, &Scenario1::WebView1_LoadCompleted);

    // Attempt to navigate to the specified URL.  Notice that a malformed URL will raise a FormatException
    // which we catch and let the user know that the URL is bad and to enter a new well-formed one.
    try
    {
        Uri^ targetUri = ref new Uri(Address->Text);
        WebView1->Navigate(targetUri);
    }
    catch (FailureException^ myE)
    {
        // Bad address
        String^ str = "Address is invalid, try again.  Details --> {0}" + myE->Message;
        rootPage->NotifyUser(str, NotifyType::ErrorMessage);
    }    
}

void SDKSample::WebViewControl::Scenario1::WebView1_LoadCompleted(Platform::Object^ sender, Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
    WebView1->Visibility = Windows::UI::Xaml::Visibility::Visible;
    BlockingRect->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    ProgressRing1->IsActive = false;
    
    // Tell the user that the page has loaded
    rootPage->NotifyUser("Page loaded", NotifyType::StatusMessage);
}


void SDKSample::WebViewControl::Scenario1::Address_KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
     if (e->Key == Windows::System::VirtualKey::Enter)
    {
        NavigateButton_Click(this, ref new RoutedEventArgs());
    }
}
private void NavigateButton_Click(object sender, RoutedEventArgs e)
{
    ProgressRing1.IsActive = true;

    // Provide an indication as to where we are trying to navigate to
    rootPage.NotifyUser(String.Format("Navigating to: {0}", Address.Text), NotifyType.StatusMessage);

    // Hook the LoadCompleted event for the WebView to know when the URL is fully loaded
    WebView1.LoadCompleted += new Windows.UI.Xaml.Navigation.LoadCompletedEventHandler(WebView1_LoadCompleted);

    // Attempt to navigate to the specified URL.  Notice that a malformed URL will raise a FormatException
    // which we catch and let the user know that the URL is bad and to enter a new well-formed one.
    try
    {
        Uri targetUri = new Uri(Address.Text);
        WebView1.Navigate(targetUri);
    }
    catch (FormatException myE)
    {
        // Bad address
        rootPage.NotifyUser(String.Format("Address is invalid, try again.  Details --> {0}", myE.Message), NotifyType.ErrorMessage);
    }
}

void WebView1_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
    WebView1.Visibility = Windows.UI.Xaml.Visibility.Visible;
    BlockingRect.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    ProgressRing1.IsActive = false;

    // Tell the user that the page has loaded
    rootPage.NotifyUser("Page loaded", NotifyType.StatusMessage);
}

void Address_KeyUp(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
    {
        NavigateButton_Click(this, new RoutedEventArgs());
    }
}
Private Sub NavigateButton_Click(sender As Object, e As RoutedEventArgs)
    ProgressRing1.IsActive = True

    ' Provide an indication as to where we are trying to navigate to
    rootPage.NotifyUser(String.Format("Navigating to: {0}", Address.Text), NotifyType.StatusMessage)

    ' Hook the LoadCompleted event for the WebView to know when the URL is fully loaded
    AddHandler WebView1.LoadCompleted, AddressOf WebView1_LoadCompleted

    ' Attempt to navigate to the specified URL.  Notice that a malformed URL will raise a FormatException
    ' which we catch and let the user know that the URL is bad and to enter a new well-formed one.
    Try
        Dim targetUri As New Uri(Address.Text)
        WebView1.Navigate(targetUri)
    Catch myE As FormatException
        ' Bad address
        rootPage.NotifyUser(String.Format("Address is invalid, try again.  Details --> {0}", myE.Message), NotifyType.ErrorMessage)
    End Try
End Sub

Private Sub WebView1_LoadCompleted(sender As Object, e As Windows.UI.Xaml.Navigation.NavigationEventArgs)
    WebView1.Visibility = Windows.UI.Xaml.Visibility.Visible
    BlockingRect.Visibility = Windows.UI.Xaml.Visibility.Collapsed
    ProgressRing1.IsActive = False

    ' Tell the user that the page has loaded
    rootPage.NotifyUser("Page loaded", NotifyType.StatusMessage)
End Sub

Private Sub Address_KeyUp(sender As Object, e As Object)
    'If e.Key = Windows.System.VirtualKey.Enter Then
    '    NavigateButton_Click(Me, New RoutedEventArgs())
    'End If
End Sub

Remarks

Button input is mapped to differerent key values for various devices, including gamepad and the Xbox One infrared remote.

This table provides the mapping detail for these devices.

ButtonKey valueOriginalKey value
GamepadA buttonSpaceGamepadA or NavigationAccept
GamepadB buttonEscapeGamepadB or NavigationCancel
GamepadX buttonGamepadXGamepadX
GamepadY buttonGamepadYGamepadY
GamepadDPad button 50GamepadDPadUp/Down/Left/RightGamepadDPadUp/Down/Left/Right or NavigationUp/Down/Left/Right
GamePad Joystick/ThumbstickGamepadLeftThumbstickButton/Up/Down/Left/Right or GamepadRightThumbstickButton/Up/Down/Left/RightGamepadLeftThumbstickButton/Up/Down/Left/Right or GamepadRightThumbstickButton/Up/Down/Left/Right or NavigationUp/Down/Left/Right
Gamepad TriggerGamePadRightTrigger/LeftTriggerGamePadRightTrigger/LeftTrigger
Gamepad Bumper/ShoulderGamePadRightShoulder/LeftShoulderGamePadRightShoulder/LeftShoulder
Gamepad Menu buttonGamepadMenuGamepadMenu or NavigationMenu
Gamepad View buttonGamepadViewGamepadView or NavigationView

Properties

DeviceId DeviceId DeviceId DeviceId

Gets a unique ID for the input device that generated this key event.

Use DeviceId to differentiate between all connected devices that can generate key events, such as multiple Xbox controllers.

DeviceId is not supported for all input devices.

public : PlatForm::String DeviceId { get; }public string DeviceId { get; }Public ReadOnly Property DeviceId As string// This API is not available in Javascript.
Value
PlatForm::String string string string

A unique identifier for the input device associated with the key event, or an empty string for an unsupported device. The same device can be assigned a different ID each time it is connected.

Additional features and requirements
Device family
Windows 10 Anniversary Edition (introduced v10.0.14393.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v3)

Remarks

Some devices, such as Xbox, permit multiple users to sign in for a single interactive session. This DeviceId property is useful for retrieving info for a specific user account associated with the input device.

In Windows 10 and later, Universal Windows Platform (UWP) app do not have access to user information without explicit user consent (unlike Windows 8, where permission is granted by default).

Universal Windows Platform (UWP) app that access user information must declare the userAccountInformation capability (Windows.System.UserDeviceAssociation.FindUserFromDeviceId, Windows.System.User.FindAllAsync and User.GetPropertiesAsync can be used to get the data).

When this capability is declared, users installing the app are prompted to allow access to their information. If the user permits the app to access the information, the app appears listed in the Privacy page of the Windows 10 Settings app (Settings > Privacy > Account info).

private async void OnKeyDown(object sender, KeyRoutedEventArgs e)
{
  User user = 
    Windows.System.UserDeviceAssociation.FindUserFromDeviceId(e.DeviceId);
  string displayName = 
    (string)await user.GetPropertyAsync(KnownUserProperties.DisplayName);
  System.Diagnostics.Debug.WriteLine(displayName);
}

Sometimes, an OnKeyDown event might not fire because the event was already handled by a control. In this case, call the GetCurrentKeyEventDeviceId method from the KeyDown handler of CoreWindow, as shown here.

public MainPage()
{
  this.InitializeComponent();
  Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown += OnKeyDown;
}

private async void OnKeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
  string device = Windows.UI.Core.CoreWindow.GetForCurrentThread().GetCurrentKeyEventDeviceId();
  User user = Windows.System.UserDeviceAssociation.FindUserFromDeviceId(device);
  string displayName = (string)await user.GetPropertyAsync(KnownUserProperties.DisplayName);
  System.Diagnostics.Debug.WriteLine("OnKeydown:" + displayName);
}
See Also

Handled Handled Handled Handled

Gets or sets a value that marks the routed event as handled. A true value for Handled prevents most handlers along the event route from handling the same event again.

public : PlatForm::Boolean Handled { get; set; }public bool Handled { get; set; }Public ReadWrite Property Handled As bool// This API is not available in Javascript.
Value
PlatForm::Boolean bool bool bool

true to mark the routed event handled; false to leave the routed event unhandled, which permits the event to potentially route further. The default is false.

See Also

Key Key Key Key

Gets the input button associated with the event.

Depending on the input device, this can be a mapped value. For raw, unmapped event data, see the OriginalKey property.

public : VirtualKey Key { get; }public VirtualKey Key { get; }Public ReadOnly Property Key As VirtualKey// This API is not available in Javascript.
Value
VirtualKey VirtualKey VirtualKey VirtualKey

A system value that indicates the code for the key referenced by the event.

KeyStatus KeyStatus KeyStatus KeyStatus

Gets a structure value that reports various system-detected characteristics of the key press, including repeat count and menu status.

public : CorePhysicalKeyStatus KeyStatus { get; }public CorePhysicalKeyStatus KeyStatus { get; }Public ReadOnly Property KeyStatus As CorePhysicalKeyStatus// This API is not available in Javascript.
Value
CorePhysicalKeyStatus CorePhysicalKeyStatus CorePhysicalKeyStatus CorePhysicalKeyStatus

A structure value with flags that report status.

OriginalKey OriginalKey OriginalKey OriginalKey

Gets the original, unmapped input button associated with the event.

Depending on the input device, the input button might be mapped to a different key value. For the mapped event value, see the Key property.

public : VirtualKey OriginalKey { get; }public VirtualKey OriginalKey { get; }Public ReadOnly Property OriginalKey As VirtualKey// This API is not available in Javascript.
Value
VirtualKey VirtualKey VirtualKey VirtualKey

A system value that indicates the code for the key referenced by the event.

See Also