Example usage for android.view KeyEvent ACTION_DOWN

List of usage examples for android.view KeyEvent ACTION_DOWN

Introduction

In this page you can find the example usage for android.view KeyEvent ACTION_DOWN.

Prototype

int ACTION_DOWN

To view the source code for android.view KeyEvent ACTION_DOWN.

Click Source Link

Document

#getAction value: the key has been pressed down.

Usage

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * Filter pre-IME key events. By forwarding {@link View#onKeyPreIme(int, KeyEvent)}
 * events to this function, views using ListPopupWindow can have it dismiss the popup
 * when the back key is pressed.//from   ww  w  .  j ava 2 s  .co  m
 *
 * @param keyCode keyCode param passed to the host view's onKeyPreIme
 * @param event event param passed to the host view's onKeyPreIme
 * @return true if the event was handled, false if it was ignored.
 * @see #setModal(boolean)
 */
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && isShowing()) {
        // special case for the back key, we do not even try to send it
        // to the drop down list but instead, consume it immediately
        final View anchorView = mDropDownAnchorView;
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            KeyEvent.DispatcherState state = anchorView.getKeyDispatcherState();
            if (state != null) {
                state.startTracking(event, this);
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            KeyEvent.DispatcherState state = anchorView.getKeyDispatcherState();
            if (state != null) {
                state.handleUpEvent(event);
            }
            if (event.isTracking() && !event.isCanceled()) {
                dismiss();
                return true;
            }
        }
    }
    return false;
}

From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java

public boolean dispatchKeyEvent(KeyEvent event) {

    int action = event.getAction();
    int keyCode = event.getKeyCode();

    LOG.debug("Got key event: " + keyCode + " with action " + action);

    if (searchMenuItem != null && searchMenuItem.isActionViewExpanded()) {
        boolean result = searchMenuItem.getActionView().dispatchKeyEvent(event);

        if (result) {
            return true;
        }/*from   w  ww .jav a2 s  .c  om*/
    }

    final int KEYCODE_NOOK_TOUCH_BUTTON_LEFT_TOP = 92;
    final int KEYCODE_NOOK_TOUCH_BUTTON_LEFT_BOTTOM = 93;
    final int KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_TOP = 94;
    final int KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_BOTTOM = 95;

    boolean nook_touch_up_press = false;

    if (isAnimating() && action == KeyEvent.ACTION_DOWN) {
        stopAnimating();
        return true;
    }

    /*
     * Tricky bit of code here: if we are NOT running TTS,
     * we want to be able to start it using the play/pause button.
     *
     * When we ARE running TTS, we'll get every media event twice:
     * once through the receiver and once here if focused.
     *
     * So, we only try to read media events here if tts is running.
     */
    if (!ttsIsRunning() && dispatchMediaKeyEvent(event)) {
        return true;
    }

    LOG.debug("Key event is NOT a media key event.");

    switch (keyCode) {

    case KeyEvent.KEYCODE_VOLUME_DOWN:
    case KeyEvent.KEYCODE_VOLUME_UP:
        return handleVolumeButtonEvent(event);

    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (action == KeyEvent.ACTION_DOWN) {
            pageDown(Orientation.HORIZONTAL);
        }

        return true;

    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (action == KeyEvent.ACTION_DOWN) {
            pageUp(Orientation.HORIZONTAL);
        }

        return true;

    case KeyEvent.KEYCODE_BACK:
        if (action == KeyEvent.ACTION_DOWN) {

            if (titleBarLayout.getVisibility() == View.VISIBLE) {
                hideTitleBar();
                updateFromPrefs();
                return true;
            } else if (bookView.hasPrevPosition()) {
                bookView.goBackInHistory();
                return true;
            }
        }

        return false;

    case KEYCODE_NOOK_TOUCH_BUTTON_LEFT_TOP:
    case KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_TOP:
        nook_touch_up_press = true;
    case KEYCODE_NOOK_TOUCH_BUTTON_LEFT_BOTTOM:
    case KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_BOTTOM:
        if (action == KeyEvent.ACTION_UP)
            return false;
        if (nook_touch_up_press == config.isNookUpButtonForward())
            pageDown(Orientation.HORIZONTAL);
        else
            pageUp(Orientation.HORIZONTAL);
        return true;
    }

    LOG.debug("Not handling key event: returning false.");
    return false;
}

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

public boolean dispatchKeyEvent(KeyEvent event) {

    int action = event.getAction();
    int keyCode = event.getKeyCode();

    LOG.debug("Got key event: " + keyCode + " with action " + action);

    if (searchMenuItem != null && searchMenuItem.isActionViewExpanded()) {
        boolean result = searchMenuItem.getActionView().dispatchKeyEvent(event);

        if (result) {
            return true;
        }/*  w  w w  . ja  v  a 2s . c o  m*/
    }

    final int KEYCODE_NOOK_TOUCH_BUTTON_LEFT_TOP = 92;
    final int KEYCODE_NOOK_TOUCH_BUTTON_LEFT_BOTTOM = 93;
    final int KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_TOP = 94;
    final int KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_BOTTOM = 95;

    boolean nook_touch_up_press = false;

    if (isAnimating() && action == KeyEvent.ACTION_DOWN) {
        stopAnimating();
        return true;
    }

    /*
     * Tricky bit of code here: if we are NOT running TTS,
     * we want to be able to start it using the play/pause button.
     * 
     * When we ARE running TTS, we'll get every media event twice:
     * once through the receiver and once here if focused. 
     * 
     * So, we only try to read media events here if tts is running.
     */
    if (!ttsIsRunning() && dispatchMediaKeyEvent(event)) {
        return true;
    }

    LOG.debug("Key event is NOT a media key event.");

    switch (keyCode) {

    case KeyEvent.KEYCODE_VOLUME_DOWN:
    case KeyEvent.KEYCODE_VOLUME_UP:
        return handleVolumeButtonEvent(event);

    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (action == KeyEvent.ACTION_DOWN) {
            pageDown(Orientation.HORIZONTAL);
        }

        return true;

    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (action == KeyEvent.ACTION_DOWN) {
            pageUp(Orientation.HORIZONTAL);
        }

        return true;

    case KeyEvent.KEYCODE_BACK:
        if (action == KeyEvent.ACTION_DOWN) {

            if (titleBarLayout.getVisibility() == View.VISIBLE) {
                hideTitleBar();
                updateFromPrefs();
                return true;
            } else if (bookView.hasPrevPosition()) {
                bookView.goBackInHistory();
                return true;
            }
        }

        return false;

    case KEYCODE_NOOK_TOUCH_BUTTON_LEFT_TOP:
    case KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_TOP:
        nook_touch_up_press = true;
    case KEYCODE_NOOK_TOUCH_BUTTON_LEFT_BOTTOM:
    case KEYCODE_NOOK_TOUCH_BUTTON_RIGHT_BOTTOM:
        if (!Configuration.IS_NOOK_TOUCH || action == KeyEvent.ACTION_UP)
            return false;
        if (nook_touch_up_press == config.isNookUpButtonForward())
            pageDown(Orientation.HORIZONTAL);
        else
            pageUp(Orientation.HORIZONTAL);
        return true;
    }

    LOG.debug("Not handling key event: returning false.");
    return false;
}

From source file:com.devwang.logcabin.LogCabinMainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //    //w w  w . j  a  v  a  2  s  .  c om
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

        if ((System.currentTimeMillis() - exitTime) > 2000) // System.currentTimeMillis()2000
        {
            Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            if (mChatService != null)
                mChatService.stop();
            finish();
            System.exit(0);
        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

@TargetApi(Build.VERSION_CODES.FROYO)
private boolean handleVolumeButtonEvent(KeyEvent event) {

    // Disable volume button handling during TTS
    if (!config.isVolumeKeyNavEnabled() || ttsIsRunning()) {
        return false;
    }//from   w  w w .  ja  v  a  2s  . com

    Activity activity = getActivity();

    if (activity == null) {
        return false;
    }

    boolean invert = false;

    int rotation = Surface.ROTATION_0;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        rotation = display.getRotation();
    }

    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_90:
        invert = false;
        break;
    case Surface.ROTATION_180:
    case Surface.ROTATION_270:
        invert = true;
        break;
    }

    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return true;
    }

    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
        if (invert) {
            pageDown(Orientation.HORIZONTAL);
        } else {
            pageUp(Orientation.HORIZONTAL);
        }
    } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if (invert) {
            pageUp(Orientation.HORIZONTAL);
        } else {
            pageDown(Orientation.HORIZONTAL);
        }
    }

    return true;
}

From source file:net.zorgblub.typhon.fragment.ReadingFragment.java

