Skip to main content
Version: next

Set Clipboard

Options​

interface ScrcpySetClipboardControlMessage {
sequence?: bigint;
paste: boolean;
content: string;
}
  • sequence: Ignored.(until v1.20) A sequence number for acknowledgment. When set to a non-zero value, the function returns a Promise that resolves when the device acknowledges the clipboard change. Set to 0n to disable acknowledgment.(since v1.21)
  • paste: Ignored.(until v1.20) Whether to trigger a paste action after setting the clipboard content.(since v1.21)
  • content: The text content to set in the clipboard.

Usage​

// Using `ScrcpyControlMessageSerializer`
const message: Uint8Array = serializer.setClipboard({
content: "Hello, world!",
paste: false,
sequence: 1n,
});

// Using `ScrcpyControlMessageWriter`
await writer.setClipboard({
content: "Hello, world!",
paste: false,
sequence: 1n,
});

// Using `AdbScrcpyClient`
await client.controller!.setClipboard({
content: "Hello, world!",
paste: false,
sequence: 1n,
});

Clipboard acknowledgments​

Set sequence to a non-zero value when the application needs confirmation that the Android device changed its clipboard:

  • sequence: 0n sets the clipboard without sending back an acknowledgment.
  • A non-zero sequence sets the clipboard, and makes the device send back an acknowledgment.

The Promise may take slightly longer to resolve with a non-zero sequence. With sequence: 0n, it resolves immediately after sending the message.

// Immediate return without waiting for device acknowledgment
await client.controller!.setClipboard({
content: "Quick paste",
paste: true,
sequence: 0n,
});

// Wait for device acknowledgment before resolving
const resultPromise = client.controller!.setClipboard({
content: "Confirmed paste",
paste: true,
sequence: 1n,
});

// resultPromise will resolve when device acknowledges the operation
await resultPromise;

This is commonly used to implement a paste operation. To change the clipboard before simulating Ctrl+V, use a non-zero sequence, await the acknowledgment, and then inject the paste shortcut:

import { AndroidKeyCode, AndroidKeyEventMeta, ScrcpyControlMessageWriter } from "@yume-chan/scrcpy";

declare const controller: ScrcpyControlMessageWriter;

await controller.setClipboard({
content: "Text to paste",
paste: false,
sequence: 1n,
});

// The device clipboard is confirmed to contain the text.
await controller.injectKeyDown({
keyCode: AndroidKeyCode.KeyV,
repeat: 0,
metaState: AndroidKeyEventMeta.Ctrl,
});
await controller.injectKeyUp({
keyCode: AndroidKeyCode.KeyV,
repeat: 0,
metaState: AndroidKeyEventMeta.Ctrl,
});

The paste field may perform the paste operation for you. Set paste: true when normal keyboard injection is available and no explicit confirmation is needed:

import { ScrcpyControlMessageWriter } from "@yume-chan/scrcpy";

declare const controller: ScrcpyControlMessageWriter;

await controller.setClipboard({
content: "Text to paste",
paste: true,
sequence: 0n,
});

UHID keyboards do not use ordinary Android key injection for the paste shortcut. In UHID keyboard mode, set the text with paste: false, wait for acknowledgment, and then send the appropriate UHID input report. This prevents the Ctrl+V report from being sent before the device accepts the new clipboard content.

Do not reuse an active sequence number for overlapping calls. Each non-zero sequence should identify one pending acknowledgment. The Promise rejects if the control socket closes or reports an error before the matching acknowledgment arrives.

The control socket must be enabled to receive acknowledgments. See Connect to server for the socket setup.