public final class InputManager extends Object
Get an instance of this class by calling
Context.getSystemService()
with the argument
Context.INPUT_SERVICE
.
Modifier and Type | Class and Description |
---|---|
static interface |
InputManager.InputDeviceListener
Listens for changes in input devices.
|
static interface |
InputManager.OnTabletModeChangedListener |
static interface |
InputManager.SwitchState |
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_QUERY_KEYBOARD_LAYOUTS
Broadcast Action: Query available keyboard layouts.
|
static int |
DEFAULT_POINTER_SPEED
Pointer Speed: The default pointer speed (0).
|
static int |
INJECT_INPUT_EVENT_MODE_ASYNC
Input Event Injection Synchronization Mode: None.
|
static int |
INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
Input Event Injection Synchronization Mode: Wait for finish.
|
static int |
INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT
Input Event Injection Synchronization Mode: Wait for result.
|
static int |
MAX_POINTER_SPEED
Pointer Speed: The maximum (fastest) pointer speed (7).
|
static String |
META_DATA_KEYBOARD_LAYOUTS
Metadata Key: Keyboard layout metadata associated with
ACTION_QUERY_KEYBOARD_LAYOUTS . |
static int |
MIN_POINTER_SPEED
Pointer Speed: The minimum (slowest) pointer speed (-7).
|
static int |
SWITCH_STATE_OFF
Switch State: Off.
|
static int |
SWITCH_STATE_ON
Switch State: On.
|
static int |
SWITCH_STATE_UNKNOWN
Switch State: Unknown.
|
Modifier and Type | Method and Description |
---|---|
void |
addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
String keyboardLayoutDescriptor)
Adds the keyboard layout descriptor for the specified input device.
|
boolean[] |
deviceHasKeys(int[] keyCodes)
Queries the framework about whether any physical keys exist on the
any keyboard attached to the device that are capable of producing the given
array of key codes.
|
boolean[] |
deviceHasKeys(int id,
int[] keyCodes)
Queries the framework about whether any physical keys exist on the
any keyboard attached to the device that are capable of producing the given
array of key codes.
|
String |
getCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier)
Gets the current keyboard layout descriptor for the specified input
device.
|
String[] |
getEnabledKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier)
Gets all keyboard layout descriptors that are enabled for the specified
input device.
|
InputDevice |
getInputDevice(int id)
Gets information about the input device with the specified id.
|
InputDevice |
getInputDeviceByDescriptor(String descriptor)
Gets information about the input device with the specified descriptor.
|
int[] |
getInputDeviceIds()
Gets the ids of all input devices in the system.
|
Vibrator |
getInputDeviceVibrator(int deviceId)
Gets a vibrator service associated with an input device, assuming it has one.
|
static InputManager |
getInstance()
Gets an instance of the input manager.
|
KeyboardLayout |
getKeyboardLayout(String keyboardLayoutDescriptor)
Gets the keyboard layout with the specified descriptor.
|
KeyboardLayout |
getKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
InputMethodInfo inputMethodInfo,
InputMethodSubtype inputMethodSubtype)
Gets the keyboard layout for the specified input device and IME subtype.
|
KeyboardLayout[] |
getKeyboardLayouts()
Gets information about all supported keyboard layouts.
|
KeyboardLayout[] |
getKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier)
Gets information about all supported keyboard layouts appropriate
for a specific input device.
|
int |
getPointerSpeed(Context context)
Gets the mouse pointer speed.
|
TouchCalibration |
getTouchCalibration(String inputDeviceDescriptor,
int surfaceRotation)
Gets the TouchCalibration applied to the specified input device's coordinates.
|
boolean |
injectInputEvent(InputEvent event,
int mode)
Injects an input event into the event system on behalf of an application.
|
int |
isInTabletMode()
Queries whether the device is in tablet mode.
|
void |
registerInputDeviceListener(InputManager.InputDeviceListener listener,
Handler handler)
Registers an input device listener to receive notifications about when
input devices are added, removed or changed.
|
void |
registerOnTabletModeChangedListener(InputManager.OnTabletModeChangedListener listener,
Handler handler)
Register a tablet mode changed listener.
|
void |
removeKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
String keyboardLayoutDescriptor)
Removes the keyboard layout descriptor for the specified input device.
|
void |
setCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
String keyboardLayoutDescriptor)
Sets the current keyboard layout descriptor for the specified input
device.
|
void |
setCustomPointerIcon(PointerIcon icon) |
void |
setKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
InputMethodInfo inputMethodInfo,
InputMethodSubtype inputMethodSubtype,
String keyboardLayoutDescriptor)
Sets the keyboard layout for the specified input device and IME subtype pair.
|
void |
setPointerIconType(int iconId)
Changes the mouse pointer's icon shape into the specified id.
|
void |
setPointerSpeed(Context context,
int speed)
Sets the mouse pointer speed.
|
void |
setTouchCalibration(String inputDeviceDescriptor,
int surfaceRotation,
TouchCalibration calibration)
Sets the TouchCalibration to apply to the specified input device's coordinates.
|
void |
tryPointerSpeed(int speed)
Changes the mouse pointer speed temporarily, but does not save the setting.
|
void |
unregisterInputDeviceListener(InputManager.InputDeviceListener listener)
Unregisters an input device listener.
|
void |
unregisterOnTabletModeChangedListener(InputManager.OnTabletModeChangedListener listener)
Unregister a tablet mode changed listener.
|
public static final String ACTION_QUERY_KEYBOARD_LAYOUTS
The input manager service locates available keyboard layouts by querying broadcast receivers that are registered for this action. An application can offer additional keyboard layouts to the user by declaring a suitable broadcast receiver in its manifest.
Here is an example broadcast receiver declaration that an application might include in its AndroidManifest.xml to advertise keyboard layouts. The meta-data specifies a resource that contains a description of each keyboard layout that is provided by the application.
<receiver android:name=".InputDeviceReceiver"
android:label="@string/keyboard_layouts_label">
<intent-filter>
<action android:name="android.hardware.input.action.QUERY_KEYBOARD_LAYOUTS" />
</intent-filter>
<meta-data android:name="android.hardware.input.metadata.KEYBOARD_LAYOUTS"
android:resource="@xml/keyboard_layouts" />
</receiver>
In the above example, the @xml/keyboard_layouts
resource refers to
an XML resource whose root element is <keyboard-layouts>
that
contains zero or more <keyboard-layout>
elements.
Each <keyboard-layout>
element specifies the name, label, and location
of a key character map for a particular keyboard layout. The label on the receiver
is used to name the collection of keyboard layouts provided by this receiver in the
keyboard layout settings.
<?xml version="1.0" encoding="utf-8"?>
<keyboard-layouts xmlns:android="http://schemas.android.com/apk/res/android">
<keyboard-layout android:name="keyboard_layout_english_us"
android:label="@string/keyboard_layout_english_us_label"
android:keyboardLayout="@raw/keyboard_layout_english_us" />
</keyboard-layouts>
The android:name
attribute specifies an identifier by which
the keyboard layout will be known in the package.
The android:label
attribute specifies a human-readable descriptive
label to describe the keyboard layout in the user interface, such as "English (US)".
The android:keyboardLayout
attribute refers to a
key character map resource that defines the keyboard layout.
public static final String META_DATA_KEYBOARD_LAYOUTS
ACTION_QUERY_KEYBOARD_LAYOUTS
.
Specifies the resource id of a XML resource that describes the keyboard layouts that are provided by the application.
public static final int MIN_POINTER_SPEED
public static final int MAX_POINTER_SPEED
public static final int DEFAULT_POINTER_SPEED
public static final int INJECT_INPUT_EVENT_MODE_ASYNC
public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT
public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
public static final int SWITCH_STATE_UNKNOWN
public static final int SWITCH_STATE_OFF
public static final int SWITCH_STATE_ON
public static InputManager getInstance()
public InputDevice getInputDevice(int id)
id
- The device id.public InputDevice getInputDeviceByDescriptor(String descriptor)
descriptor
- The input device descriptor.public int[] getInputDeviceIds()
public void registerInputDeviceListener(InputManager.InputDeviceListener listener, Handler handler)
listener
- The listener to register.handler
- The handler on which the listener should be invoked, or null
if the listener should be invoked on the calling thread's looper.unregisterInputDeviceListener(android.hardware.input.InputManager.InputDeviceListener)
public void unregisterInputDeviceListener(InputManager.InputDeviceListener listener)
listener
- The listener to unregister.registerInputDeviceListener(android.hardware.input.InputManager.InputDeviceListener, android.os.Handler)
public int isInTabletMode()
SWITCH_STATE_UNKNOWN
,
SWITCH_STATE_OFF
or SWITCH_STATE_ON
.public void registerOnTabletModeChangedListener(InputManager.OnTabletModeChangedListener listener, Handler handler)
listener
- The listener to register.handler
- The handler on which the listener should be invoked, or null
if the listener should be invoked on the calling thread's looper.public void unregisterOnTabletModeChangedListener(InputManager.OnTabletModeChangedListener listener)
listener
- The listener to unregister.public KeyboardLayout[] getKeyboardLayouts()
The input manager consults the built-in keyboard layouts as well
as all keyboard layouts advertised by applications using a
ACTION_QUERY_KEYBOARD_LAYOUTS
broadcast receiver.
public KeyboardLayout[] getKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier)
The input manager consults the built-in keyboard layouts as well
as all keyboard layouts advertised by applications using a
ACTION_QUERY_KEYBOARD_LAYOUTS
broadcast receiver.
public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor)
keyboardLayoutDescriptor
- The keyboard layout descriptor, as returned by
KeyboardLayout.getDescriptor()
.public String getCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier)
identifier
- Identifier for the input devicepublic void setCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, String keyboardLayoutDescriptor)
This method may have the side-effect of causing the input device in question to be reconfigured.
identifier
- The identifier for the input device.keyboardLayoutDescriptor
- The keyboard layout descriptor to use,
must not be null.public String[] getEnabledKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier)
identifier
- The identifier for the input device.public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, String keyboardLayoutDescriptor)
This method may have the side-effect of causing the input device in question to be reconfigured.
identifier
- The identifier for the input device.keyboardLayoutDescriptor
- The descriptor of the keyboard layout to
add.public void removeKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, String keyboardLayoutDescriptor)
This method may have the side-effect of causing the input device in question to be reconfigured.
identifier
- The identifier for the input device.keyboardLayoutDescriptor
- The descriptor of the keyboard layout to
remove.public KeyboardLayout getKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, InputMethodInfo inputMethodInfo, InputMethodSubtype inputMethodSubtype)
identifier
- The identifier for the input device.inputMethodInfo
- The input method.inputMethodSubtype
- The input method subtype. null
if this input method does
not support any subtype.KeyboardLayout
, or null if one has not been set.public void setKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, InputMethodInfo inputMethodInfo, InputMethodSubtype inputMethodSubtype, String keyboardLayoutDescriptor)
identifier
- The identifier for the input device.inputMethodInfo
- The input method with which to associate the keyboard layout.inputMethodSubtype
- The input method subtype which which to associate the keyboard
layout. null
if this input method does not support any subtype.keyboardLayoutDescriptor
- The descriptor of the keyboard layout to setpublic TouchCalibration getTouchCalibration(String inputDeviceDescriptor, int surfaceRotation)
inputDeviceDescriptor
- The input device descriptor.public void setTouchCalibration(String inputDeviceDescriptor, int surfaceRotation, TouchCalibration calibration)
This method may have the side-effect of causing the input device in question
to be reconfigured. Requires android.Manifest.permissions.SET_INPUT_CALIBRATION
.
inputDeviceDescriptor
- The input device descriptor.calibration
- The calibration to be appliedpublic int getPointerSpeed(Context context)
Only returns the permanent mouse pointer speed. Ignores any temporary pointer
speed set by tryPointerSpeed(int)
.
context
- The application context.MIN_POINTER_SPEED
and
MAX_POINTER_SPEED
, or the default value DEFAULT_POINTER_SPEED
.public void setPointerSpeed(Context context, int speed)
Requires android.Manifest.permissions.WRITE_SETTINGS
.
context
- The application context.speed
- The pointer speed as a value between MIN_POINTER_SPEED
and
MAX_POINTER_SPEED
, or the default value DEFAULT_POINTER_SPEED
.public void tryPointerSpeed(int speed)
Requires android.Manifest.permission.SET_POINTER_SPEED
.
speed
- The pointer speed as a value between MIN_POINTER_SPEED
and
MAX_POINTER_SPEED
, or the default value DEFAULT_POINTER_SPEED
.public boolean[] deviceHasKeys(int[] keyCodes)
keyCodes
- The array of key codes to query.public boolean[] deviceHasKeys(int id, int[] keyCodes)
id
- The id of the device to query.keyCodes
- The array of key codes to query.public boolean injectInputEvent(InputEvent event, int mode)
Requires android.Manifest.permission.INJECT_EVENTS
to inject into
windows that are owned by other applications.
Make sure you correctly set the event time and input source of the event before calling this method.
event
- The event to inject.mode
- The synchronization mode. One of:
INJECT_INPUT_EVENT_MODE_ASYNC
,
INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT
, or
INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
.public void setPointerIconType(int iconId)
iconId
- The id of the pointer graphic, as a value between
PointerIcon.TYPE_ARROW
and PointerIcon.TYPE_GRABBING
.public void setCustomPointerIcon(PointerIcon icon)
public Vibrator getInputDeviceVibrator(int deviceId)