InputDriverEvent

public class InputDriverEvent
extends Object

java.lang.Object
   ↳ com.google.android.things.userdriver.input.InputDriverEvent


An event to send to an InputDriver. A single event reflects a change in state of one or more components (buttons, motion axes, etc.) of the driver, all happening since the last event.

Usage Example


 InputDriver driver = ...;  // See InputDriver class.
 InputDriverEvent event = new InputDriverEvent();

 event.setKeyPressed(KeyEvent.KEYCODE_A, true);
 event.setKeyPressed(KeyEvent.KEYCODE_B, true);
 driver.emit(event)

 event.clear();
 event.setPosition(MotionEvent.AXIS_X, 10);
 event.setPosition(MotionEvent.AXIS_Y, 5);
 event.setContact(true);
 driver.emit(event);

 event.clear();
 event.setPosition(MotionEvent.AXIS_X, 5);
 event.setKeyPressed(KeyEvent.KEYCODE_A, false);
 driver.emit(event);

 event.clear();
 event.setContact(false);
 event.setKeyPressed(KeyEvent.KEYCODE_B, false);
 driver.emit(event);
 

This describes a sequence of 4 moments in time: 1) A and B keys were pressed. 2) Positional contact was made at (10,5) 3) Contact moved to (5,5), A key was released. 4) Contact and B key were released. Notice that components not explicitly changed are assumed to stay in the same state.

Clearing Events

Given that components not explicitly changed are assumed to maintain state, it's best practice, but not strictly necessary, to call clear() between emissions. This will only matter if the same event is emitted via multiple drivers, or with a different event between emissions via the same driver, because state is driver-specific. Clearing before each re-use helps avoid potential mix ups if drawing from a common pool of event objects.

Summary

Public constructors

InputDriverEvent()

Constructor.

Public methods

void clear()

Clear the event.

void setContact(boolean contact)

Sets the contact state of the positional input.

void setKeyPressed(int code, boolean pressed)

Sets a key state.

void setPosition(int axis, int value)

Sets a position along an axis.

Inherited methods

From class java.lang.Object

Public constructors

InputDriverEvent

InputDriverEvent ()

Constructor. Creates a new event with no state changes (until state setters are called).

Public methods

clear

void clear ()

Clear the event. After this call, the event will indicate no state changes (until state setters are called).

setContact

void setContact (boolean contact)

Sets the contact state of the positional input. Used to indicate when an input tool (e.g. finger or stylus) is determined to be within meaningful physical contact with the positional input device. "Meaningful" is up to the driver implementer; it could be any contact, or it could be some pressure threshold.

Parameters
contact boolean: true if an input tool is in contact with the device.

setKeyPressed

void setKeyPressed (int code, 
                boolean pressed)

Sets a key state. Multiple keys can have their state updated in a single event, simply call this method for each of them.

Parameters
code int: A key code representing the key being changed. Must be a value from KeyEvent.

pressed boolean: true if the key is pressed, false if released.

Throws
IllegalArgumentException if code is not a recognized key code.

setPosition

void setPosition (int axis, 
                int value)

Sets a position along an axis.

Parameters
axis int: One of the axis constants from MotionEvent. Currently only X and Y are supported.

value int: The position along that axis.

Throws
IllegalArgumentException if axis is not supported.