Example usage for android.view KeyEvent getKeyCode

List of usage examples for android.view KeyEvent getKeyCode

Introduction

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

Prototype

public final int getKeyCode() 

Source Link

Document

Retrieve the key code of the key event.

Usage

From source file:com.android.launcher3.allapps.FullMergeAlgorithm.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // Determine if the key event was actual text, if so, focus the search bar and then dispatch
    // the key normally so that it can process this key event
    if (!mSearchBarController.isSearchFieldFocused() && event.getAction() == KeyEvent.ACTION_DOWN) {
        final int unicodeChar = event.getUnicodeChar();
        final boolean isKeyNotWhitespace = unicodeChar > 0 && !Character.isWhitespace(unicodeChar)
                && !Character.isSpaceChar(unicodeChar);
        if (isKeyNotWhitespace) {
            boolean gotKey = TextKeyListener.getInstance().onKeyDown(this, mSearchQueryBuilder,
                    event.getKeyCode(), event);
            if (gotKey && mSearchQueryBuilder.length() > 0) {
                mSearchBarController.focusSearchField();
            }/*from w w w. jav a  2  s. c om*/
        }
    }

    return super.dispatchKeyEvent(event);
}

From source file:com.actionbarsherlock.internal.ActionBarSherlockCompat.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (DEBUG)/*  w w  w . j a  v  a 2s. c  om*/
        Log.d(TAG, "[dispatchKeyEvent] event: " + event);

    final int keyCode = event.getKeyCode();

    // Not handled by the view hierarchy, does the action bar want it
    // to cancel out of something special?
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        final int action = event.getAction();
        // Back cancels action modes first.
        if (mActionMode != null) {
            if (action == KeyEvent.ACTION_UP) {
                mActionMode.finish();
            }
            if (DEBUG)
                Log.d(TAG, "[dispatchKeyEvent] returning true");
            return true;
        }

        // Next collapse any expanded action views.
        if (wActionBar != null && wActionBar.hasExpandedActionView()) {
            if (action == KeyEvent.ACTION_UP) {
                wActionBar.collapseActionView();
            }
            if (DEBUG)
                Log.d(TAG, "[dispatchKeyEvent] returning true");
            return true;
        }
    }

    boolean result = false;
    if (keyCode == KeyEvent.KEYCODE_MENU && isReservingOverflow()) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.isLongPress()) {
            mMenuKeyIsLongPress = true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            if (!mMenuKeyIsLongPress) {
                if (mActionMode == null && wActionBar != null) {
                    if (wActionBar.isOverflowMenuShowing()) {
                        wActionBar.hideOverflowMenu();
                    } else {
                        wActionBar.showOverflowMenu();
                    }
                }
                result = true;
            }
            mMenuKeyIsLongPress = false;
        }
    }

    if (DEBUG)
        Log.d(TAG, "[dispatchKeyEvent] returning " + result);
    return result;
}

From source file:org.jitsi.android.gui.call.VideoCallActivity.java

/**
 * {@inheritDoc}//from   w  w  w .  j av a2 s. c  o m
 */
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    /**
     * The call to:
     * setVolumeControlStream(AudioManager.STREAM_VOICE_CALL)
     * doesn't work when notification was being played during this Activity
     * creation, so the buttons must be captured and the voice call level
     * will be manipulated programmatically.
     */
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_UP) {
            volControl.onKeyVolUp();
        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            volControl.onKeyVolDown();
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

From source file:com.android.talkback.SpeechController.java

/**
 * Stops the TTS engine when the Ctrl key is tapped without any other keys.
 *///from w  w w  . j a v a 2s. c  o  m
@Override
public boolean onKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        mInterruptKeyDown = (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_CTRL_RIGHT);
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        if (mInterruptKeyDown
                && (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_CTRL_RIGHT)) {
            mInterruptKeyDown = false;
            mService.interruptAllFeedback();
        }
    }

    return false;
}

From source file:android.car.ui.provider.CarDrawerLayout.java

@Override
public boolean dispatchKeyEvent(@NonNull KeyEvent keyEvent) {
    int action = keyEvent.getAction();
    int keyCode = keyEvent.getKeyCode();
    final View drawerView = findDrawerView();
    if (drawerView != null && getDrawerLockMode(drawerView) == LOCK_MODE_UNLOCKED) {
        if (isDrawerOpen()) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_SOFT_RIGHT) {
                closeDrawer();//from ww  w.j av  a  2s.  co  m
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_BACK && action == KeyEvent.ACTION_UP
                    && mDrawerControllerListener != null) {
                mDrawerControllerListener.onBack();
                return true;
            } else {
                return drawerView.dispatchKeyEvent(keyEvent);
            }
        }
    }

    return mContentView.dispatchKeyEvent(keyEvent);
}

From source file:uk.co.pjmobile.mobile_apps.page_turner_reader.ReadingActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();

    if (isAnimating() && action == KeyEvent.ACTION_DOWN) {
        stopAnimating();//from   w  ww .  j a  v  a  2 s.co  m
        return true;
    }

    switch (keyCode) {

    case KeyEvent.KEYCODE_VOLUME_DOWN:
        // Yes, this is nasty: if the setting is true, we fall through to
        // the next case.
        if (!config.isVolumeKeyNavEnabled()) {
            return false;
        }

    case KeyEvent.KEYCODE_DPAD_RIGHT:

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

        return true;

    case KeyEvent.KEYCODE_VOLUME_UP:
        // Same dirty trick.
        if (!config.isVolumeKeyNavEnabled()) {
            return false;
        }

    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();
            } else if (bookView.hasPrevPosition()) {
                bookView.goBackInHistory();

                return true;
            } else {
                this.finish();
            }
        }

    }

    return false;
}

