識別輸入裝置
識別連接到 Windows 應用程式裝置的輸入裝置,並識別其功能和屬性。
重要 API:Windows.Devices.Input、Windows.UI.Input、Windows.UI.Xaml.Input
擷取滑鼠屬性
Windows.Devices.Input 命名空間包含 MouseCapabilities 類別,用來擷取一或多個連接滑鼠所公開的屬性。 只要建立新的 MouseCapabilities 物件,並取得您感興趣的屬性。
注意這裡討論的屬性所傳回的值是以所有偵測到的滑鼠為基礎:如果至少有一個滑鼠支援特定功能,則布林屬性傳回非零,而數值屬性會傳回任何一個滑鼠所公開的最大值。
下列程式碼會使用一系列的 TextBlock 元素來顯示各個滑鼠的屬性和值。
private void GetMouseProperties()
{
MouseCapabilities mouseCapabilities = new Windows.Devices.Input.MouseCapabilities();
MousePresent.Text = mouseCapabilities.MousePresent != 0 ? "Yes" : "No";
VertWheel.Text = mouseCapabilities.VerticalWheelPresent != 0 ? "Yes" : "No";
HorzWheel.Text = mouseCapabilities.HorizontalWheelPresent != 0 ? "Yes" : "No";
SwappedButtons.Text = mouseCapabilities.SwapButtons != 0 ? "Yes" : "No";
NumButtons.Text = mouseCapabilities.NumberOfButtons.ToString();
}
擷取鍵盤屬性
Windows.Devices.Input 命名空間包含用來擷取鍵盤是否已連接的 KeyboardCapabilities 類別。 只要建立新的 KeyboardCapabilities 物件並取得 KeyboardPresent 屬性即可。
以下程式碼會使用 TextBlock 元素來顯示鍵盤屬性和值。
private void GetKeyboardProperties()
{
KeyboardCapabilities keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities();
KeyboardPresent.Text = keyboardCapabilities.KeyboardPresent != 0 ? "Yes" : "No";
}
擷取觸控屬性
Windows.Devices.Input 命名空間包含 TouchCapabilities 類別,用來擷取是否已連接任何觸控數位板。 只要建立新的 TouchCapabilities 物件,並取得您感興趣的屬性。
注意這裡討論的屬性所傳回的值是以所有偵測到的觸控數位板為基礎:如果至少有一個數位板支援特定功能,則布林屬性傳回非零,而數值屬性會傳回任何一個數位板所公開的最大值。
下列程式碼會使用一系列的 TextBlock 元素來顯示觸控的屬性和值。
private void GetTouchProperties()
{
TouchCapabilities touchCapabilities = new Windows.Devices.Input.TouchCapabilities();
TouchPresent.Text = touchCapabilities.TouchPresent != 0 ? "Yes" : "No";
Contacts.Text = touchCapabilities.Contacts.ToString();
}
擷取指標屬性
Windows.Devices.Input 命名空間包含 PointerDevice 類別,用來擷取任何偵測到的裝置是否支持指標輸入 (觸控、觸控板、滑鼠或手寫筆)。 只要建立新的 PointerDevice 物件,並取得您感興趣的屬性。
注意這裡討論的屬性所傳回的值是以所有偵測到的指標裝置為基礎:如果至少有一個裝置支援特定功能,則布林屬性傳回非零,而數值屬性會傳回任何一個指標裝置所公開的最大值。
下列程式碼會使用資料表來顯示每個指標裝置的屬性和值。
private void GetPointerDevices()
{
IReadOnlyList<PointerDevice> pointerDevices = Windows.Devices.Input.PointerDevice.GetPointerDevices();
int gridRow = 0;
int gridColumn = 0;
for (int i = 0; i < pointerDevices.Count; i++)
{
// Pointer device type.
TextBlock textBlock1 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock1);
textBlock1.Text = (i + 1).ToString() + " Pointer Device Type:";
Grid.SetRow(textBlock1, gridRow);
Grid.SetColumn(textBlock1, gridColumn);
TextBlock textBlock2 = new TextBlock();
textBlock2.Text = pointerDevices[i].PointerDeviceType.ToString();
Grid_PointerProps.Children.Add(textBlock2);
Grid.SetRow(textBlock2, gridRow++);
Grid.SetColumn(textBlock2, gridColumn + 1);
// Is external?
TextBlock textBlock3 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock3);
textBlock3.Text = (i + 1).ToString() + " Is External?";
Grid.SetRow(textBlock3, gridRow);
Grid.SetColumn(textBlock3, gridColumn);
TextBlock textBlock4 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock4);
textBlock4.Text = pointerDevices[i].IsIntegrated.ToString();
Grid.SetRow(textBlock4, gridRow++);
Grid.SetColumn(textBlock4, gridColumn + 1);
// Maximum contacts.
TextBlock textBlock5 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock5);
textBlock5.Text = (i + 1).ToString() + " Max Contacts:";
Grid.SetRow(textBlock5, gridRow);
Grid.SetColumn(textBlock5, gridColumn);
TextBlock textBlock6 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock6);
textBlock6.Text = pointerDevices[i].MaxContacts.ToString();
Grid.SetRow(textBlock6, gridRow++);
Grid.SetColumn(textBlock6, gridColumn + 1);
// Physical device rectangle.
TextBlock textBlock7 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock7);
textBlock7.Text = (i + 1).ToString() + " Physical Device Rect:";
Grid.SetRow(textBlock7, gridRow);
Grid.SetColumn(textBlock7, gridColumn);
TextBlock textBlock8 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock8);
textBlock8.Text = pointerDevices[i].PhysicalDeviceRect.X.ToString() + "," +
pointerDevices[i].PhysicalDeviceRect.Y.ToString() + "," +
pointerDevices[i].PhysicalDeviceRect.Width.ToString() + "," +
pointerDevices[i].PhysicalDeviceRect.Height.ToString();
Grid.SetRow(textBlock8, gridRow++);
Grid.SetColumn(textBlock8, gridColumn + 1);
// Screen rectangle.
TextBlock textBlock9 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock9);
textBlock9.Text = (i + 1).ToString() + " Screen Rect:";
Grid.SetRow(textBlock9, gridRow);
Grid.SetColumn(textBlock9, gridColumn);
TextBlock textBlock10 = new TextBlock();
Grid_PointerProps.Children.Add(textBlock10);
textBlock10.Text = pointerDevices[i].ScreenRect.X.ToString() + "," +
pointerDevices[i].ScreenRect.Y.ToString() + "," +
pointerDevices[i].ScreenRect.Width.ToString() + "," +
pointerDevices[i].ScreenRect.Height.ToString();
Grid.SetRow(textBlock10, gridRow++);
Grid.SetColumn(textBlock10, gridColumn + 1);
gridColumn += 2;
gridRow = 0;
}