Twaik Yont
Twaik Yont

Reputation: 100

How to distinguish mouse events from touchscreen events?

In my application I need to know which input device produced touch event: mouse, touchscreen, touchpad or something else.

event.getSource() returns:

for mouse: 8194
for touchscreen: 4098

I've made a method which outputs to logcat types of source:

void dumpSource(MotionEvent e) {
    int s = e.getSource();
    Log.e("LorieService", "Motion event is from sources: " +
            ((s&InputDevice.SOURCE_KEYBOARD)!=0?"keyboard ":"") +
            ((s&InputDevice.SOURCE_DPAD)!=0?"dpad ":"") +
            ((s&InputDevice.SOURCE_GAMEPAD)!=0?"gamepad ":"") +
            ((s&InputDevice.SOURCE_TOUCHSCREEN)!=0?"touchscreen ":"") +
            ((s&InputDevice.SOURCE_MOUSE)!=0?"mouse ":"") +
            ((s&InputDevice.SOURCE_STYLUS)!=0?"stylus ":"") +
            ((s&InputDevice.SOURCE_BLUETOOTH_STYLUS)!=0?"bt_stylus ":"") +
            ((s&InputDevice.SOURCE_TRACKBALL)!=0?"trackball ":"") +
            ((s&InputDevice.SOURCE_MOUSE_RELATIVE)!=0?"mouse_relative ":"") +
            ((s&InputDevice.SOURCE_TOUCHPAD)!=0?"touchpad ":"") +
            ((s&InputDevice.SOURCE_TOUCH_NAVIGATION)!=0?"touch_navigation ":"") +
            ((s&InputDevice.SOURCE_ROTARY_ENCODER)!=0?"rotary_encoder ":"") +
            ((s&InputDevice.SOURCE_JOYSTICK)!=0?"joystick ":"") +
            ((s&InputDevice.SOURCE_HDMI)!=0?"hdmi":"")

    );
}

But it outputs touchscreen mouse stylus bt_stylus for both mouse and touchscreen.

How to distinguish mouse and touchscreen events in proper way?

Upvotes: 1

Views: 807

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93569

This isn't how to check it. The correct way to check it would be type = s&InputDevice.SOURCE_MASK; then check equality matching on type. The way you're doing it will return true if any of the bits of the source are the same for the two device types. The type itself is not a bitmask, its an integer enumeration.

Upvotes: 2

Related Questions