Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. CaptureController
  4. setFocusBehavior()

CaptureController: setFocusBehavior() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

TheCaptureController interface'ssetFocusBehavior() method controls whether the captured tab or window will be focused when an associatedMediaDevices.getDisplayMedia()Promise fulfills, or whether the focus will remain with the tab containing the capturing app.

You can set this behavior multiple times before theMediaDevices.getDisplayMedia() call, or once immediately after itsPromise resolves. After that, the focus behavior is said to be finalized, and can't be changed.

Syntax

js
setFocusBehavior(focusBehavior)

Parameters

focusBehavior

An enumerated value that describes whether the user agent should transfer focus to the captured display surface, or keep the capturing app focused. Possible values arefocus-captured-surface (transfer focus) andno-focus-change (keep focus on the capturing app).

Return value

None (undefined).

Exceptions

InvalidStateErrorDOMException

Thrown if:

  • The capture stream has been stopped.
  • The user chose to share a screen (displaySurface typemonitor) rather than abrowser tab orwindow — you can't focus a monitor. In this case the exception is thrown after theMediaDevices.getDisplayMedia()Promise resolves.
  • Enough time has elapsed after theMediaDevices.getDisplayMedia()Promise fulfills that the focus behavior has been finalized.

Examples

BasicsetFocusBehavior() usage

js
// Create a new CaptureController instanceconst controller = new CaptureController();// Prompt the user to share a tab, window, or screen.const stream = await navigator.mediaDevices.getDisplayMedia({ controller });// Query the displaySurface value of the captured video trackconst [track] = stream.getVideoTracks();const displaySurface = track.getSettings().displaySurface;if (displaySurface === "browser") {  // Focus the captured tab.  controller.setFocusBehavior("focus-captured-surface");} else if (displaySurface === "window") {  // Do not move focus to the captured window.  // Keep the capturing page focused.  controller.setFocusBehavior("no-focus-change");}

Specifications

Specification
Screen Capture
# dom-capturecontroller-setfocusbehavior

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp