UIElement.PointerPressed 事件

定義

發生于指標裝置在這個專案內起始 Press 動作時。

public:
 virtual event PointerEventHandler ^ PointerPressed;
// Register
event_token PointerPressed(PointerEventHandler const& handler) const;

// Revoke with event_token
void PointerPressed(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::PointerPressed_revoker PointerPressed(auto_revoke_t, PointerEventHandler const& handler) const;
public event PointerEventHandler PointerPressed;
function onPointerPressed(eventArgs) { /* Your code */ }
uIElement.addEventListener("pointerpressed", onPointerPressed);
uIElement.removeEventListener("pointerpressed", onPointerPressed);
- or -
uIElement.onpointerpressed = onPointerPressed;
Public Custom Event PointerPressed As PointerEventHandler 
<uiElement PointerPressed="eventhandler"/>

事件類型

備註

觸控、滑鼠和手寫筆/手寫筆互動會在 UWP 應用程式中接收、處理及管理為指標輸入。 這些互動中的任何一個都可以產生 PointerPressed 事件。 如需詳細資訊,請參閱 處理指標輸入。 指標事件適用于您有興趣多個指標及其關聯性的案例,或當您檢查每個指標的特定專案時,例如確切座標位置。 否則,您可以考慮處理手勢事件,例如 點選

使用以 PointerEventHandler 為基礎的處理常式來處理此事件。

滑鼠輸入會與第一次偵測到滑鼠輸入時指派的單一指標相關聯。 按一下滑鼠按鈕 (左鍵、滾輪或右鍵) 會透過 PointerPressed 事件建立指標與該按鈕的次要關聯。 只在放開相同的滑鼠按鈕時才會觸發 PointerReleased 事件 (這個事件完成前,沒有其他按鈕可以與該指標關聯)。 由於這個專屬關聯的關係,其他滑鼠按鈕的按一下都會經由 PointerMoved 事件進行路由。 您可以在處理此事件時測試滑鼠按鍵狀態,如下列範例所示。

private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

    // Multiple, simultaneous mouse button inputs are processed here.
    // Mouse input is associated with a single pointer assigned when 
    // mouse input is first detected. 
    // Clicking additional mouse buttons (left, wheel, or right) during 
    // the interaction creates secondary associations between those buttons 
    // and the pointer through the pointer pressed event. 
    // The pointer released event is fired only when the last mouse button 
    // associated with the interaction (not necessarily the initial button) 
    // is released. 
    // Because of this exclusive association, other mouse button clicks are 
    // routed through the pointer move event.          
    if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
        if (ptrPt.Properties.IsLeftButtonPressed)
        {
            eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsMiddleButtonPressed)
        {
            eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsRightButtonPressed)
        {
            eventLog.Text += "\nRight button: " + ptrPt.PointerId;
        }
    }

    // Prevent most handlers along the event route from handling the same event again.
    e.Handled = true;

    // Display pointer details.
    updateInfoPop(e);
}
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

    // Multiple, simultaneous mouse button inputs are processed here.
    // Mouse input is associated with a single pointer assigned when 
    // mouse input is first detected. 
    // Clicking additional mouse buttons (left, wheel, or right) during 
    // the interaction creates secondary associations between those buttons 
    // and the pointer through the pointer pressed event. 
    // The pointer released event is fired only when the last mouse button 
    // associated with the interaction (not necessarily the initial button) 
    // is released. 
    // Because of this exclusive association, other mouse button clicks are 
    // routed through the pointer move event.          
    if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
        if (ptrPt.Properties.IsLeftButtonPressed)
        {
            eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsMiddleButtonPressed)
        {
            eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsRightButtonPressed)
        {
            eventLog.Text += "\nRight button: " + ptrPt.PointerId;
        }
    }

    // Prevent most handlers along the event route from handling the same event again.
    e.Handled = true;

    // Display pointer details.
    updateInfoPop(e);
}

滑鼠輸入裝置的指標事件通常會在應用程式存留期的事件詳細資料中具有相同的 PointerId

PointerPressed 是路由事件。 如需路由事件概念的詳細資訊,請參閱 事件和路由事件概觀

對於觸控動作以及因為觸控動作而引發的互動特定或操作事件,元素必須具有點擊測試可見性,才能成為事件來源並引發與動作相關聯的事件。 UIElement.Visibility 必須是 可見的。 衍生型別的其他屬性也會影響點擊測試可見度。 如需詳細資訊,請參閱事件與路由事件概觀

PointerPressed 支援將事件處理常式附加至將叫用的路由,即使事件的事件資料標示為 Handled 也一樣。 請參閱 AddHandler

特定Windows 執行階段控制項可能有 PointerPressed 輸入事件的類別型處理。 如果是,控制項可能具有 OnPointerPressed方法的覆寫。 一般而言,事件會由類別處理常式標示為處理,而且不會引發 PointerPressed 事件,以供該控制項上的任何使用者程式碼處理常式處理。 例如, ButtonBase 具有處理 PointerPressed 的類別處理,並改為引發 Click。 如需事件類別型處理運作方式的詳細資訊,請參閱 事件和路由事件概觀

控制項也可能有 PointerDownThemeAnimation 個人化動畫,與事件無關。

指標擷

當擷取指標時,PointerPressed 有時會當做起始事件使用,如此一來,只要指標保持向下,特定元素就會擷取進一步的指標相關事件。 若要擷取指標,您可以在應該維護擷取的特定 UI 元素上呼叫 CapturePointer 方法。 這通常是在 PointerPressed 事件處理常式內完成,而且您在事件的傳送者上呼叫CapturePointer。 如需如何擷取指標以及如何執行此動作的詳細資訊,請參閱 CapturePointer

PointerPressed 和手勢事件,或操作事件

一開始引發 PointerPressed 事件的使用者動作最終會導致 代表保留 手勢的 Hold 事件,除非輸入裝置是滑鼠。 如果 PointerPressed 發生的專案具有非預設 的 ManipulationMode 值,則動作也可能會導致各種操作事件,例如 ManipulationStarted。 如需詳細資訊,請參閱 處理指標輸入中的一節。

當 PointerPressed 引發時, 點選 之類的手勢事件尚未引發,因為 保留 以外的手勢事件正在等待指標釋放,再引發事件。

作為應用程式使用者互動的一般設計規則,您應該檢查是否有可處理互動的控制項特定事件,還是適當的手勢事件。 例如,如果您的控制項是 Button,該控制項有一個 Click 事件,該事件特別適用于使用者叫用按鈕的動作。 或者,如果您的元素不是按鈕,但您正在處理元素的主要動作和事件,則會處理 點選

您可以藉由設定 IsTapEnabled之類的屬性,在個別元素上特別停用手勢事件。 如果您正在處理操作,您可能會停用手勢事件,但如果您正在處理指標事件,通常不需要停用手勢事件。 指標事件無法特別停用,但您可以選擇不處理它們。

PointerPressed 和 PointerReleased

其他事件而非 PointerReleased 可能會在動作結束時引發,例如 PointerCanceledPointerCaptureLost。 請勿依賴 PointerPressed 和 PointerReleased 事件一律以配對方式發生。 若要正常運作,您的應用程式必須接聽並處理代表 按下 動作可能結論的所有事件。 您可能會收到 PointerReleased 出現的一些原因如下:

  • 特定硬體如何處理觸控動作和 按下 動作的差異
  • 從不同指標擷取程式設計指標
  • 變更顯示區域關聯性的使用者動作,例如變更解析度或監視設定
  • 輸入互動,例如觸控與先前觸控動作相同表面的手寫筆

適用於

另請參閱