@TargetApi(Build.VERSION_CODES.FROYO)
private boolean handleVolumeButtonEvent(KeyEvent event) {

    //Disable volume button handling during TTS
    if (!config.isVolumeKeyNavEnabled() || ttsIsRunning()) {
        return false;
    }//from w ww  . j  a va2  s .  c  o  m

    Activity activity = getActivity();

    if (activity == null) {
        return false;
    }

    boolean invert = false;

    int rotation = Surface.ROTATION_0;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        rotation = display.getRotation();
    }

    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_90:
        invert = false;
        break;
    case Surface.ROTATION_180:
    case Surface.ROTATION_270:
        invert = true;
        break;
    }

    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return true;
    }

    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
        if (config.isVolumeKeyNavChaptersEnabled()) {
            if (invert) {
                bookView.navigateForward();
            } else {
                bookView.navigateBack();
            }
        } else {
            if (invert) {
                pageDown(Orientation.HORIZONTAL);
            } else {
                pageUp(Orientation.HORIZONTAL);
            }
        }
    } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if (config.isVolumeKeyNavChaptersEnabled()) {
            if (invert) {
                bookView.navigateBack();
            } else {
                bookView.navigateForward();
            }
        } else {
            if (invert) {
                pageUp(Orientation.HORIZONTAL);
            } else {
                pageDown(Orientation.HORIZONTAL);
            }
        }
    }

    return true;
}

From source file:bw.com.yunifangstore.view.LazyViewPager.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy./*from  ww  w  . jav  a2s. c  o  m*/
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            handled = arrowScroll(FOCUS_LEFT);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            handled = arrowScroll(FOCUS_RIGHT);
            break;
        case KeyEvent.KEYCODE_TAB:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = arrowScroll(FOCUS_FORWARD);
            } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                handled = arrowScroll(FOCUS_BACKWARD);
            }
            break;
        }
    }
    return handled;
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

private boolean simulateButtonPress(int action, int idToSend, ImageButton buttonToClick) {
    if (action == KeyEvent.ACTION_DOWN) {
        onMediaButtonEvent(idToSend);//from www  .j a v  a  2s  .  co m
        buttonToClick.setPressed(true);
    } else {
        buttonToClick.setPressed(false);
    }

    buttonToClick.invalidate();
    return true;
}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new BaseInputConnection(this, false) {
        @Override/*from w w w .  j  a  v a 2 s. c  o m*/
        public boolean sendKeyEvent(KeyEvent event) {
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
            if (beforeLength == 1 && afterLength == 0) {
                // backspace
                super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
            }

            return super.deleteSurroundingText(beforeLength, afterLength);
        }
    };
}

From source file:com.android.tv.MainActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (SystemProperties.LOG_KEYEVENT.getValue())
        Log.d(TAG, "dispatchKeyEvent(" + event + ")");
    // If an activity is closed on a back key down event, back key down events with none zero
    // repeat count or a back key up event can be happened without the first back key down
    // event which should be ignored in this activity.
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            mBackKeyPressed = true;/*  www.ja v a 2  s. c  om*/
        }
        if (!mBackKeyPressed) {
            return true;
        }
        if (event.getAction() == KeyEvent.ACTION_UP) {
            mBackKeyPressed = false;
        }
    }

    // When side panel is closing, it has the focus.
    // Keep the focus, but just don't deliver the key events.
    if ((mOverlayRootView.hasFocusable() && !mOverlayManager.getSideFragmentManager().isHiding())
            || mOverlayManager.getSideFragmentManager().isActive()) {
        return super.dispatchKeyEvent(event);
    }
    if (BLACKLIST_KEYCODE_TO_TIS.contains(event.getKeyCode()) || KeyEvent.isGamepadButton(event.getKeyCode())) {
        // If the event is in blacklisted or gamepad key, do not pass it to session.
        // Gamepad keys are blacklisted to support TV UIs and here's the detail.
        // If there's a TIS granted RECEIVE_INPUT_EVENT, TIF sends key events to TIS
        // and return immediately saying that the event is handled.
        // In this case, fallback key will be injected but with FLAG_CANCELED
        // while gamepads support DPAD_CENTER and BACK by fallback.
        // Since we don't expect that TIS want to handle gamepad buttons now,
        // blacklist gamepad buttons and wait for next fallback keys.
        // TODO) Need to consider other fallback keys (e.g. ESCAPE)
        return super.dispatchKeyEvent(event);
    }
    return dispatchKeyEventToSession(event) || super.dispatchKeyEvent(event);
}