KeyEventArgs KeyEventArgs KeyEventArgs KeyEventArgs Class

Definition

Contains the arguments returned by a virtual key event.

public : sealed class KeyEventArgs : ICoreWindowEventArgs, IKeyEventArgs, IKeyEventArgs2
struct winrt::Windows::UI::Core::KeyEventArgs : ICoreWindowEventArgs, IKeyEventArgs, IKeyEventArgs2
public sealed class KeyEventArgs : ICoreWindowEventArgs, IKeyEventArgs, IKeyEventArgs2
Public NotInheritable Class KeyEventArgs Implements ICoreWindowEventArgs, IKeyEventArgs, IKeyEventArgs2
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Windows 10 Apps do not receive this event when an Input Method Editor (IME) is enabled. The Input Method Editor (IME) handles all keyboard input and sets Handled to true.

Windows Phone This API is supported in native apps only.

This object is returned by a delegate registered for one of the following events:

void SetWindow(CoreWindow const & window)
{
    ...
    window.KeyDown({ this, &App::OnKeyDown });
    window.KeyUp({ this, &App::OnKeyUp });
}

// Keypress data is in KeyEventArgs.
void OnKeyDown(Windows::UI::Core::CoreWindow const& /* sender */,
    Windows::UI::Core::KeyEventArgs const& /* args */) {}

void OnKeyUp(Windows::UI::Core::CoreWindow const& /* sender */,
    Windows::UI::Core::KeyEventArgs const& /* args */) {}
// returning keypress events data through KeyEventArgs
void MyCoreWindowEvents::SetWindow( // implementation called by CoreApplication::Run(), provided for context
    _In_ CoreWindow^ window
    )
{
    // ...
    window->KeyDown +=
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &CoreWindowEvents::OnKeyDown);
    window->KeyUp +=
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &CoreWindowEvents::OnKeyUp);

    // ...
}

Note

: This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX).

Properties

DeviceId DeviceId DeviceId DeviceId

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

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

DeviceId is not supported for all input devices.

Handled Handled Handled Handled

Gets or sets whether the key press event was handled.

KeyStatus KeyStatus KeyStatus KeyStatus

Gets the status of a key at the time the event is fired.

VirtualKey VirtualKey VirtualKey VirtualKey

Gets the virtual key that maps to the key that was pressed.

See Also