重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop...
[源碼下載]
作者:webabcd
介紹
重新想象 Windows 8 Store Apps 之?輸入
- 輸入設備的相關信息
- SIP(Soft Input Panel)的應用
- Tab 鍵導航
- Pointer - 指針,鼠標
- Tap - 觸摸
- Drag 和 Drop
示例
1、演示如何獲取輸入設備的相關信息
Input/InputDeviceInfo.xaml
Input/InputDeviceInfo.xaml.cs
/** 演示如何獲取輸入設備的相關信息*/using System; using Windows.Devices.Input; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation;namespace XamlDemo.Input {public sealed partial class InputDeviceInfo : Page{public InputDeviceInfo(){this.InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){// 獲取鼠標設備的相關信息MouseCapabilities mouseCapabilities = new MouseCapabilities();lblMsg.Text = "MouseCapabilities.MousePresent: " + mouseCapabilities.MousePresent; // 是否存在鼠標lblMsg.Text += Environment.NewLine;lblMsg.Text += "MouseCapabilities.HorizontalWheelPresent: " + mouseCapabilities.HorizontalWheelPresent; // 是否有水平滾輪lblMsg.Text += Environment.NewLine;lblMsg.Text += "MouseCapabilities.VerticalWheelPresent: " + mouseCapabilities.VerticalWheelPresent; // 是否有垂直滾輪lblMsg.Text += Environment.NewLine;lblMsg.Text += "MouseCapabilities.SwapButtons: " + mouseCapabilities.SwapButtons; // 是否交換了左右按鈕lblMsg.Text += Environment.NewLine;lblMsg.Text += "MouseCapabilities.NumberOfButtons: " + mouseCapabilities.NumberOfButtons; // 鼠標上的按鈕數量lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;// 獲取硬件鍵盤設備的相關信息KeyboardCapabilities keyboardCapabilities = new KeyboardCapabilities();lblMsg.Text += "KeyboardCapabilities.KeyboardPresent: " + keyboardCapabilities.KeyboardPresent; // 是否存在硬件鍵盤lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;// 獲取觸摸設備的相關信息TouchCapabilities touchCapabilities = new TouchCapabilities();lblMsg.Text += "TouchCapabilities.TouchPresent: " + touchCapabilities.TouchPresent; // 是否存在觸摸設備lblMsg.Text += Environment.NewLine;lblMsg.Text += "TouchCapabilities.Contacts: " + touchCapabilities.Contacts; // 觸摸設備所支持的多點觸摸的點數lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;// 獲取 Pointer 設備(Touch, Pen, Mouse)的相關信息var pointerDeviceList = PointerDevice.GetPointerDevices();int displayIndex = 0;foreach (PointerDevice pointerDevice in pointerDeviceList){displayIndex++;lblMsg.Text += "Pointer Device Index: " + displayIndex;lblMsg.Text += Environment.NewLine;lblMsg.Text += "PointerDevice.PointerDeviceType: " + pointerDevice.PointerDeviceType; // Pointer 類型(Touch, Pen, Mouse)lblMsg.Text += Environment.NewLine;lblMsg.Text += "PointerDevice.IsIntegrated: " + pointerDevice.IsIntegrated; // 是否是集成設備lblMsg.Text += Environment.NewLine;lblMsg.Text += "PointerDevice.MaxContacts: " + pointerDevice.MaxContacts; // 最大的同時觸摸點數lblMsg.Text += Environment.NewLine;lblMsg.Text += "PointerDevice.PhysicalDeviceRect: " + pointerDevice.PhysicalDeviceRect; // 物理設備的 RectlblMsg.Text += Environment.NewLine;lblMsg.Text += "PointerDevice.ScreenRect: " + pointerDevice.ScreenRect; // Pointer 設備所支持的屏幕的 RectlblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;}}} }
2、演示 SIP(Soft Input Panel)的應用
Input/Keyboard/Demo.xaml
Input/Keyboard/Demo.xaml.cs
/** 演示 SIP(Soft Input Panel)的應用* * 注:* 1、TextBox, RichEditBox, PasswordBox 等文本輸入框獲取焦點后,會自動顯示虛擬鍵盤(對于 ReadOnly 的輸入框,即使獲取到焦點也不會彈出虛擬鍵盤)* 2、鍵盤相關的事件有 KeyDown 和 KeyUp* 3、WinRT 的 SIP 支持的 InputScope 類型詳見 Windows.UI.Xaml.Input.InputScopeNameValue 枚舉* * * Windows.UI.ViewManagement.InputPane - 軟鍵盤面板* GetForCurrentView() - 獲取當前的 InputPane 對象* OccludedRect - 軟鍵盤面板所占用的矩形區域* Hiding - 軟鍵盤隱藏時觸發的事件* Showing - 軟鍵盤顯示時觸發的事件 * * * 如果需要檢測當前某個虛擬按鍵的狀態,可以用如下方法:* CoreWindow.GetAsyncKeyState(VirtualKey virtualKey)* CoreWindow.GetKeyState(VirtualKey virtualKey)* * 監測鍵盤事件可以通過 CoreWindow.KeyDown 事件* 關于 CoreWindow 的更多內容請參見:Feature/CoreDemo.xaml.cs*/using System; using Windows.System; using Windows.UI.Core; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input;namespace XamlDemo.Input.Keyboard {public sealed partial class Demo : Page{public Demo(){this.InitializeComponent();this.Loaded += Demo_Loaded;}async void Demo_Loaded(object sender, RoutedEventArgs e){// 檢測虛擬按鍵當前的狀態CoreVirtualKeyStates capitalLockState = Windows.UI.Xaml.Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock);if (capitalLockState == CoreVirtualKeyStates.Locked){await new MessageDialog("您的鍵盤處于“大寫”輸入狀態").ShowAsync();}}private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e){// 判斷用戶是否按下了 SIP 上的回車鍵if (e.Key == VirtualKey.Enter){// 將焦點移出文本輸入框,虛擬鍵盤會自動隱藏 txtReadOnly.Focus(FocusState.Programmatic);}}} }
3、演示 Control 的 Tab 導航相關屬性的應用
Input/Keyboard/TabNavigation.xaml
4、演示 Pointer 相關事件的應用
Input/Touch/Pointer.xaml
Input/Touch/Pointer.xaml.cs
/** 演示 Pointer 相關事件的應用* * * PointerRoutedEventArgs - 指針路由事件的事件參數* OriginalSource - 引發此路由事件的對象* Handled - 是否將事件標記為已處理* KeyModifiers - 獲取當前按下的輔助鍵(Windows.System.VirtualKeyModifiers 枚舉)* None, Control, Menu, Shift, Windows* Pointer - 獲取 Pointer 對象* Pointer.PointerDeviceType - 指針設備的類型(Touch, Pen, Mouse)* Pointer.PointerId - 指針標識,可以根據此屬性來區分多點觸摸場景下的不同指針* GetCurrentPoint(UIElement relativeTo) - 返回當前指針相對于指定元素的 PointerPoint 對象* PointerPoint.Position - 指針的位置* PointerPoint.Properties - 返回 PointerPointProperties 對象,有一堆 PointerPoint 的相關屬性* GetIntermediatePoints(UIElement relativeTo) - 返回與此事件關聯的 PointerPoint 的中間值(平均值)集合* * * HoldingRoutedEventArgs - Holding 路由事件的事件參數* OriginalSource - 引發此路由事件的對象* Handled - 是否將事件標記為已處理* PointerDeviceType - 指針設備的類型(Touch, Pen, Mouse)* GetPosition(UIElement relativeTo) - 返回當前指針相對于指定元素的位置* HoldingState - Holding 狀態(Windows.UI.Input.HoldingState 枚舉)* Started, Completed, Canceled* * * UIElement.CapturePointer(Pointer value) - 捕獲此 UIElement 上的指針,即在 UIElement 之外也可以響應 PointerReleased 事件* UIElement.ReleasePointerCapture(Pointer value) - 釋放對此 UIElement 上的指針的捕獲* UIElement.ReleasePointerCaptures() - 釋放全部指針的捕獲* * * 注:通過 CoreWindow.PointerCursor 設置指針光標的樣式*/using System; using Windows.UI.Core; using Windows.UI.Input; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Shapes;namespace XamlDemo.Input.Touch {public sealed partial class Pointer : Page{public Pointer(){this.InitializeComponent();// 修改指針光標的樣式Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Cross, 1);rectangle.PointerEntered += rectangle_PointerEntered;rectangle.PointerExited += rectangle_PointerExited;rectangle.PointerPressed += rectangle_PointerPressed;rectangle.PointerReleased += rectangle_PointerReleased;rectangle.PointerMoved += rectangle_PointerMoved;rectangle.PointerWheelChanged += rectangle_PointerWheelChanged;rectangle.PointerCanceled += rectangle_PointerCanceled;rectangle.PointerCaptureLost += rectangle_PointerCaptureLost;rectangle.Holding += rectangle_Holding;}void rectangle_Holding(object sender, HoldingRoutedEventArgs e){lblMsg.Text += "Holding";lblMsg.Text += Environment.NewLine;}void rectangle_PointerCaptureLost(object sender, PointerRoutedEventArgs e){lblMsg.Text += "PointerCaptureLost " + e.Pointer.PointerId;lblMsg.Text += Environment.NewLine;}void rectangle_PointerCanceled(object sender, PointerRoutedEventArgs e){lblMsg.Text += "PointerCanceled " + e.Pointer.PointerId;lblMsg.Text += Environment.NewLine;}void rectangle_PointerWheelChanged(object sender, PointerRoutedEventArgs e){// 判斷鼠標滾輪的滾動方向lblMsg.Text += "PointerWheelChanged " + e.GetCurrentPoint(null).Properties.MouseWheelDelta;lblMsg.Text += Environment.NewLine;}void rectangle_PointerMoved(object sender, PointerRoutedEventArgs e){// lblMsg.Text += "PointerMoved " + e.Pointer.PointerId;// lblMsg.Text += Environment.NewLine; }void rectangle_PointerReleased(object sender, PointerRoutedEventArgs e){lblMsg.Text += "PointerReleased " + e.Pointer.PointerId;lblMsg.Text += Environment.NewLine;((Rectangle)sender).ReleasePointerCapture(e.Pointer);}void rectangle_PointerPressed(object sender, PointerRoutedEventArgs e){lblMsg.Text += "PointerPressed " + e.Pointer.PointerId;lblMsg.Text += Environment.NewLine;bool hasCapture = ((Rectangle)sender).CapturePointer(e.Pointer);lblMsg.Text += "Got Capture: " + hasCapture;lblMsg.Text += Environment.NewLine;PointerPointProperties props = e.GetCurrentPoint(null).Properties;lblMsg.Text += "接觸區域的邊框: " + props.ContactRect.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "原始輸入的邊框: " + props.ContactRectRaw.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "觸筆設備的筒狀按鈕是否按下: " + props.IsBarrelButtonPressed.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否已由指針設備取消: " + props.IsCanceled.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否來自橡皮擦: " + props.IsEraser.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否來自滾輪: " + props.IsHorizontalMouseWheel.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針是否在觸摸屏的范圍內: " + props.IsInRange.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "是否是反轉的值: " + props.IsInverted.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否來自鼠標左鍵: " + props.IsLeftButtonPressed.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否來自鼠標中鍵: " + props.IsMiddleButtonPressed.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否來自鼠標右鍵: " + props.IsRightButtonPressed.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "輸入是否來自主要指針: " + props.IsPrimary.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "第一個擴展按鈕的按下狀態: " + props.IsXButton1Pressed.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "第二個擴展按鈕的按下狀態: " + props.IsXButton2Pressed.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針施加到觸摸屏上的力度(0.0-1.0): " + props.Pressure.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "觸摸是否被拒絕了: " + props.TouchConfidence.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針狀態的更改類型: " + props.PointerUpdateKind.ToString(); // PointerUpdateKind 枚舉:LeftButtonPressed, LeftButtonReleased 等等lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針設備相關的 Orientation: " + props.Orientation.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針設備相關的 Twist: " + props.Twist.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針設備相關的 XTilt: " + props.XTilt.ToString();lblMsg.Text += Environment.NewLine;lblMsg.Text += "指針設備相關的 YTiltYTilt: " + props.YTilt.ToString();lblMsg.Text += Environment.NewLine;// 輸入設備相關// props.HasUsage(uint usagePage, uint usageId)// props.GetUsageValue(uint usagePage, uint usageId) }void rectangle_PointerExited(object sender, PointerRoutedEventArgs e){lblMsg.Text += "PointerExited " + e.Pointer.PointerId;lblMsg.Text += Environment.NewLine;}void rectangle_PointerEntered(object sender, PointerRoutedEventArgs e){lblMsg.Text += "PointerEntered " + e.Pointer.PointerId;lblMsg.Text += Environment.NewLine;}} }
5、演示 Tap 相關事件的應用
Input/Touch/Tap.xaml
Input/Touch/Tap.xaml.cs
/** 演示 Tap 相關事件的應用* * * TappedRoutedEventArgs - Tap 路由事件的事件參數* DoubleTappedRoutedEventArgs - DoubleTap 路由事件的事件參數* RightTappedRoutedEventArgs - RightTap 路由事件的事件參數* OriginalSource - 引發此路由事件的對象* Handled - 是否將事件標記為已處理* PointerDeviceType - 指針設備的類型(Touch, Pen, Mouse)* GetPosition(UIElement relativeTo) - 返回當前指針相對于指定元素的位置* * * HoldingRoutedEventArgs - Holding 路由事件的事件參數* OriginalSource - 引發此路由事件的對象* Handled - 是否將事件標記為已處理* PointerDeviceType - 指針設備的類型(Touch, Pen, Mouse)* GetPosition(UIElement relativeTo) - 返回當前指針相對于指定元素的位置* HoldingState - Holding 狀態(Windows.UI.Input.HoldingState 枚舉)* Started, Completed, Canceled*/using System; using Windows.UI.Xaml.Controls;namespace XamlDemo.Input.Touch {public sealed partial class Tap : Page{public Tap(){this.InitializeComponent();rectangle.IsTapEnabled = true; // 默認值就是 truerectangle.IsDoubleTapEnabled = true; // 默認值就是 truerectangle.IsRightTapEnabled = true; // 默認值就是 truerectangle.IsHoldingEnabled = true; // 默認值就是 true rectangle.Tapped += rectangle_Tapped;rectangle.DoubleTapped += rectangle_DoubleTapped;rectangle.RightTapped += rectangle_RightTapped;rectangle.Holding += rectangle_Holding;}void rectangle_Holding(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e){lblMsg.Text += "Holding";lblMsg.Text += Environment.NewLine;}void rectangle_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e){lblMsg.Text += "RightTapped";lblMsg.Text += Environment.NewLine;}void rectangle_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e){lblMsg.Text += "DoubleTapped";lblMsg.Text += Environment.NewLine;}void rectangle_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e){lblMsg.Text += "Tapped";lblMsg.Text += Environment.NewLine;}} }
6、關于 AllowDrop, Drop, DragEnter, DragOver, DragLeave
Input/Touch/DragDrop.xaml
?
OK
[源碼下載]
轉載于:https://www.cnblogs.com/lonelyxmas/p/3590100.html
總結
以上是生活随笔為你收集整理的重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android.database.cur
- 下一篇: java信息管理系统总结_java实现科