首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 Store Apps 開發實戰(49):輸入設備偵測、虛擬鍵盤 (SIP)、Tab 導航、Pointer/Tap/DragDrop 全方位指南

     編輯:關於.NET

前言

Windows 8 輸入模型統一了滑鼠、觸控、筆、鍵盤。本文涵蓋:硬體能力偵測、軟鍵盤 (SIP) 控制、Tab 導航屬性、Pointer 事件全家桶、Tap 系列事件、Drag/Drop 基礎,為觸控應用打下堅實基礎。

一、輸入設備能力偵測


// 滑鼠
var mouse = new MouseCapabilities();
Log($"MousePresent: {mouse.MousePresent}");           // 1/0
Log($"Buttons: {mouse.NumberOfButtons}");
Log($"HorizontalWheel: {mouse.HorizontalWheelPresent}");
Log($"VerticalWheel: {mouse.VerticalWheelPresent}");
Log($"SwapButtons: {mouse.SwapButtons}");

// 鍵盤
var kb = new KeyboardCapabilities();
Log($"KeyboardPresent: {kb.KeyboardPresent}");        // 1/0

// 觸控
var touch = new TouchCapabilities();
Log($"TouchPresent: {touch.TouchPresent}");           // 1/0
Log($"Contacts: {touch.Contacts}");                   // 多點觸控點數

// 統一 Pointer 設備列舉
foreach (var dev in PointerDevice.GetPointerDevices())
{
    Log($"Type: {dev.PointerDeviceType}");            // Touch/Pen/Mouse
    Log($"Integrated: {dev.IsIntegrated}");
    Log($"MaxContacts: {dev.MaxContacts}");
    Log($"PhysicalRect: {dev.PhysicalDeviceRect}");
    Log($"ScreenRect: {dev.ScreenRect}");
}

二、SIP (Soft Input Panel) 虛擬鍵盤控制

自動顯示/隱藏規則

  • TextBox/RichEditBox/PasswordBox 獲得焦點 → 自動顯示。
  • IsReadOnly="True" 即使焦點也不顯示。
  • 焦點移離 → 自動隱藏。

InputScope 控制鍵盤佈局


<TextBox InputScope="Default" />
<TextBox>
    <TextBox.InputScope>
        <InputScope><InputScopeName NameValue="TelephoneNumber" /></InputScope>
    </TextBox.InputScope>
</TextBox>

支援 InputScopeNameValue 列舉:Text, Number, TelephoneNumber, EmailSmtpAddress, Url, Search, Formula, Date, Time, ...。

程式碼偵測 CapsLock / 虛擬鍵狀態


var state = Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock);
if (state == CoreVirtualKeyStates.Locked) await new MessageDialog("大寫鎖定開啟").ShowAsync();

// Enter 鍵隱藏鍵盤
void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Enter)
        txtReadOnly.Focus(FocusState.Programmatic); // 移走焦點
}

InputPane 事件 (進階佈局調整)


var pane = InputPane.GetForCurrentView();
pane.Showing += (s,e) => { var rect = e.OccludedRect; /* 調整 UI 避開鍵盤 */ };
pane.Hiding += (s,e) => { /* 還原 UI */ };

三、Tab 導航屬性


<Button Content="Button 1" TabIndex="3" />
<Button Content="Button 2" TabIndex="1" />
<Button Content="Button 3" IsTabStop="False" />  <!-- 排除 Tab 順序 -->
<Button Content="Button 4" TabIndex="4" />
<Button Content="Button 5" TabIndex="2" />

TabNavigation 屬性在 WinRT 中實測效果有限,主要靠 TabIndex + IsTabStop 控制。

四、Pointer 事件全家桶

事件列表

事件觸發時機
PointerEntered / PointerExited指進入/離開元素範圍
PointerPressed / PointerReleased按下/釋放 (含滑鼠、觸控、筆)
PointerMoved移動中 (高頻)
PointerWheelChanged滑鼠滾輪
PointerCanceled / PointerCaptureLost輸入取消/捕獲遺失
Holding長按 (觸控/筆)

關鍵屬性:PointerPointProperties


var props = e.GetCurrentPoint(null).Properties;
Log($"ContactRect: {props.ContactRect}");           // 接觸區域矩形
Log($"Pressure: {props.Pressure}");                 // 0.0-1.0 壓力
Log($"IsLeftButtonPressed: {props.IsLeftButtonPressed}");
Log($"IsRightButtonPressed: {props.IsRightButtonPressed}");
Log($"IsPrimary: {props.IsPrimary}");               // 主要指針 (第一根手指/滑鼠)
Log($"PointerUpdateKind: {props.PointerUpdateKind}"); // 變更類型枚舉

指標捕獲:拖曳超出元素仍接收事件


void Rect_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    ((UIElement)sender).CapturePointer(e.Pointer); // 捕獲
}
void Rect_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    ((UIElement)sender).ReleasePointerCapture(e.Pointer); // 釋放
}

自訂游標


Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Cross, 1);

五、Tap 系列事件

事件觸發條件事件參數關鍵屬性
Tapped單擊/單指輕觸PointerDeviceType, GetPosition()
DoubleTapped雙擊/雙指快速輕觸同上
RightTapped右鍵/長按 (觸控)同上
Holding長按持續HoldingState (Started/Completed/Canceled)

rectangle.IsTapEnabled = true;          // 預設 true
rectangle.IsDoubleTapEnabled = true;
rectangle.IsRightTapEnabled = true;
rectangle.IsHoldingEnabled = true;

六、Drag / Drop 基礎

WinRT AllowDrop, Drop, DragEnter, DragOver, DragLeave 事件模式與 WPF 類似,詳細實作請參考 Controls/GridView/DragItem.xaml 範例。

總結:輸入開發檢查清單

  1. 啟動時偵測裝置能力,適配 UI (如無觸控隱藏手勢提示)。
  2. 為所有文字輸入框設定正確 InputScope。
  3. 處理 InputPane.Showing/Hiding 避免鍵盤遮擋關鍵 UI。
  4. 使用 Pointer 統一事件模型,CapturePointer 實現拖曳。
  5. 依需求啟用 IsTapEnabled/IsHoldingEnabled 等屬性。
  6. 複雜拖放參考 GridView 內建 DragItemsStarting 等高階 API。

原始碼下載**:https://files.cnblogs.com/webabcd/Windows8.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved