輸入: 獲取輸入設備信息, 虛擬鍵盤, Tab 導航, Pointer, Tap, Drag, Drop
介紹
重新想象 Windows 8 Store Apps 之 輸入
輸入設備的相關信息
SIP(Soft Input Panel)的應用
Tab 鍵導航
Pointer - 指針,鼠標
Tap - 觸摸
Drag 和 Drop
示例
1、演示如何獲取輸入設備的相關信息
Input/InputDeviceInfo.xaml
<Page x:Class="XamlDemo.Input.InputDeviceInfo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Input" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid Margin="120 0 0 0"> <ScrollViewer Margin="0 0 10 10"> <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap" /> </ScrollViewer> </Grid> </Grid> </Page>
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; // 物理設備的 Rect lblMsg.Text += Environment.NewLine; lblMsg.Text += "PointerDevice.ScreenRect: " + pointerDevice.ScreenRect; // Pointer 設備所支持的屏幕的 Rect lblMsg.Text += Environment.NewLine; lblMsg.Text += Environment.NewLine; } } } }
2、演示 SIP(Soft Input Panel)的應用
Input/Keyboard/Demo.xaml
<Page x:Class="XamlDemo.Input.Keyboard.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Input.Keyboard" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <!-- TextBox - 文本輸入框 IsTextPredictionEnabled - 是否啟用“自動完成”,默認值 true IsSpellCheckEnabled - 是否啟用拼音檢查,默認值 false --> <TextBox IsTextPredictionEnabled="True" IsSpellCheckEnabled="True" Margin="0 0 10 0" /> <!-- InputScope - 限制 SIP 的輸入范圍,即設置 SIP 的布局方式 --> <TextBox InputScope="Default" Margin="0 10 10 0" /> <TextBox KeyDown="TextBox_KeyDown_1" Margin="0 10 10 0"> <TextBox.InputScope> <InputScope> <InputScope.Names> <InputScopeName NameValue="TelephoneNumber" /> </InputScope.Names> </InputScope> </TextBox.InputScope> </TextBox> <!-- 對於 ReadOnly 的輸入框,即使獲取到焦點也不會彈出虛擬鍵盤 --> <TextBox Name="txtReadOnly" IsReadOnly="True" Margin="0 10 10 0" /> </StackPanel> </Grid> </Page>
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
<Page x:Class="XamlDemo.Input.Keyboard.TabNavigation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Input.Keyboard" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <!-- 演示 Control 的 Tab 導航相關屬性的應用 Control - 控件 TabIndex - Tab 導航的順序,默認值為 int.MaxValue IsTabStop - 是否加入 Tab 導航,默認值為 true TabNavigation - Tab 導航的工作方式。經測試,沒什麼效果 --> <Button Content="button 1" TabIndex="3" TabNavigation="Once" /> <Button Content="button 2" Margin="0 10 0 0" TabIndex="1" /> <Button Content="button 3" Margin="0 10 0 0" IsTabStop="False" /> <Button Content="button 4" Margin="0 10 0 0" TabIndex="4" /> <Button Content="button 5" Margin="0 10 0 0" TabIndex="2" /> <ListBox Width="200" Height="300" Margin="0 10 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <ListBox.Items> <ListBoxItem Content="ListBoxItem1" /> <ListBoxItem Content="ListBoxItem2" /> <ListBoxItem Content="ListBoxItem3" /> </ListBox.Items> </ListBox> </StackPanel> </Grid> </Page>
4、演示 Pointer 相關事件的應用
Input/Touch/Pointer.xaml
<Page x:Class="XamlDemo.Input.Touch.Pointer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Input.Touch" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid Margin="120 0 0 0"> <Rectangle Name="rectangle" Width="400" Height="100" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Top" /> <ScrollViewer Margin="0 110 0 10" HorizontalAlignment="Stretch" VerticalAlignment="Top"> <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap" /> </ScrollViewer> </Grid> </Grid> </Page>
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
<Page x:Class="XamlDemo.Input.Touch.Tap" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Input.Touch" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <Rectangle Name="rectangle" Width="400" Height="100" Fill="Orange" HorizontalAlignment="Left" /> <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
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; // 默認值就是 true rectangle.IsDoubleTapEnabled = true; // 默認值就是 true rectangle.IsRightTapEnabled = true; // 默認值就是 true rectangle.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
<Page x:Class="XamlDemo.Input.Touch.DragDrop" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Input.Touch" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap"> <Run>關於 AllowDrop, Drop, DragEnter, DragOver, DragLeave 等詳見:Controls/GridView/DragItem.xaml</Run> </TextBlock> </StackPanel> </Grid> </Page>
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar