Skip to main content
Version: next

Control device

Scrcpy supports various input events, including mouse, touch, keyboard, and physical buttons. The client sends input events to the server, which injects them into the device.

There are three ways to create or send control messages, they provide the same set of methods:

Using @yume-chan/scrcpy

Using ScrcpyControlMessageSerializer

The ScrcpyControlMessageSerializer class provides methods to serialize events into Scrcpy messages. It's up to the caller to send the serialized message to the server.

The message format differs between Scrcpy versions, so a ScrcpyOptionsX_YY instance is required when creating the serializer.

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

const options = new ScrcpyOptions1_21({});
const serializer = new ScrcpyControlMessageSerializer(options);
const message: Uint8Array = serializer.rotateDevice();

Using ScrcpyControlMessageWriter

ScrcpyControlMessageWriter class connects a WritableStream<Uint8Array> to a ScrcpyControlMessageSerializer. It writes serialized messages to the stream.

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

const stream = new WritableStream<Uint8Array>();
const options = new ScrcpyOptions1_21({});
const writer = new ScrcpyControlMessageWriter(stream.getWriter(), options);

await writer.rotateDevice();

It has the same set of methods as ScrcpyControlMessageSerializer, plus a write method to write arbitrary data to the stream.

await writer.write(new Uint8Array([0x01, 0x02, 0x03]));

Make sure to write the whole message at once, otherwise it might be interleaved with other messages.

Using @yume-chan/adb-scrcpy

If the control socket is enabled, AdbScrcpyClient.controller is a ScrcpyControlMessageWriter that connects to the server's control socket.

if (client.controller) {
await client.controller.rotateDevice();
}

AdbScrcpyClient also hooks up the device message socket to the options object, so setClipboard method can be used directly.