From source file:de.vanita5.twittnuker.activity.support.ComposeActivity.java

@Override
public boolean onEditorAction(final TextView view, final int actionId, final KeyEvent event) {
    if (event == null)
        return false;
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_ENTER: {
        updateStatus();//from   w  w w . j  a  v a 2s  .c o m
        return true;
    }
    }
    return false;
}

From source file:com.ferdi2005.secondgram.voip.VoIPService.java

void onMediaButtonEvent(KeyEvent ev) {
    if (ev.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) {
        if (ev.getAction() == KeyEvent.ACTION_UP) {
            if (currentState == STATE_WAITING_INCOMING) {
                acceptIncomingCall();//from   www  . jav a  2s  . co  m
            } else {
                setMicMute(!isMicMute());
                for (StateListener l : stateListeners)
                    l.onAudioSettingsChanged();
            }
        }
    }
}

From source file:org.appcelerator.titanium.TiBaseActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    boolean handled = false;

    TiViewProxy window;/*from w w  w  .  j  a  va  2  s .co  m*/
    if (this.window != null) {
        window = this.window;
    } else {
        window = this.view;
    }

    if (window == null) {
        return super.dispatchKeyEvent(event);
    }

    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_BACK: {

        if (event.getAction() == KeyEvent.ACTION_UP) {
            String backEvent = "android:back";
            KrollProxy proxy = null;
            //android:back could be fired from a tabGroup window (activityProxy)
            //or hw window (window).This event is added specifically to the activity
            //proxy of a tab group in window.js
            if (activityProxy.hasListeners(backEvent)) {
                proxy = activityProxy;
            } else if (window.hasListeners(backEvent)) {
                proxy = window;
            }

            if (proxy != null) {
                proxy.fireEvent(backEvent, null);
                handled = true;
            }

        }
        break;
    }
    case KeyEvent.KEYCODE_CAMERA: {
        if (window.hasListeners(TiC.EVENT_ANDROID_CAMERA)) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent(TiC.EVENT_ANDROID_CAMERA, null);
            }
            handled = true;
        }
        // TODO: Deprecate old event
        if (window.hasListeners("android:camera")) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent("android:camera", null);
            }
            handled = true;
        }

        break;
    }
    case KeyEvent.KEYCODE_FOCUS: {
        if (window.hasListeners(TiC.EVENT_ANDROID_FOCUS)) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent(TiC.EVENT_ANDROID_FOCUS, null);
            }
            handled = true;
        }
        // TODO: Deprecate old event
        if (window.hasListeners("android:focus")) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent("android:focus", null);
            }
            handled = true;
        }

        break;
    }
    case KeyEvent.KEYCODE_SEARCH: {
        if (window.hasListeners(TiC.EVENT_ANDROID_SEARCH)) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent(TiC.EVENT_ANDROID_SEARCH, null);
            }
            handled = true;
        }
        // TODO: Deprecate old event
        if (window.hasListeners("android:search")) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent("android:search", null);
            }
            handled = true;
        }

        break;
    }
    case KeyEvent.KEYCODE_VOLUME_UP: {
        if (window.hasListeners(TiC.EVENT_ANDROID_VOLUP)) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent(TiC.EVENT_ANDROID_VOLUP, null);
            }
            handled = true;
        }
        // TODO: Deprecate old event
        if (window.hasListeners("android:volup")) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent("android:volup", null);
            }
            handled = true;
        }

        break;
    }
    case KeyEvent.KEYCODE_VOLUME_DOWN: {
        if (window.hasListeners(TiC.EVENT_ANDROID_VOLDOWN)) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent(TiC.EVENT_ANDROID_VOLDOWN, null);
            }
            handled = true;
        }
        // TODO: Deprecate old event
        if (window.hasListeners("android:voldown")) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                window.fireEvent("android:voldown", null);
            }
            handled = true;
        }

        break;
    }
    }

    if (!handled) {
        handled = super.dispatchKeyEvent(event);
    }

    return handled;
}

From source file:es.farfuteam.vncpp.controller.CanvasActivity.java

/**
 * @param event The event/* ww  w .ja v  a 2s.  c om*/
 * @brief Captures the keys of the keyboard
 * @details Capture the keys of the keyboard. If the character has to be pressed with a combination of the shift key
 * and other key then adds 100 to the modKeyCount attribute to applies the offset
 */
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN || event.getAction() == KeyEvent.ACTION_UP
            || event.getAction() == KeyEvent.ACTION_MULTIPLE) {
        int keyunicode = event.getUnicodeChar(event.getMetaState());
        char character = (char) keyunicode;
        int key = event.getKeyCode();
        if (key == 59) {
            if (modKeyCount == 0) {
                modKeyCount = 100;
            } else {
                modKeyCount = 0;
            }
        } else if (key >= 7) {
            boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
            vnc.sendKey(modKeyCount + key, down);

        }

        Log.e(DEBUG_TAG, String.valueOf(event.getKeyCode()));
    }
    return super.dispatchKeyEvent(event);
}