Skip to main content

Overview

The EventController class runs on the scrcpy server side and processes input events received from the client, injecting them into the Android system. It supports multi-touch gestures, keyboard input, and screen power management. Package: org.server.scrcpy Location: Server-side (runs on the Android device being controlled)

Initialization

Constructor

Creates a new EventController instance.
Device
required
Device instance for injecting events and accessing screen state
DroidConnection
required
Connection instance for receiving control events from the client
Example:

Main Control Loop

control()

Main event processing loop that receives and handles input events.
Behavior:
  • Turns the screen on when starting
  • Continuously receives events from the client
  • Routes events to appropriate handlers
  • Blocks until an IOException occurs
Example:
Event Loop Implementation:

Touch Event Handling

injectTouch()

Processes and injects touch events with multi-touch support.
int
required
MotionEvent action constant (e.g., ACTION_DOWN, ACTION_MOVE, ACTION_UP)
long
required
Unique identifier for the touch pointer (0-9 for multi-touch)
Point
required
Touch coordinates on the screen
int
required
Button state flags
boolean
Returns true if the event was successfully injected
Supported Actions:
int
First finger touches the screen
int
Last finger leaves the screen
int
Touch point moves
int
Additional finger touches (for multi-touch)
int
Additional finger leaves (for multi-touch)
Multi-Touch Implementation:

Key Event Handling

injectKeycode()

Injects a keycode as both DOWN and UP events.
int
required
Android KeyEvent keycode constant
boolean
Returns true if both events were successfully injected
Common Keycodes:
  • KeyEvent.KEYCODE_POWER - Power button
  • KeyEvent.KEYCODE_BACK - Back button
  • KeyEvent.KEYCODE_HOME - Home button
  • KeyEvent.KEYCODE_VOLUME_UP - Volume up
  • KeyEvent.KEYCODE_VOLUME_DOWN - Volume down
Implementation:

injectKeyEvent()

Injects a single key event with full control.
int
required
KeyEvent.ACTION_DOWN or KeyEvent.ACTION_UP
int
required
Android KeyEvent keycode
int
required
Repeat count for the key event
int
required
Meta key state (Shift, Alt, Ctrl, etc.)
Implementation:

Event Data Format

Events are received as integer arrays from the client.

Touch Event Format

Array Structure (5 integers = 20 bytes):
Example:

Key Event Format

Array Structure (1 integer = 4 bytes):
Detection Logic:

Special Keycodes

int
Proximity sensor near - Sets proximity flag to true
int
Proximity sensor far - Sets proximity flag to false

Multi-Touch Support

The EventController supports up to 10 simultaneous touch points.

PointersState

Manages the state of all active touch pointers.
Pointer Initialization:

Pointer Tracking Example

Screen Power Management

The EventController includes special handling for screen wake-up.

Double-Tap Wake

When the screen is off or proximity sensor is active, tapping twice quickly wakes the screen:

turnScreenOn()

Ensures the screen is on when the controller starts.
Implementation:

Event Injection

injectEvent()

Low-level method to inject events into the Android system.
InputEvent
required
The MotionEvent or KeyEvent to inject
boolean
Returns true if injection was successful
Implementation:
Events are injected asynchronously (INJECT_INPUT_EVENT_MODE_ASYNC) for better performance and to avoid blocking the control loop.

Coordinate Transformation

Touch coordinates are transformed from client screen space to device physical coordinates:
This handles:
  • Screen rotation
  • Resolution differences
  • Aspect ratio adjustments

Usage Example

Complete server-side event handling setup:

Performance Considerations

Event injection requires system-level permissions. The server must run with appropriate privileges to inject input events.
Key Points:
  • Events are processed sequentially in the order received
  • Asynchronous injection prevents blocking
  • Multi-touch state is tracked automatically
  • Coordinate transformation is applied per-event
  • Screen state affects touch event handling

See Also