KeyRoutedEventArgs Class

Definition

Provides event data for the KeyUp and KeyDown routed events.

Equivalent WinUI class: Microsoft.UI.Xaml.Input.KeyRoutedEventArgs.

public ref class KeyRoutedEventArgs sealed : RoutedEventArgs
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.WebHostHidden]
class KeyRoutedEventArgs sealed : RoutedEventArgs
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.WebHostHidden]
public sealed class KeyRoutedEventArgs : RoutedEventArgs
Public NotInheritable Class KeyRoutedEventArgs
Inherits RoutedEventArgs
Inheritance
KeyRoutedEventArgs
Attributes

Windows 10 requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

Here, we demonstrate how to handle the KeyUp event to navigate to a URL if the virtual key associated with the event is the Enter key. 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.

Button@"Windows.UI.Xaml.Input.KeyRoutedEventArgs.Key?text=Key" value@"Windows.UI.Xaml.Input.KeyRoutedEventArgs.OriginalKey?text=OriginalKey" value
GamepadA buttonSpaceGamepadA or NavigationAccept
GamepadB buttonEscapeGamepadB or NavigationCancel
GamepadX buttonGamepadXGamepadX
GamepadY buttonGamepadYGamepadY
GamepadDPad button

50

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

Version history

Windows version SDK version Value added
1607 14393 DeviceId

Properties

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.

Equivalent WinUI property: Microsoft.UI.Xaml.Input.KeyRoutedEventArgs.DeviceId.

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.

Equivalent WinUI property: Microsoft.UI.Xaml.Input.KeyRoutedEventArgs.Handled.

Key

Gets the virtual key associated with the event.

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

Equivalent WinUI property: Microsoft.UI.Xaml.Input.KeyRoutedEventArgs.Key.

KeyStatus

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

Equivalent WinUI property: Microsoft.UI.Xaml.Input.KeyRoutedEventArgs.KeyStatus.

OriginalKey

Gets the original, unmapped virtual key 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.

Equivalent WinUI property: Microsoft.UI.Xaml.Input.KeyRoutedEventArgs.OriginalKey.

OriginalSource

Gets a reference to the object that raised the event. This is often a template part of a control rather than an element that was declared in your app UI.

Equivalent WinUI property: Microsoft.UI.Xaml.RoutedEventArgs.OriginalSource.

(Inherited from RoutedEventArgs)

Applies to

See also