KeyRoutedEventArgs.DeviceId 屬性

定義

取得產生此金鑰事件之輸入裝置的唯一識別碼。

使用 DeviceId 區分可產生重要事件的所有連線裝置,例如多個遊戲控制器。

所有輸入裝置都不支援 DeviceId。

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

屬性值

String

Platform::String

winrt::hstring

與金鑰事件相關聯的輸入裝置的唯一識別碼,或不支援裝置的空字串。 每次連線時,相同的裝置都可以指派不同的識別碼。

Windows 需求

裝置系列
Windows 10 Anniversary Edition (已於 10.0.14393.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v3.0 引進)

備註

某些裝置,例如 Xbox,允許多個使用者登入單一互動式會話。 此 DeviceId 屬性適用于擷取與輸入裝置相關聯之特定使用者帳戶的資訊。

在Windows 10和更新版本中,通用 Windows 平臺 (UWP) app 在與預設) 授與許可權Windows 8不同的情況下,沒有明確使用者同意 (存取使用者資訊。

通用 Windows 平臺 (存取使用者資訊的 UWP) 應用程式必須宣告userAccountInformation 功能, (Windows.System.UserDeviceAssociation.FindUserFromDeviceIdWindows.System.User.FindAllAsyncUser.GetPropertiesAsync來取得資料) 。

宣告此功能時,系統會提示安裝應用程式的使用者允許存取其資訊。 如果使用者允許應用程式存取訊號,應用程式會出現在 [Windows 10設定] 應用程式的 [隱私權] 頁面中, (> 設定隱私權 > 帳戶資訊) 。

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

有時候, OnKeyDown 事件可能不會引發,因為事件已經由控制項處理。 在此情況下,請從CoreWindowKeyDown處理常式呼叫GetCurrentKeyEventDeviceId方法,如下所示。

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

適用於

另請參閱