Jaa


KeyRoutedEventArgs.DeviceId Property

Definition

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 game controllers.

DeviceId is not supported for all input devices.

public:
 property Platform::String ^ DeviceId { Platform::String ^ get(); };
winrt::hstring DeviceId();
public string DeviceId { get; }
var string = keyRoutedEventArgs.deviceId;
Public ReadOnly Property DeviceId As String

Property Value

String

Platform::String

winrt::hstring

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.

Windows requirements

Device family
Windows 10 Anniversary Edition (introduced in 10.0.14393.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v3.0)

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);
}

Applies to

See also