學習如何利用筆裝置的觸覺回饋,在你的 Windows 應用程式 SDK 應用程式中提供更自然且觸感的互動。
Windows 11 為數位筆引入了觸覺回饋,讓使用者能感受到筆與應用程式介面的互動。 提及此功能時,開發人員 API 和說明文件中使用的是 haptic,而在 Windows 設定中向使用者顯示的名稱則是 tactile。
主要 API
- 筆式裝置
- SimpleHaptics控制器
- 已知的簡單觸覺控制器波形
- PenDeviceInterop (Windows 應用程式 SDK)
觸覺反饋類型
有兩種觸覺回饋:
- 描線回饋 — 透過筆觸螢幕時持續震動,模擬書寫或繪畫工具(筆、麥克筆、鉛筆、螢光筆)的觸感。
- 互動回饋 — 使用者動作的簡短觸覺訊號,例如滑鼠移至按鈕、點擊或確認已完成的動作。
偵測筆輸入
為 PointerEntered 事件註冊,並檢查指標裝置的類型是否為手寫筆:
private void InputObserver_PointerEntered(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType != Microsoft.UI.Input.PointerDeviceType.Pen)
{
return;
}
// Pen detected — check for haptic support.
}
在 Windows 應用程式 SDK 中取得 PenDevice
在 Windows 應用程式 SDK 應用程式中,使用 PenDeviceInterop — Windows 應用程式 SDK 互操作類別 — 從指標點取得 WinRT PenDevice 物件。
PenDevice、 SimpleHapticsController、 和 KnownSimpleHapticsControllerWaveforms 是來自 Windows.Devices.Input and Windows.Devices.Haptics 命名空間的 WinRT 類型;只有互操作呼叫本身來自 Windows 應用程式 SDK Microsoft.UI.Input.Interop 的命名空間:
using Microsoft.UI.Input.Interop;
using Windows.Devices.Haptics;
using Windows.Devices.Input;
using Windows.Foundation.Metadata;
private void InputObserver_PointerEntered(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType != Microsoft.UI.Input.PointerDeviceType.Pen)
{
return;
}
var penDevice = PenDeviceInterop.FromPointerPoint(e.GetCurrentPoint(null));
if (penDevice == null)
{
return; // Pen does not support advanced features.
}
// SimpleHapticsController requires Windows 11. Check for API presence
// before accessing it so the app doesn't throw on Windows 10.
if (!ApiInformation.IsPropertyPresent(
"Windows.Devices.Input.PenDevice", "SimpleHapticsController"))
{
return; // Haptic feedback requires Windows 11.
}
var hapticsController = penDevice.SimpleHapticsController;
if (hapticsController == null)
{
return; // Pen does not support haptic feedback.
}
// Configure haptic feedback (see examples below).
}
備註
在 UWP 應用程式中,請改用 PenDevice.GetFromPointerId 。 這個PenDeviceInterop類別是專門針對 Windows 應用程式 SDK 設計的。
墨跡波形
當手寫筆接觸螢幕時,書寫波形會持續播放。 所需的波形保證由任何觸覺筆支援。
| 波形 | Description | 為必填項目 |
|---|---|---|
| 墨水連續 | 預設的原子筆手感。 作為無支援波形的備用方案。 | Yes |
| 刷子連續 | 筆刷工具 | No |
| 鉛筆不間斷 | 鉛筆工具 | No |
| 持續標記 | 標記工具 | No |
| ChiselMarkerContinuous | 鑿子筆/螢光筆 | No |
| EraserContinuous | 橡皮擦工具 | No |
| Galaxy 手寫筆連續書寫 | 特殊墨水(多色畫筆) | No |
交互作用波形
互動波形為使用者介面動作提供簡短且直接的回饋。
| 波形 | Description | 為必填項目 |
|---|---|---|
| Click | 預設的短鍵。 作為無支援互動的備案。 | Yes |
| 錯誤 | 動作失敗或發生錯誤 | No |
| 盤旋 | 開始將游標停留在互動式元素上方 | No |
| 壓 | 按下互動元素 | No |
| 釋放 | 釋出互動元素 | No |
| Success | 行動成功 | No |
| 持續嗡鳴 | 持續的嗡嗡聲 | No |
| 持續震動 | 持續的隆隆聲 | No |
傳送觸覺回饋
反覆 SupportedFeedback 迭代以求出所需波形,然後呼叫 SendHapticFeedback:
SimpleHapticsControllerFeedback currentWaveform = null;
// Try to use BrushContinuous, fall back to InkContinuous.
foreach (var waveform in hapticsController.SupportedFeedback)
{
if (waveform.Waveform == KnownSimpleHapticsControllerWaveforms.BrushContinuous)
{
currentWaveform = waveform;
break;
}
}
if (currentWaveform == null)
{
foreach (var waveform in hapticsController.SupportedFeedback)
{
if (waveform.Waveform == KnownSimpleHapticsControllerWaveforms.InkContinuous)
{
currentWaveform = waveform;
break;
}
}
}
if (currentWaveform != null)
{
hapticsController.SendHapticFeedback(currentWaveform);
}
停止觸覺回饋
當指標離開元素時,務必停止觸覺回饋。 在同一元素上為 PointerExited 註冊:
private void Canvas_PointerExited(object sender, PointerRoutedEventArgs e)
{
hapticsController?.StopFeedback();
}
備註
有些筆在筆離開螢幕時會自動停止觸覺功能。 不過,並非所有筆都能做到這點,所以一定要明確呼叫 StopFeedback 。
自訂觸覺回饋
有些筆支援額外自訂功能。 使用前請先確認是否支援它們:
| 房產 | 查看支援情況 |
|---|---|
| 強度 | IsIntensitySupported |
| 出場次數 | IsPlayCountSupported |
| 遊玩時長 | IsPlayDurationSupported |
| 重播暫停間隔 | IsReplayPauseIntervalSupported |
if (hapticsController.IsIntensitySupported)
{
hapticsController.SendHapticFeedback(currentWaveform, 0.75); // 75% intensity
}