Skip to main content
Version: 1.0.0

Keyboard event

Inject a keyboard event into the device.

Options

export const AndroidKeyEventAction = {
Down: 0,
Up: 1,
} as const;

export type AndroidKeyEventAction =
(typeof AndroidKeyEventAction)[keyof typeof AndroidKeyEventAction];

export const AndroidKeyEventMeta = {
None: 0,
Alt: 0x02,
AltLeft: 0x10,
AltRight: 0x20,
Shift: 0x01,
ShiftLeft: 0x40,
ShiftRight: 0x80,
Ctrl: 0x1000,
CtrlLeft: 0x2000,
CtrlRight: 0x4000,
Meta: 0x10000,
MetaLeft: 0x20000,
MetaRight: 0x40000,
CapsLock: 0x100000,
NumLock: 0x200000,
ScrollLock: 0x400000,
} as const;

export type AndroidKeyEventMeta =
(typeof AndroidKeyEventMeta)[keyof typeof AndroidKeyEventMeta];

interface ScrcpyInjectKeyCodeControlMessage {
action: AndroidKeyEventAction;
keyCode: number;
repeat: number;
metaState: AndroidKeyEventMeta;
}
  • action: The action of the event.
  • keyCode: The key code of the event. It uses Android key codes, see AndroidKeyCode enum for a mapping between Web key codes to Android key codes.
  • repeat: The number of times the key was repeated.
  • metaState: The meta state of the event.

Usage

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

// Using `ScrcpyControlMessageSerializer`
const message: Uint8Array = serializer.injectKeyCode({
action: AndroidKeyEventAction.Down,
keyCode: AndroidKeyCode.KeyA,
repeat: 0,
metaState: AndroidKeyEventMeta.Shift,
});

// Using `ScrcpyControlMessageWriter`
await writer.injectKeyCode({
action: AndroidKeyEventAction.Down,
keyCode: AndroidKeyCode.KeyA,
repeat: 0,
metaState: AndroidKeyEventMeta.Shift,
});

// Using `AdbScrcpyClient`
await client.controller!.injectKeyCode({
action: AndroidKeyEventAction.Down,
keyCode: AndroidKeyCode.KeyA,
repeat: 0,
metaState: AndroidKeyEventMeta.Shift,
});