FileDocCategorySizeDatePackage
InputDevice.javaAPI DocAndroid 5.1 API33190Thu Mar 12 22:22:10 GMT 2015android.view

InputDevice

public final class InputDevice extends Object implements android.os.Parcelable
Describes the capabilities of a particular input device.

Each input device may support multiple classes of input. For example, a multi-function keyboard may compose the capabilities of a standard keyboard together with a track pad mouse or other pointing device.

Some input devices present multiple distinguishable sources of input. Applications can query the framework about the characteristics of each distinct source.

As a further wrinkle, different kinds of input sources uses different coordinate systems to describe motion events. Refer to the comments on the input source constants for the appropriate interpretation.

Fields Summary
private final int
mId
private final int
mGeneration
private final int
mControllerNumber
private final String
mName
private final int
mVendorId
private final int
mProductId
private final String
mDescriptor
private final android.hardware.input.InputDeviceIdentifier
mIdentifier
private final boolean
mIsExternal
private final int
mSources
private final int
mKeyboardType
private final KeyCharacterMap
mKeyCharacterMap
private final boolean
mHasVibrator
private final boolean
mHasButtonUnderPad
private final ArrayList
mMotionRanges
private android.os.Vibrator
mVibrator
public static final int
SOURCE_CLASS_MASK
A mask for input source classes. Each distinct input source constant has one or more input source class bits set to specify the desired interpretation for its input events.
public static final int
SOURCE_CLASS_NONE
The input source has no class. It is up to the application to determine how to handle the device based on the device type.
public static final int
SOURCE_CLASS_BUTTON
The input source has buttons or keys. Examples: {@link #SOURCE_KEYBOARD}, {@link #SOURCE_DPAD}. A {@link KeyEvent} should be interpreted as a button or key press. Use {@link #getKeyCharacterMap} to query the device's button and key mappings.
public static final int
SOURCE_CLASS_POINTER
The input source is a pointing device associated with a display. Examples: {@link #SOURCE_TOUCHSCREEN}, {@link #SOURCE_MOUSE}. A {@link MotionEvent} should be interpreted as absolute coordinates in display units according to the {@link View} hierarchy. Pointer down/up indicated when the finger touches the display or when the selection button is pressed/released. Use {@link #getMotionRange} to query the range of the pointing device. Some devices permit touches outside the display area so the effective range may be somewhat smaller or larger than the actual display size.
public static final int
SOURCE_CLASS_TRACKBALL
The input source is a trackball navigation device. Examples: {@link #SOURCE_TRACKBALL}. A {@link MotionEvent} should be interpreted as relative movements in device-specific units used for navigation purposes. Pointer down/up indicates when the selection button is pressed/released. Use {@link #getMotionRange} to query the range of motion.
public static final int
SOURCE_CLASS_POSITION
The input source is an absolute positioning device not associated with a display (unlike {@link #SOURCE_CLASS_POINTER}). A {@link MotionEvent} should be interpreted as absolute coordinates in device-specific surface units. Use {@link #getMotionRange} to query the range of positions.
public static final int
SOURCE_CLASS_JOYSTICK
The input source is a joystick. A {@link MotionEvent} should be interpreted as absolute joystick movements. Use {@link #getMotionRange} to query the range of positions.
public static final int
SOURCE_UNKNOWN
The input source is unknown.
public static final int
SOURCE_KEYBOARD
The input source is a keyboard. This source indicates pretty much anything that has buttons. Use {@link #getKeyboardType()} to determine whether the keyboard has alphabetic keys and can be used to enter text.
public static final int
SOURCE_DPAD
The input source is a DPad.
public static final int
SOURCE_GAMEPAD
The input source is a game pad. (It may also be a {@link #SOURCE_JOYSTICK}).
public static final int
SOURCE_TOUCHSCREEN
The input source is a touch screen pointing device.
public static final int
SOURCE_MOUSE
The input source is a mouse pointing device. This code is also used for other mouse-like pointing devices such as trackpads and trackpoints.
public static final int
SOURCE_STYLUS
The input source is a stylus pointing device.

Note that this bit merely indicates that an input device is capable of obtaining input from a stylus. To determine whether a given touch event was produced by a stylus, examine the tool type returned by {@link MotionEvent#getToolType(int)} for each individual pointer.

A single touch event may multiple pointers with different tool types, such as an event that has one pointer with tool type {@link MotionEvent#TOOL_TYPE_FINGER} and another pointer with tool type {@link MotionEvent#TOOL_TYPE_STYLUS}. So it is important to examine the tool type of each pointer, regardless of the source reported by {@link MotionEvent#getSource()}.

public static final int
SOURCE_TRACKBALL
The input source is a trackball.
public static final int
SOURCE_TOUCHPAD
The input source is a touch pad or digitizer tablet that is not associated with a display (unlike {@link #SOURCE_TOUCHSCREEN}).
public static final int
SOURCE_TOUCH_NAVIGATION
The input source is a touch device whose motions should be interpreted as navigation events. For example, an upward swipe should be as an upward focus traversal in the same manner as pressing up on a D-Pad would be. Swipes to the left, right and down should be treated in a similar manner.
public static final int
SOURCE_JOYSTICK
The input source is a joystick. (It may also be a {@link #SOURCE_GAMEPAD}).
public static final int
SOURCE_HDMI
The input source is a device connected through HDMI-based bus. The key comes in through HDMI-CEC or MHL signal line, and is treated as if it were generated by a locally connected DPAD or keyboard.
public static final int
SOURCE_ANY
A special input source constant that is used when filtering input devices to match devices that provide any type of input source.
public static final int
MOTION_RANGE_X
Constant for retrieving the range of values for {@link MotionEvent#AXIS_X}.
public static final int
MOTION_RANGE_Y
Constant for retrieving the range of values for {@link MotionEvent#AXIS_Y}.
public static final int
MOTION_RANGE_PRESSURE
Constant for retrieving the range of values for {@link MotionEvent#AXIS_PRESSURE}.
public static final int
MOTION_RANGE_SIZE
Constant for retrieving the range of values for {@link MotionEvent#AXIS_SIZE}.
public static final int
MOTION_RANGE_TOUCH_MAJOR
Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MAJOR}.
public static final int
MOTION_RANGE_TOUCH_MINOR
Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MINOR}.
public static final int
MOTION_RANGE_TOOL_MAJOR
Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MAJOR}.
public static final int
MOTION_RANGE_TOOL_MINOR
Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MINOR}.
public static final int
MOTION_RANGE_ORIENTATION
Constant for retrieving the range of values for {@link MotionEvent#AXIS_ORIENTATION}.
public static final int
KEYBOARD_TYPE_NONE
There is no keyboard.
public static final int
KEYBOARD_TYPE_NON_ALPHABETIC
The keyboard is not fully alphabetic. It may be a numeric keypad or an assortment of buttons that are not mapped as alphabetic keys suitable for text input.
public static final int
KEYBOARD_TYPE_ALPHABETIC
The keyboard supports a complement of alphabetic keys.
public static final Parcelable.Creator
CREATOR
Constructors Summary
private InputDevice(int id, int generation, int controllerNumber, String name, int vendorId, int productId, String descriptor, boolean isExternal, int sources, int keyboardType, KeyCharacterMap keyCharacterMap, boolean hasVibrator, boolean hasButtonUnderPad)


    // Called by native code.
              
                     
                  
        mId = id;
        mGeneration = generation;
        mControllerNumber = controllerNumber;
        mName = name;
        mVendorId = vendorId;
        mProductId = productId;
        mDescriptor = descriptor;
        mIsExternal = isExternal;
        mSources = sources;
        mKeyboardType = keyboardType;
        mKeyCharacterMap = keyCharacterMap;
        mHasVibrator = hasVibrator;
        mHasButtonUnderPad = hasButtonUnderPad;
        mIdentifier = new InputDeviceIdentifier(descriptor, vendorId, productId);
    
private InputDevice(android.os.Parcel in)

        mId = in.readInt();
        mGeneration = in.readInt();
        mControllerNumber = in.readInt();
        mName = in.readString();
        mVendorId = in.readInt();
        mProductId = in.readInt();
        mDescriptor = in.readString();
        mIsExternal = in.readInt() != 0;
        mSources = in.readInt();
        mKeyboardType = in.readInt();
        mKeyCharacterMap = KeyCharacterMap.CREATOR.createFromParcel(in);
        mHasVibrator = in.readInt() != 0;
        mHasButtonUnderPad = in.readInt() != 0;
        mIdentifier = new InputDeviceIdentifier(mDescriptor, mVendorId, mProductId);

        for (;;) {
            int axis = in.readInt();
            if (axis < 0) {
                break;
            }
            addMotionRange(axis, in.readInt(), in.readFloat(), in.readFloat(), in.readFloat(),
                    in.readFloat(), in.readFloat());
        }
    
Methods Summary
private voidaddMotionRange(int axis, int source, float min, float max, float flat, float fuzz, float resolution)

        mMotionRanges.add(new MotionRange(axis, source, min, max, flat, fuzz, resolution));
    
private voidappendSourceDescriptionIfApplicable(java.lang.StringBuilder description, int source, java.lang.String sourceName)

        if ((mSources & source) == source) {
            description.append(" ");
            description.append(sourceName);
        }
    
public intdescribeContents()

        return 0;
    
public intgetControllerNumber()
The controller number for a given input device.

Each gamepad or joystick is given a unique, positive controller number when initially configured by the system. This number may change due to events such as device disconnects / reconnects or user initiated reassignment. Any change in number will trigger an event that can be observed by registering an {@link InputManager.InputDeviceListener}.

All input devices which are not gamepads or joysticks will be assigned a controller number of 0.

return
The controller number of the device.

        return mControllerNumber;
    
public java.lang.StringgetDescriptor()
Gets the input device descriptor, which is a stable identifier for an input device.

An input device descriptor uniquely identifies an input device. Its value is intended to be persistent across system restarts, and should not change even if the input device is disconnected, reconnected or reconfigured at any time.

It is possible for there to be multiple {@link InputDevice} instances that have the same input device descriptor. This might happen in situations where a single human input device registers multiple {@link InputDevice} instances (HID collections) that describe separate features of the device, such as a keyboard that also has a trackpad. Alternately, it may be that the input devices are simply indistinguishable, such as two keyboards made by the same manufacturer.

The input device descriptor returned by {@link #getDescriptor} should only be used when an application needs to remember settings associated with a particular input device. For all other purposes when referring to a logical {@link InputDevice} instance at runtime use the id returned by {@link #getId()}.

return
The input device descriptor.

        return mDescriptor;
    
public static android.view.InputDevicegetDevice(int id)
Gets information about the input device with the specified id.

param
id The device id.
return
The input device or null if not found.

        return InputManager.getInstance().getInputDevice(id);
    
public static int[]getDeviceIds()
Gets the ids of all input devices in the system.

return
The input device ids.

        return InputManager.getInstance().getInputDeviceIds();
    
public intgetGeneration()
Gets a generation number for this input device. The generation number is incremented whenever the device is reconfigured and its properties may have changed.

return
The generation number.
hide

        return mGeneration;
    
public intgetId()
Gets the input device id.

Each input device receives a unique id when it is first configured by the system. The input device id may change when the system is restarted or if the input device is disconnected, reconnected or reconfigured at any time. If you require a stable identifier for a device that persists across boots and reconfigurations, use {@link #getDescriptor()}.

return
The input device id.

        return mId;
    
public android.hardware.input.InputDeviceIdentifiergetIdentifier()
The set of identifying information for type of input device. This information can be used by the system to configure appropriate settings for the device.

return
The identifier object for this device
hide

        return mIdentifier;
    
public KeyCharacterMapgetKeyCharacterMap()
Gets the key character map associated with this input device.

return
The key character map.

        return mKeyCharacterMap;
    
public intgetKeyboardType()
Gets the keyboard type.

return
The keyboard type.

        return mKeyboardType;
    
public android.view.InputDevice$MotionRangegetMotionRange(int axis)
Gets information about the range of values for a particular {@link MotionEvent} axis. If the device supports multiple sources, the same axis may have different meanings for each source. Returns information about the first axis found for any source. To obtain information about the axis for a specific source, use {@link #getMotionRange(int, int)}.

param
axis The axis constant.
return
The range of values, or null if the requested axis is not supported by the device.
see
MotionEvent#AXIS_X
see
MotionEvent#AXIS_Y

        final int numRanges = mMotionRanges.size();
        for (int i = 0; i < numRanges; i++) {
            final MotionRange range = mMotionRanges.get(i);
            if (range.mAxis == axis) {
                return range;
            }
        }
        return null;
    
public android.view.InputDevice$MotionRangegetMotionRange(int axis, int source)
Gets information about the range of values for a particular {@link MotionEvent} axis used by a particular source on the device. If the device supports multiple sources, the same axis may have different meanings for each source.

param
axis The axis constant.
param
source The source for which to return information.
return
The range of values, or null if the requested axis is not supported by the device.
see
MotionEvent#AXIS_X
see
MotionEvent#AXIS_Y

        final int numRanges = mMotionRanges.size();
        for (int i = 0; i < numRanges; i++) {
            final MotionRange range = mMotionRanges.get(i);
            if (range.mAxis == axis && range.mSource == source) {
                return range;
            }
        }
        return null;
    
public java.util.ListgetMotionRanges()
Gets the ranges for all axes supported by the device.

return
The motion ranges for the device.
see
#getMotionRange(int, int)

        return mMotionRanges;
    
public java.lang.StringgetName()
Gets the name of this input device.

return
The input device name.

        return mName;
    
public intgetProductId()
Gets the product id for the given device, if available.

A product id uniquely identifies which product within the address space of a given vendor, identified by the device's vendor id. A value of 0 will be assigned where a product id is not available.

return
The product id of a given device

        return mProductId;
    
public intgetSources()
Gets the input sources supported by this input device as a combined bitfield.

return
The supported input sources.

        return mSources;
    
public intgetVendorId()
Gets the vendor id for the given device, if available.

A vendor id uniquely identifies the company who manufactured the device. A value of 0 will be assigned where a vendor id is not available.

return
The vendor id of a given device

        return mVendorId;
    
public android.os.VibratorgetVibrator()
Gets the vibrator service associated with the device, if there is one. Even if the device does not have a vibrator, the result is never null. Use {@link Vibrator#hasVibrator} to determine whether a vibrator is present. Note that the vibrator associated with the device may be different from the system vibrator. To obtain an instance of the system vibrator instead, call {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.

return
The vibrator service associated with the device, never null.

        synchronized (mMotionRanges) {
            if (mVibrator == null) {
                if (mHasVibrator) {
                    mVibrator = InputManager.getInstance().getInputDeviceVibrator(mId);
                } else {
                    mVibrator = NullVibrator.getInstance();
                }
            }
            return mVibrator;
        }
    
public booleanhasButtonUnderPad()
Reports whether the device has a button under its touchpad

return
Whether the device has a button under its touchpad
hide

        return mHasButtonUnderPad;
    
public boolean[]hasKeys(int keys)
Gets whether the device is capable of producing the list of keycodes.

param
keys The list of android keycodes to check for.
return
An array of booleans where each member specifies whether the device is capable of generating the keycode given by the corresponding value at the same index in the keys array.

        return InputManager.getInstance().deviceHasKeys(mId, keys);
    
public booleanisExternal()
Returns true if the device is external (connected to USB or Bluetooth or some other peripheral bus), otherwise it is built-in.

return
True if the device is external.
hide

        return mIsExternal;
    
public booleanisFullKeyboard()
Returns true if the device is a full keyboard.

return
True if the device is a full keyboard.
hide

        return (mSources & SOURCE_KEYBOARD) == SOURCE_KEYBOARD
                && mKeyboardType == KEYBOARD_TYPE_ALPHABETIC;
    
public booleanisVirtual()
Returns true if the device is a virtual input device rather than a real one, such as the virtual keyboard (see {@link KeyCharacterMap#VIRTUAL_KEYBOARD}).

Virtual input devices are provided to implement system-level functionality and should not be seen or configured by users.

return
True if the device is virtual.
see
KeyCharacterMap#VIRTUAL_KEYBOARD

        return mId < 0;
    
public booleansupportsSource(int source)
Determines whether the input device supports the given source or sources.

param
source The input source or sources to check against. This can be a generic device type such as {@link InputDevice#SOURCE_MOUSE}, a more generic device class, such as {@link InputDevice#SOURCE_CLASS_POINTER}, or a combination of sources bitwise ORed together.
return
Whether the device can produce all of the given sources.

        return (mSources & source) == source;
    
public java.lang.StringtoString()

        StringBuilder description = new StringBuilder();
        description.append("Input Device ").append(mId).append(": ").append(mName).append("\n");
        description.append("  Descriptor: ").append(mDescriptor).append("\n");
        description.append("  Generation: ").append(mGeneration).append("\n");
        description.append("  Location: ").append(mIsExternal ? "external" : "built-in").append("\n");

        description.append("  Keyboard Type: ");
        switch (mKeyboardType) {
            case KEYBOARD_TYPE_NONE:
                description.append("none");
                break;
            case KEYBOARD_TYPE_NON_ALPHABETIC:
                description.append("non-alphabetic");
                break;
            case KEYBOARD_TYPE_ALPHABETIC:
                description.append("alphabetic");
                break;
        }
        description.append("\n");

        description.append("  Has Vibrator: ").append(mHasVibrator).append("\n");

        description.append("  Sources: 0x").append(Integer.toHexString(mSources)).append(" (");
        appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard");
        appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad");
        appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHSCREEN, "touchscreen");
        appendSourceDescriptionIfApplicable(description, SOURCE_MOUSE, "mouse");
        appendSourceDescriptionIfApplicable(description, SOURCE_STYLUS, "stylus");
        appendSourceDescriptionIfApplicable(description, SOURCE_TRACKBALL, "trackball");
        appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHPAD, "touchpad");
        appendSourceDescriptionIfApplicable(description, SOURCE_JOYSTICK, "joystick");
        appendSourceDescriptionIfApplicable(description, SOURCE_GAMEPAD, "gamepad");
        description.append(" )\n");

        final int numAxes = mMotionRanges.size();
        for (int i = 0; i < numAxes; i++) {
            MotionRange range = mMotionRanges.get(i);
            description.append("    ").append(MotionEvent.axisToString(range.mAxis));
            description.append(": source=0x").append(Integer.toHexString(range.mSource));
            description.append(" min=").append(range.mMin);
            description.append(" max=").append(range.mMax);
            description.append(" flat=").append(range.mFlat);
            description.append(" fuzz=").append(range.mFuzz);
            description.append(" resolution=").append(range.mResolution);
            description.append("\n");
        }
        return description.toString();
    
public voidwriteToParcel(android.os.Parcel out, int flags)

        out.writeInt(mId);
        out.writeInt(mGeneration);
        out.writeInt(mControllerNumber);
        out.writeString(mName);
        out.writeInt(mVendorId);
        out.writeInt(mProductId);
        out.writeString(mDescriptor);
        out.writeInt(mIsExternal ? 1 : 0);
        out.writeInt(mSources);
        out.writeInt(mKeyboardType);
        mKeyCharacterMap.writeToParcel(out, flags);
        out.writeInt(mHasVibrator ? 1 : 0);
        out.writeInt(mHasButtonUnderPad ? 1 : 0);

        final int numRanges = mMotionRanges.size();
        for (int i = 0; i < numRanges; i++) {
            MotionRange range = mMotionRanges.get(i);
            out.writeInt(range.mAxis);
            out.writeInt(range.mSource);
            out.writeFloat(range.mMin);
            out.writeFloat(range.mMax);
            out.writeFloat(range.mFlat);
            out.writeFloat(range.mFuzz);
            out.writeFloat(range.mResolution);
        }
        out.writeInt(-1);