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:Main.java

/**
 * Checks whether the given event is any of ENTER or NUMPAD ENTER.
 * @param event Event to be checked./*from ww  w . j a v  a2  s.  c o m*/
 * @return Whether the event should be processed as ENTER.
 */
public static boolean isEnter(KeyEvent event) {
    return isActionUp(event) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
            || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER);
}

From source file:Main.java

/**
 * Checks whether the given event is any of DPAD up or NUMPAD up.
 * @param event Event to be checked./*from  w ww .  ja v  a 2s  . co m*/
 * @return Whether the event should be processed as a navigation up.
 */
public static boolean isGoUp(KeyEvent event) {
    return isActionDown(event) && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
            || (!event.isNumLockOn() && event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_8));
}

From source file:Main.java

/**
 * Checks whether the given event is any of DPAD right or NUMPAD right.
 * @param event Event to be checked./*from   w w  w.j  a  v a  2 s  .  c  o m*/
 * @return Whether the event should be processed as a navigation right.
 */
public static boolean isGoRight(KeyEvent event) {
    return isActionDown(event) && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT
            || (!event.isNumLockOn() && event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_6));
}

From source file:Main.java

/**
 * Checks whether the given event is any of DPAD down or NUMPAD down.
 * @param event Event to be checked./*from   ww w  .j a  va2 s  . c o  m*/
 * @return Whether the event should be processed as a navigation down.
 */
public static boolean isGoDown(KeyEvent event) {
    return isActionDown(event) && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN
            || (!event.isNumLockOn() && event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_2));
}

From source file:com.dragedy.playermusic.service.MediaButtonIntentReceiver.java

public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }// www  .  j a  va  2  s. co  m

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime();

        String command = null;
        switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MusicService.ACTION_STOP;
            break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MusicService.ACTION_TOGGLE_PAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MusicService.ACTION_SKIP;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MusicService.ACTION_REWIND;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MusicService.ACTION_PAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MusicService.ACTION_PLAY;
            break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG)
                            Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0,
                                context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.skubit.satoshidice.placebet.BetEditText.java

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (mEventIntent != null) {
            LocalBroadcastManager.getInstance(this.getContext()).sendBroadcast(mEventIntent);
        }/*www.ja  va 2s.co  m*/
        return true;
    }
    return super.onKeyPreIme(keyCode, event);
}

From source file:com.app.khclub.base.easeim.activity.BaseActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        finish();//from ww  w  . j av a2s .  com
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        return true;
    } else
        return super.onKeyDown(keyCode, event);
}

From source file:com.farmerbb.taskbar.widget.StartMenuLayout.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (viewHandlesBackButton && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        LocalBroadcastManager.getInstance(getContext())
                .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
        return true;
    }//from   w  w  w  . j a v a  2  s. c om

    return super.dispatchKeyEvent(event);
}

From source file:com.darly.im.ui.BaseFragment.java

/**
 * ?,??/*ww w  .jav a2s.  c o m*/
 * @param keyCode
 * @param event
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (streamVolume >= mMusicMaxVolume) {
            LogUtil.d(LogUtil.getLogUtilsTag(BaseFragment.class), "has set the max volume");
            return true;
        }
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume + mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume - mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

From source file:com.gao.im.ui.BaseFragment.java

/**
 * ?,??/*  w ww. j a v a  2s.  co m*/
 * @param keyCode
 * @param event
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (streamVolume >= mMusicMaxVolume) {
            LogUtil.d(LogUtil.getLogUtilsTag(BaseFragment.class), "has set the max volume");
            return true;
        }
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume + mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume - mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}