Overview
TheEventController 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
Main Control Loop
control()
Main event processing loop that receives and handles input events.- Turns the screen on when starting
- Continuously receives events from the client
- Routes events to appropriate handlers
- Blocks until an IOException occurs
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 injectedint
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)
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 injectedKeyEvent.KEYCODE_POWER- Power buttonKeyEvent.KEYCODE_BACK- Back buttonKeyEvent.KEYCODE_HOME- Home buttonKeyEvent.KEYCODE_VOLUME_UP- Volume upKeyEvent.KEYCODE_VOLUME_DOWN- Volume down
injectKeyEvent()
Injects a single key event with full control.int
required
KeyEvent.ACTION_DOWN or KeyEvent.ACTION_UPint
required
Android KeyEvent keycode
int
required
Repeat count for the key event
int
required
Meta key state (Shift, Alt, Ctrl, etc.)
Event Data Format
Events are received as integer arrays from the client.Touch Event Format
Array Structure (5 integers = 20 bytes):Key Event Format
Array Structure (1 integer = 4 bytes):Special Keycodes
int
Proximity sensor near - Sets proximity flag to
trueint
Proximity sensor far - Sets proximity flag to
falseMulti-Touch Support
The EventController supports up to 10 simultaneous touch points.PointersState
Manages the state of all active touch pointers.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.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 successfulEvents 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:- Screen rotation
- Resolution differences
- Aspect ratio adjustments
Usage Example
Complete server-side event handling setup:Performance Considerations
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
- Scrcpy Service - Client-side service
- Video Decoder - Video stream decoding
- Audio Decoder - Audio stream decoding
