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.plusot.senselib.SenseMain.java

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

    if ((lastAction == action && lastKeyCode == keyCode)) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_CAMERA:
            newInterval();//from  ww  w.j  a  v  a 2s .  c om
            break;
        case KeyEvent.KEYCODE_POWER:
            //if (action == KeyEvent.)
            LLog.d(Globals.TAG, CLASSTAG + " Power button pressed");
            break;
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                dimScreen(true);
            }
            result = true;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                dimScreen(false);
            }
            result = true;
            break;
        }
    } else {
        AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        switch (keyCode) {
        case KeyEvent.KEYCODE_CAMERA:
            newInterval();
            break;
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (sameActionKeyCode < 2 && lastAction == KeyEvent.ACTION_DOWN && action == KeyEvent.ACTION_UP
                    && lastKeyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                mgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
                        AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
            }
            result = true;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (sameActionKeyCode < 2 && lastAction == KeyEvent.ACTION_DOWN && action == KeyEvent.ACTION_UP
                    && lastKeyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                mgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
                        AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
            }
            result = true;
            break;
        }
    }
    if (!result) {
        // LLog.d(Globals.TAG, CLASSTAG +
        // ".distpatchKeyEvent: Calling super with " + delta);
        result = super.dispatchKeyEvent(event);
    }
    if (lastAction == action && lastKeyCode == keyCode) {
        sameActionKeyCode++;
    } else
        sameActionKeyCode = 0;

    lastKeyCode = keyCode;
    lastAction = action;
    return result;

}

From source file:com.lambergar.verticalviewpager.VerticalViewPager.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  w  ww.j  a  v a  2 s .c om*/
 *
 * @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_UP:
            handled = arrowScroll(FOCUS_UP);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            handled = arrowScroll(FOCUS_RIGHT);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            handled = arrowScroll(FOCUS_DOWN);
            break;
        case KeyEvent.KEYCODE_TAB:
            if (KeyEvent.metaStateHasNoModifiers(event.getMetaState())) {
                handled = arrowScroll(FOCUS_FORWARD);
            } else if (KeyEvent.metaStateHasModifiers(event.getMetaState(), KeyEvent.META_SHIFT_ON)) {
                handled = arrowScroll(FOCUS_BACKWARD);
            }
            break;
        }
    }
    return handled;
}

From source file:VerticalViewPager.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 w w  w.  j  a  va  2 s  .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_UP:
            handled = arrowScroll(FOCUS_UP);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            handled = arrowScroll(FOCUS_RIGHT);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            handled = arrowScroll(FOCUS_DOWN);
            break;
        case KeyEvent.KEYCODE_TAB:
            if (Build.VERSION.SDK_INT >= 11) {
                // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                // before Android 3.0. Ignore the tab key on those devices.
                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.nuwa.widgets.LoopViewPager.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  w  w w  .  ja  va2 s .  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 (Build.VERSION.SDK_INT >= 11) {
                // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                // before Android 3.0. Ignore the tab key on those devices.
                if (event.hasNoModifiers()) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
            }
            break;
        }
    }
    return handled;
}

From source file:com.suning.boxcontroller.control.ExViewPager.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.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 *//*from   w w  w  .j  av  a 2s.c o m*/
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 (Build.VERSION.SDK_INT >= 11) {
                // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                // before Android 3.0. Ignore the tab key on those devices.
                if (KeyEventCompat.hasNoModifiers(event)) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
            }
            break;

        default:
            break;
        }
    }
    return handled;
}

From source file:eu.kanade.tachiyomi.ui.reader.viewer.pager.vertical.VerticalViewPagerImpl.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  www . j a  va  2  s.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 (Build.VERSION.SDK_INT >= 11) {
                // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                // before Android 3.0. Ignore the tab key on those devices.
                if (KeyEventCompat.hasNoModifiers(event)) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
            }
            break;
        default:
            break;
        }
    }
    return handled;
}

From source file:com.koma.music.service.MusicService.java

private void setUpMediaSession() {
    mSession = new MediaSession(this, "KomaMusic");
    mSession.setCallback(new MediaSession.Callback() {
        @Override//  ww  w .j  a v  a 2s.co  m
        public void onPause() {
            pause();
            mPausedByTransientLossOfFocus = false;
        }

        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onSeekTo(long pos) {
            seek(pos);
        }

        @Override
        public void onSkipToNext() {
            gotoNext(true);
        }

        @Override
        public void onSkipToPrevious() {
            prev(false);
        }

        @Override
        public void onStop() {
            pause();
            mPausedByTransientLossOfFocus = false;
            seek(0);
            releaseServiceUiAndStop();
        }

        @Override
        public void onSkipToQueueItem(long id) {
            setQueuePosition((int) id);
        }

        @Override
        public boolean onMediaButtonEvent(@NonNull Intent mediaButtonIntent) {
            if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonIntent.getAction())) {
                KeyEvent ke = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                if (ke != null && ke.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) {
                    if (ke.getAction() == KeyEvent.ACTION_UP) {
                        handleHeadsetHookClick(ke.getEventTime());
                    }
                    return true;
                }
            }
            return super.onMediaButtonEvent(mediaButtonIntent);
        }
    });

    PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, MediaButtonIntentReceiver.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setMediaButtonReceiver(pi);

    mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
}

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;
        }/*  w w w.  j av  a2  s  .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 (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;
        }/*from  www.  j a 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:net.zorgblub.typhon.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 && MenuItemCompat.isActionViewExpanded(searchMenuItem)) {
        boolean result = MenuItemCompat.getActionView(searchMenuItem).dispatchKeyEvent(event);

        if (result) {
            return true;
        }//from w w  w. j  a  va  2s.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;
}