Java Code Examples for android.view.MotionEvent#getButtonState()

The following examples show how to use android.view.MotionEvent#getButtonState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbsHListView.java    From letv with Apache License 2.0 5 votes vote down vote up
@TargetApi(14)
protected boolean performButtonActionOnTouchDown(MotionEvent event) {
    if (VERSION.SDK_INT < 14 || (event.getButtonState() & 2) == 0 || !showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
        return false;
    }
    return true;
}
 
Example 2
Source File: LayerRowCallbacks.java    From spline with Apache License 2.0 5 votes vote down vote up
public boolean onRowTouch(View view, MotionEvent event, LayerRowViewModel row) {
    if (row != null) {
        currentTouchLayer = row.getLayer();
    }
    currentTouchView = view;
    detector.onTouchEvent(event);

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        currentTouchIsSecondaryButton = event.getButtonState() == MotionEvent.BUTTON_SECONDARY;
    }

    return true;
}
 
Example 3
Source File: MotionEventUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ボタンの押し下げ状態を文字列として返す
 * @param event
 * @return
 */
public static String getButtonStateString(@NonNull final MotionEvent event) {
	int buttonState = event.getButtonState();
	if (buttonState == 0) {
		return "[]";
	}
	final StringBuilder result = new StringBuilder("[");
	int i = 0;
	while (buttonState != 0) {
		final boolean isSet = (buttonState & 1) != 0;
		buttonState >>>= 1; // unsigned shift!
		if (isSet) {
			final String name = BUTTON_SYMBOLIC_NAMES[i];
			if (result.length() == 1) {
				result.append(name);
				if (buttonState == 0) {
					break;
				}
			} else {
				result.append('|');
				result.append(name);
			}
		}
		i += 1;
	}
	result.append("]");
	return result.toString();
}
 
Example 4
Source File: AbsHListView.java    From Klyph with MIT License 5 votes vote down vote up
/**
 * Performs button-related actions during a touch down event.
 * 
 * @param event
 *           The event.
 * @return True if the down was consumed.
 * 
 */
@TargetApi(14)
protected boolean performButtonActionOnTouchDown( MotionEvent event ) {
	if ( android.os.Build.VERSION.SDK_INT >= 14 ) {
		if ( ( event.getButtonState() & MotionEvent.BUTTON_SECONDARY ) != 0 ) {
			if ( showContextMenu( event.getX(), event.getY(), event.getMetaState() ) ) {
				return true;
			}
		}
	}
	return false;
}
 
Example 5
Source File: DragInputEventReceiver.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void onInputEvent(InputEvent event, int displayId) {
    boolean handled = false;
    try {
        if (!(event instanceof MotionEvent)
                || (event.getSource() & SOURCE_CLASS_POINTER) == 0
                || mMuteInput) {
            return;
        }
        final MotionEvent motionEvent = (MotionEvent) event;
        final float newX = motionEvent.getRawX();
        final float newY = motionEvent.getRawY();
        final boolean isStylusButtonDown =
                (motionEvent.getButtonState() & BUTTON_STYLUS_PRIMARY) != 0;

        if (mIsStartEvent) {
            // First event and the button was down, check for the button being
            // lifted in the future, if that happens we'll drop the item.
            mStylusButtonDownAtStart = isStylusButtonDown;
            mIsStartEvent = false;
        }

        switch (motionEvent.getAction()) {
            case ACTION_DOWN:
                if (DEBUG_DRAG) Slog.w(TAG_WM, "Unexpected ACTION_DOWN in drag layer");
                return;
            case ACTION_MOVE:
                if (mStylusButtonDownAtStart && !isStylusButtonDown) {
                    if (DEBUG_DRAG) {
                        Slog.d(TAG_WM, "Button no longer pressed; dropping at " + newX + ","
                                + newY);
                    }
                    mMuteInput = true;
                }
                break;
            case ACTION_UP:
                if (DEBUG_DRAG) {
                    Slog.d(TAG_WM, "Got UP on move channel; dropping at " + newX + "," + newY);
                }
                mMuteInput = true;
                break;
            case ACTION_CANCEL:
                if (DEBUG_DRAG) Slog.d(TAG_WM, "Drag cancelled!");
                mMuteInput = true;
                break;
            default:
                return;
        }

        mDragDropController.handleMotionEvent(!mMuteInput /* keepHandling */, newX, newY);
        handled = true;
    } catch (Exception e) {
        Slog.e(TAG_WM, "Exception caught by drag handleMotion", e);
    } finally {
        finishInputEvent(event, handled);
    }
}
 
