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

XRSession: select event

· 52CE 我爱测

The WebXR **`select`** event is sent to an XRSession when one of the session's input sources has completed a primary action.

WebXR Device API

The WebXR select event is sent to an XRSession when one of the session's input sources has completed a primary action.

The Element.beforexrselect_event is fired before this event and can prevent this event from being raised.

Syntax

Use the event name in EventTarget/addEventListener:

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

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

onselect = (event) => { }

Event type

An XRInputSourceEvent. Inherits from Event.

XRInputSourceEvent

Description

Trigger

Triggered when a user presses triggers or buttons, taps a touchpad, speaks a command or performs a recognizable gesture when using a video tracking system or handheld controller with an accelerometer.

Use cases

The XRSession.selectstart_event and XRSession.selectend_event events tell you when you might want to display something to the user indicating that the primary action is going on. This might be drawing a controller with the activated button in a new color, or showing the targeted object being grabbed and moved around, starting when selectstart arrives and stopping when selectend is received.

The select event tells your code that the user has completed an action. This might be as simple as throwing an object or pulling the trigger of a gun in a game, or as involved as placing a dragged object at a new location.

If your primary action is a simple trigger action and you don't need to animate anything while the trigger is engaged, you can ignore the selectstart and selectend events and act on the start event.

Examples

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

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

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

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

Specifications

Browser compatibility

See also

  • XRSession.selectstart_event and XRSession.selectend_event
  • Element.beforexrselect_event

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

资料来源与适用范围

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

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

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