Example usage for android.view KeyEvent getEventTime

List of usage examples for android.view KeyEvent getEventTime

Introduction

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

Prototype

@Override
public final long getEventTime() 

Source Link

Document

Retrieve the time this event occurred, in the android.os.SystemClock#uptimeMillis time base.

Usage

From source file:com.example.angel.parkpanda.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        switch (event.getAction()) {
        case KeyEvent.ACTION_DOWN:
            if (event.getDownTime() - lastPressedTime < PERIOD) {
                System.exit(0);//from   ww  w .j  av  a 2s . c  o m
                finish();

            } else {
                Toast.makeText(getApplicationContext(), "Press again to exit.", Toast.LENGTH_SHORT).show();
                lastPressedTime = event.getEventTime();
            }
            return true;
        }
    }
    return false;
}

From source file:net.sourceforge.servestream.activity.MediaPlayerActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    int repcnt = event.getRepeatCount();

    if ((seekmethod == 0) ? seekMethod1(keyCode) : seekMethod2(keyCode))
        return true;

    switch (keyCode) {
    case KeyEvent.KEYCODE_SLASH:
        seekmethod = 1 - seekmethod;//from   w ww  .  j  a v  a  2s .c om
        return true;

    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (!useDpadMusicControl()) {
            break;
        }
        if (!mPrevButton.hasFocus()) {
            mPrevButton.requestFocus();
        }
        scanBackward(repcnt, event.getEventTime() - event.getDownTime());
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (!useDpadMusicControl()) {
            break;
        }
        if (!mNextButton.hasFocus()) {
            mNextButton.requestFocus();
        }
        scanForward(repcnt, event.getEventTime() - event.getDownTime());
        return true;

    case KeyEvent.KEYCODE_S:
        toggleShuffle();
        return true;

    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_SPACE:
        doPauseResume();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:org.yammp.app.MusicPlaybackActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    int repcnt = event.getRepeatCount();

    switch (keyCode) {

    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (!useDpadMusicControl()) {
            break;
        }/*from  ww  w.  j  a v  a  2 s  .  c  o m*/
        if (!mPrevButton.hasFocus()) {
            mPrevButton.requestFocus();
        }
        scanBackward(repcnt, event.getEventTime() - event.getDownTime());
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (!useDpadMusicControl()) {
            break;
        }
        if (!mNextButton.hasFocus()) {
            mNextButton.requestFocus();
        }
        scanForward(repcnt, event.getEventTime() - event.getDownTime());
        return true;

    // case KeyEvent.KEYCODE_R:
    // toggleRepeat();
    // return true;
    //
    // case KeyEvent.KEYCODE_S:
    // toggleShuffle();
    // return true;

    case KeyEvent.KEYCODE_N:
        if (mService != null) {
            try {
                mService.next();
                return true;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else
            return false;

    case KeyEvent.KEYCODE_P:
        if (mService != null) {
            try {
                mService.prev();
                return true;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else
            return false;

    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_SPACE:
        doPauseResume();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:org.musicmod.android.app.MusicPlaybackActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    int repcnt = event.getRepeatCount();

    switch (keyCode) {

    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (!useDpadMusicControl()) {
            break;
        }//from w w  w .j av a2  s  .  c o m
        if (!mPrevButton.hasFocus()) {
            mPrevButton.requestFocus();
        }
        scanBackward(repcnt, event.getEventTime() - event.getDownTime());
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (!useDpadMusicControl()) {
            break;
        }
        if (!mNextButton.hasFocus()) {
            mNextButton.requestFocus();
        }
        scanForward(repcnt, event.getEventTime() - event.getDownTime());
        return true;

    //         case KeyEvent.KEYCODE_R:
    //            toggleRepeat();
    //            return true;
    //
    //         case KeyEvent.KEYCODE_S:
    //            toggleShuffle();
    //            return true;

    case KeyEvent.KEYCODE_N:
        if (mService != null) {
            try {
                mService.next();
                return true;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else {
            return false;
        }

    case KeyEvent.KEYCODE_P:
        if (mService != null) {
            try {
                mService.prev();
                return true;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else {
            return false;
        }

    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_SPACE:
        doPauseResume();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.android.app.MediaPlaybackActivity.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    try {/*from   w w  w.j a  v a2 s .  com*/
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (!useDpadMusicControl()) {
                break;
            }
            if (mService != null) {
                if (!mSeeking && mStartSeekPos >= 0) {
                    mPauseButton.requestFocus();
                    if (mStartSeekPos < 1000) {
                        mService.prev();
                    } else {
                        mService.seek(0);
                    }
                } else {
                    scanBackward(-1, event.getEventTime() - event.getDownTime());
                    mPauseButton.requestFocus();
                    mStartSeekPos = -1;
                }
            }
            mSeeking = false;
            mPosOverride = -1;
            return true;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (!useDpadMusicControl()) {
                break;
            }
            if (mService != null) {
                if (!mSeeking && mStartSeekPos >= 0) {
                    mPauseButton.requestFocus();
                    mService.next();
                } else {
                    scanForward(-1, event.getEventTime() - event.getDownTime());
                    mPauseButton.requestFocus();
                    mStartSeekPos = -1;
                }
            }
            mSeeking = false;
            mPosOverride = -1;
            return true;
        }
    } catch (RemoteException ex) {
    }
    return super.onKeyUp(keyCode, event);
}

From source file:org.musicmod.android.app.MusicPlaybackActivity.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {

    try {// w  ww .ja  va  2s  . c  o  m
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (!useDpadMusicControl()) {
                break;
            }
            if (mService != null) {
                if (!mSeeking && mStartSeekPos >= 0) {
                    mPauseButton.requestFocus();
                    if (mStartSeekPos < 1000) {
                        mService.prev();
                    } else {
                        mService.seek(0);
                    }
                } else {
                    scanBackward(-1, event.getEventTime() - event.getDownTime());
                    mPauseButton.requestFocus();
                    mStartSeekPos = -1;
                }
            }
            mSeeking = false;
            mPosOverride = -1;
            return true;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (!useDpadMusicControl()) {
                break;
            }
            if (mService != null) {
                if (!mSeeking && mStartSeekPos >= 0) {
                    mPauseButton.requestFocus();
                    mService.next();
                } else {
                    scanForward(-1, event.getEventTime() - event.getDownTime());
                    mPauseButton.requestFocus();
                    mStartSeekPos = -1;
                }
            }
            mSeeking = false;
            mPosOverride = -1;
            return true;
        }
    } catch (RemoteException ex) {
    }
    return super.onKeyUp(keyCode, event);
}

From source file:org.apache.cordova.AndroidWebView.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // A custom view is currently displayed  (e.g. playing a video)
        if (mCustomView != null) {
            this.hideCustomView();
        } else {//  ww  w. j ava 2  s . c  om
            // The webview is currently displayed
            // If back key is bound, then send event to JavaScript
            if (this.bound) {
                this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
                return true;
            } else {
                // If not bound
                // Go to previous page in webview if it is possible to go back
                if (this.backHistory()) {
                    return true;
                }
                // If not, then invoke default behavior
                else {
                    //this.activityState = ACTIVITY_EXITING;
                    //return false;
                    // If they hit back button when app is initializing, app should exit instead of hang until initialization (CB2-458)
                    this.cordova.getActivity().finish();
                }
            }
        }
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (this.lastMenuEventTime < event.getEventTime()) {
            this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
        }
        this.lastMenuEventTime = event.getEventTime();
        return super.onKeyUp(keyCode, event);
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
        return true;
    } else if (keyUpCodes.contains(keyCode)) {
        //What the hell should this do?
        return super.onKeyUp(keyCode, event);
    }

    //Does webkit change this behavior?
    return super.onKeyUp(keyCode, event);
}

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

private void setUpMediaSession() {
    mSession = new MediaSession(this, "KomaMusic");
    mSession.setCallback(new MediaSession.Callback() {
        @Override//from   www . j av  a2 s  .  com
        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:com.android.app.MediaPlaybackActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    int direction = -1;
    int repcnt = event.getRepeatCount();

    if ((seekmethod == 0) ? seekMethod1(keyCode) : seekMethod2(keyCode))
        return true;

    switch (keyCode) {
    /*//  www.  j  a v a 2 s  .  c o  m
    // image scale
    case KeyEvent.KEYCODE_Q: av.adjustParams(-0.05, 0.0, 0.0, 0.0, 0.0,-1.0); break;
    case KeyEvent.KEYCODE_E: av.adjustParams( 0.05, 0.0, 0.0, 0.0, 0.0, 1.0); break;
    // image translate
    case KeyEvent.KEYCODE_W: av.adjustParams(    0.0, 0.0,-1.0, 0.0, 0.0, 0.0); break;
    case KeyEvent.KEYCODE_X: av.adjustParams(    0.0, 0.0, 1.0, 0.0, 0.0, 0.0); break;
    case KeyEvent.KEYCODE_A: av.adjustParams(    0.0,-1.0, 0.0, 0.0, 0.0, 0.0); break;
    case KeyEvent.KEYCODE_D: av.adjustParams(    0.0, 1.0, 0.0, 0.0, 0.0, 0.0); break;
    // camera rotation
    case KeyEvent.KEYCODE_R: av.adjustParams(    0.0, 0.0, 0.0, 0.0, 0.0,-1.0); break;
    case KeyEvent.KEYCODE_U: av.adjustParams(    0.0, 0.0, 0.0, 0.0, 0.0, 1.0); break;
    // camera translate
    case KeyEvent.KEYCODE_Y: av.adjustParams(    0.0, 0.0, 0.0, 0.0,-1.0, 0.0); break;
    case KeyEvent.KEYCODE_N: av.adjustParams(    0.0, 0.0, 0.0, 0.0, 1.0, 0.0); break;
    case KeyEvent.KEYCODE_G: av.adjustParams(    0.0, 0.0, 0.0,-1.0, 0.0, 0.0); break;
    case KeyEvent.KEYCODE_J: av.adjustParams(    0.0, 0.0, 0.0, 1.0, 0.0, 0.0); break;
            
    */

    case KeyEvent.KEYCODE_SLASH:
        seekmethod = 1 - seekmethod;
        return true;

    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (!useDpadMusicControl()) {
            break;
        }
        if (!mPrevButton.hasFocus()) {
            mPrevButton.requestFocus();
        }
        scanBackward(repcnt, event.getEventTime() - event.getDownTime());
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (!useDpadMusicControl()) {
            break;
        }
        if (!mNextButton.hasFocus()) {
            mNextButton.requestFocus();
        }
        scanForward(repcnt, event.getEventTime() - event.getDownTime());
        return true;

    case KeyEvent.KEYCODE_S:
        toggleShuffle();
        return true;

    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_SPACE:
        doPauseResume();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
    Logger.d(TAG, "onKeyUp keycode=%d", keyCode);
    switch (keyCode) {
    // Issue 248/*from ww w  . ja  va2 s  . c  om*/
    case KeyEvent.KEYCODE_VOLUME_DOWN:
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (!isInputViewShown()) {
            return super.onKeyUp(keyCode, event);
        }
        if (mAskPrefs.useVolumeKeyForLeftRight()) {
            // no need of vol up/down sound
            return true;
        }
    case KeyEvent.KEYCODE_DPAD_DOWN:
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_DPAD_LEFT:
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (getInputView() != null && getInputView().isShown() && getInputView().isShifted()) {
            event = new KeyEvent(event.getDownTime(), event.getEventTime(), event.getAction(),
                    event.getKeyCode(), event.getRepeatCount(), event.getDeviceId(), event.getScanCode(),
                    KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON);
            InputConnection ic = getCurrentInputConnection();
            if (ic != null)
                ic.sendKeyEvent(event);

            return true;
        }
        break;
    case KeyEvent.KEYCODE_ALT_LEFT:
    case KeyEvent.KEYCODE_ALT_RIGHT:
    case KeyEvent.KEYCODE_SHIFT_LEFT:
    case KeyEvent.KEYCODE_SHIFT_RIGHT:
    case KeyEvent.KEYCODE_SYM:
        mMetaState = MyMetaKeyKeyListener.handleKeyUp(mMetaState, keyCode, event);
        Logger.d(TAG + "-meta-key", getMetaKeysStates("onKeyUp"));
        setInputConnectionMetaStateAsCurrentMetaKeyKeyListenerState();
        break;
    }
    return super.onKeyUp(keyCode, event);
}