Example 6
Source File: MotionEvents.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isButtonPressed(MotionEvent e, int button) {
    if (button == 0) {
        return false;
    }
    return (e.getButtonState() & button) == button;
}
 
Example 7
Source File: MotionEvents.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static boolean isTouchpadScroll(@NonNull MotionEvent e) {
    // Touchpad inputs are treated as mouse inputs, and when scrolling, there are no buttons
    // returned.
    return isMouseEvent(e) && isActionMove(e) && e.getButtonState() == 0;
}
 
Example 8
Source File: TouchPaint.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
private boolean onTouchOrHoverEvent(MotionEvent event, boolean isTouch) {
    final int buttonState = event.getButtonState();
    int pressedButtons = buttonState & ~mOldButtonState;
    mOldButtonState = buttonState;

    if ((pressedButtons & MotionEvent.BUTTON_SECONDARY) != 0) {
        // Advance color when the right mouse button or first stylus button
        // is pressed.
        advanceColor();
    }

    PaintMode mode;
    if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0) {
        // Splat paint when the middle mouse button or second stylus button is pressed.
        mode = PaintMode.Splat;
    } else if (isTouch || (buttonState & MotionEvent.BUTTON_PRIMARY) != 0) {
        // Draw paint when touching or if the primary button is pressed.
        mode = PaintMode.Draw;
    } else {
        // Otherwise, do not paint anything.
        return false;
    }

    final int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE
            || action == MotionEvent.ACTION_HOVER_MOVE) {
        final int N = event.getHistorySize();
        final int P = event.getPointerCount();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < P; j++) {
                paint(getPaintModeForTool(event.getToolType(j), mode),
                        event.getHistoricalX(j, i),
                        event.getHistoricalY(j, i),
                        event.getHistoricalPressure(j, i),
                        event.getHistoricalTouchMajor(j, i),
                        event.getHistoricalTouchMinor(j, i),
                        event.getHistoricalOrientation(j, i),
                        event.getHistoricalAxisValue(MotionEvent.AXIS_DISTANCE, j, i),
                        event.getHistoricalAxisValue(MotionEvent.AXIS_TILT, j, i));
            }
        }
        for (int j = 0; j < P; j++) {
            paint(getPaintModeForTool(event.getToolType(j), mode),
                    event.getX(j),
                    event.getY(j),
                    event.getPressure(j),
                    event.getTouchMajor(j),
                    event.getTouchMinor(j),
                    event.getOrientation(j),
                    event.getAxisValue(MotionEvent.AXIS_DISTANCE, j),
                    event.getAxisValue(MotionEvent.AXIS_TILT, j));
        }
        mCurX = event.getX();
        mCurY = event.getY();
    }
    return true;
}
 
Example 9
Source File: StylusEventHelper.java    From LaunchEnr with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button
 * pressed.
 *
 * @param event The event to check.
 * @return Whether a stylus button press occurred.
 */
private static boolean isStylusButtonPressed(MotionEvent event) {
    return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS
            && ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY)
                    == MotionEvent.BUTTON_SECONDARY);
}
 
Example 10
Source File: StylusEventHelper.java    From Trebuchet with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button
 * pressed.
 *
 * @param event The event to check.
 * @return Whether a stylus button press occurred.
 */
private static boolean isStylusButtonPressed(MotionEvent event) {
    return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS
            && ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY)
                    == MotionEvent.BUTTON_SECONDARY);
}