Udostępnij za pośrednictwem


KeyEventArgs Class

Definition

Contains the arguments returned by a virtual key event.

public ref class KeyEventArgs sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
class KeyEventArgs final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
public sealed class KeyEventArgs
Public NotInheritable Class KeyEventArgs
Inheritance
Object Platform::Object IInspectable KeyEventArgs
Attributes
Implements

Windows requirements

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

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).

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 devices that can generate key events, such as multiple game controllers.

DeviceId is not supported for all input devices.

Handled

Gets or sets whether the key press event was handled.

KeyStatus

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

VirtualKey

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

Applies to

See also