大网数据广告活动
Web 标准与前端开发

XRSession: squeeze event

· 52CE 我爱测

The WebXR **`squeeze`** event is sent to an XRSession when one of the session's input sources has completed a primary squeeze action. Examples of common kinds of primary action are users pressing triggers or buttons, tapping a touchpad, speaking a command, or performing a recogni

WebXR Device API

The WebXR squeeze event is sent to an XRSession when one of the session's input sources has completed a primary squeeze action. Examples of common kinds of primary action are users pressing triggers or buttons, tapping a touchpad, speaking a command, or performing a recognizable gesture when using a video tracking system or handheld controller with an accelerometer.

For details on how the XRSession.squeezestart_event, squeeze, and XRSession.squeezeend_event events work, and how you should react to them, see Inputs and input sources.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("squeeze", (event) => { })

Alternatively, assign to the event handler property of the interface:

onsqueeze = (event) => { }

Event type

An XRInputSourceEvent. Inherits from Event.

XRInputSourceEvent

Description

Trigger

Triggered when users are squeezing the controller, making a hand gesture that mimes grabbing something, or using (squeezing) a trigger.

Use cases

The XRSession.squeezestart_event event indicates that the user has begun a squeeze action.

If the primary squeeze action ends successfully, the session is sent a squeeze event.

A XRSession.squeezeend_event event is sent to indicate that the squeeze action is no longer underway. This is sent whether the squeeze action succeeded or not.

Examples

The following example uses EventTarget.addEventListener to set up a handler for the squeeze event. The handler fetches the pose representing the target ray for tracked-pointer inputs and sends the pose's transform to a function called myHandleSqueezeWithRay().

This code treats the squeeze as an instantaneous action that doesn't involve tracking an ongoing activity. If you need to track a squeeze action that isn't instantaneous, listen for the XRSession.squeezestart_event and XRSession.squeezeend_event events to sense when the squeeze action begins and ends.

xrSession.addEventListener("squeeze", (event) => {
  if (event.inputSource.targetRayMode === "tracked-pointer") {
    let targetRayPose = event.frame.getPose(
      event.inputSource.targetRaySpace,
      myRefSpace,
    );
    if (targetRayPose) {
      myHandleSqueezeWithRay(targetRayPose.transform);
    }
  }
});

You can also set up a handler for squeeze events by setting the XRSession object's onsqueeze event handler property to a function that handles the event (but note that this replaces other event listeners added using onsqueeze, if any):

xrSession.onsqueeze = (event) => {
  if (event.inputSource.targetRayMode === "tracked-pointer") {
    let targetRayPose = event.frame.getPose(
      event.inputSource.targetRaySpace,
      myRefSpace,
    );
    if (targetRayPose) {
      myHandleSqueezeWithRay(targetRayPose.transform);
    }
  }
};

Specifications

Browser compatibility

See also

  • XRSession.squeezestart_event and XRSession.squeezeend_event event

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。