Java android.view.KeyEvent 代码实例

・25 分钟阅读

以下是展示如何使用android.view.KeyEvent的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
                LOG.v(TAG, "onKeyDown()", " KeyEvent.KEYCODE_BACK ");
                if (appFrame.getCurrentState() == YouPlayerAppFrame.STATE_EXPLORER) {
                        if (appFrame.getContainer().currentHideType == YouPlayerContainerView.HIDE_LEFT) {
                                appFrame.getContainer().hide(YouPlayerContainerView.HIDE_BOTH);
                        } else if (appFrame.getContainer().currentHideType == YouPlayerContainerView.HIDE_BOTH) {
                                boolean flag = appFrame.getContainer().getCurrentViewControler().onkeyDown(keyCode, event);
                                LOG.v(TAG, "flag : ", flag + "");
                                if (!flag) {
                                        if (appFrame.getContainer().viewLevel == 1) {
                                                YouPlayerEventControler.fn_core_service_request(
                                                                You_Core.FN_COMMON_BTN_MAIN_MENU,
                                                                You_Core.FN_UI_EVT_TOUCH_UP, null, null);
                                        } else {
                                                YouPlayerEventControler.fn_core_service_request(
                                                                You_Core.FN_COMMON_BTN_BACK,
                                                                You_Core.FN_UI_EVT_TOUCH_UP, null, null);
                                        }
                                }
                        } else {
                                YouPlayerEventControler.fn_core_service_request(
                                                You_Core.FN_COMMON_BTN_BACK,
                                                You_Core.FN_UI_EVT_TOUCH_UP, null, null);
                        }
                } else if (appFrame.getCurrentState() == YouPlayerAppFrame.STATE_FULLPLAYER) {
                        appFrame.getFullPlayerControler().onkeyDown(keyCode, event);
                }
                break;
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (appFrame.getCurrentState() == YouPlayerAppFrame.STATE_FULLPLAYER) {
                        appFrame.getFullPlayerControler().onkeyDown(keyCode, event);
                } else if ((appFrame.getCurrentState() == YouPlayerAppFrame.STATE_EXPLORER)) {
                        appFrame.getContainer().getCurrentViewControler().onkeyDown(keyCode, event);
                }
                break;
        default:
                break;
        }
        return true;
}
 

实例 2


protected boolean onDoneEvent(int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE || event != null && (event.getAction() == KeyEvent.ACTION_DOWN &&
            event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
        if (!isUserDataValid()) {
            return true;
        }
        // hide keyboard before calling the done action
        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        View view = getActivity().getCurrentFocus();
        if (view != null) {
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
        // call child action
        onDoneAction();
        return true;
    }
    return false;
}
 

实例 3


/**
 * Description : Method to pause the song. It uses ACTION_MEDIA_BUTTON intent.
 */
public final void pauseSong()
{
        if(CAFConfig.isEnableDebugging())
        {
                Log.d(TAG,"pauseSong Method");
        }       
        try
        {
                mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
                eventtime = SystemClock.uptimeMillis();
                if(mAudioManager!=null)
                {
                        if(mAudioManager.isMusicActive())
                        {
                                Log.d("PauseSong","Check1");
                                Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
                                KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
                                downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
                                localContext.sendOrderedBroadcast(downIntent, null);
                                Log.d("PauseSong","Check2");
                        }
                }
        }
        catch(Exception e)
        {
                e.printStackTrace();
        }
}
 

实例 4


@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    if (appView != null && (appView.isCustomViewShowing() || appView.getFocusedChild() != null ) &&
            (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU)) {
        return appView.onKeyUp(keyCode, event);
    } else {
        return super.onKeyUp(keyCode, event);
        }
}
 

实例 5


@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
            && event.getAction() == KeyEvent.ACTION_UP) {
        if (mOnImeBack != null)
            mOnImeBack.onImeBack(this, this.getText().toString());
    }
    return super.onKeyPreIme(keyCode, event);
}
 

实例 6


public static int[] getButtonCodes() {
        return new int[] { 
                        //Core/regular buttons
                        KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, 
                        KeyEvent.KEYCODE_A, KeyEvent.KEYCODE_B, KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2, 
                        KeyEvent.KEYCODE_PLUS, KeyEvent.KEYCODE_MINUS, KeyEvent.KEYCODE_H,
                        //Classic buttons
                        KEYCODE_BUTTON_A, KEYCODE_BUTTON_B, KEYCODE_BUTTON_X, KEYCODE_BUTTON_Y,
                        KEYCODE_BUTTON_L1, KEYCODE_BUTTON_R1, KEYCODE_BUTTON_L2, KEYCODE_BUTTON_R2,
                        //Nunchuck/classic thumbstick keys
                        KeyEvent.KEYCODE_W, KeyEvent.KEYCODE_Q, KeyEvent.KEYCODE_S, KeyEvent.KEYCODE_D,
                        KeyEvent.KEYCODE_8, KeyEvent.KEYCODE_4, KeyEvent.KEYCODE_5, KeyEvent.KEYCODE_6,
                        //Nunchuck buttons
                        KeyEvent.KEYCODE_C, KeyEvent.KEYCODE_Z,
                        //Core accelerometer keys
                        //KeyEvent.KEYCODE_N, KeyEvent.KEYCODE_M,
                        //KeyEvent.KEYCODE_J, KeyEvent.KEYCODE_K,
                        //KeyEvent.KEYCODE_I, KeyEvent.KEYCODE_O,
                        //Nunchuck accelerometer keys
                        //KeyEvent.KEYCODE_V, KeyEvent.KEYCODE_B,
                        //KeyEvent.KEYCODE_G, KeyEvent.KEYCODE_H,
                        //KeyEvent.KEYCODE_Y, KeyEvent.KEYCODE_U,
        };
}
 

实例 7


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown() called: " + keyCode  + "|Event:" + event.getKeyCode());
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_CAMERA) {
                mCamera.autoFocus(this);
                return true;
        }
        return super.onKeyDown(keyCode, event);
}
 

实例 8


private void setupMap() {
        for (int i = 0; i < keys.length ; i++)
                keys[i] = -1;
        keys[0x4e] = KeyEvent.KEYCODE_MINUS;
        keys[0x0e] = KeyEvent.KEYCODE_GRAVE;
        keys[0x55] = KeyEvent.KEYCODE_EQUALS;
        keys[0x66] = KeyEvent.KEYCODE_DEL; // BACKSPACE!
        keys[0x0d] = KeyEvent.KEYCODE_TAB;
        keys[0x54] = KeyEvent.KEYCODE_LEFT_BRACKET;
        keys[0x5b] = KeyEvent.KEYCODE_RIGHT_BRACKET;
        keys[0x5d] = KeyEvent.KEYCODE_BACKSLASH;
        keys[0x4c] = KeyEvent.KEYCODE_SEMICOLON;
        keys[0x52] = KeyEvent.KEYCODE_APOSTROPHE;
        keys[0x5a] = KeyEvent.KEYCODE_ENTER;
        keys[0x48] = KeyEvent.KEYCODE_COMMA;
        keys[0x49] = KeyEvent.KEYCODE_PERIOD;
        keys[0x4a] = KeyEvent.KEYCODE_SLASH;
        keys[0x11] = KeyEvent.KEYCODE_HOME;
        keys[0x12] = KeyEvent.KEYCODE_SHIFT_LEFT;
        keys[0x59] = KeyEvent.KEYCODE_SHIFT_RIGHT;
        keys[0x29] = KeyEvent.KEYCODE_SPACE;
        keys[0x28] = KeyEvent.KEYCODE_DPAD_UP;
        keys[0x5e] = KeyEvent.KEYCODE_DPAD_LEFT;
        keys[0x60] = KeyEvent.KEYCODE_DPAD_DOWN;
        keys[0x2f] = KeyEvent.KEYCODE_DPAD_RIGHT;
        keys[0x16] = KeyEvent.KEYCODE_1;
        keys[0x1e] = KeyEvent.KEYCODE_2;
        keys[0x26] = KeyEvent.KEYCODE_3;
        keys[0x25] = KeyEvent.KEYCODE_4;
        keys[0x2e] = KeyEvent.KEYCODE_5;
        keys[0x36] = KeyEvent.KEYCODE_6;
        keys[0x3d] = KeyEvent.KEYCODE_7;
        keys[0x3e] = KeyEvent.KEYCODE_8;
        keys[0x46] = KeyEvent.KEYCODE_9;
        keys[0x45] = KeyEvent.KEYCODE_0;
        keys[0x15] = KeyEvent.KEYCODE_Q;
        keys[0x1d] = KeyEvent.KEYCODE_W;
        keys[0x24] = KeyEvent.KEYCODE_E;
        keys[0x2d] = KeyEvent.KEYCODE_R;
        keys[0x2c] = KeyEvent.KEYCODE_T;
        keys[0x35] = KeyEvent.KEYCODE_Y;
        keys[0x3c] = KeyEvent.KEYCODE_U;
        keys[0x43] = KeyEvent.KEYCODE_I;
        keys[0x44] = KeyEvent.KEYCODE_O;
        keys[0x4d] = KeyEvent.KEYCODE_P;
        keys[0x1c] = KeyEvent.KEYCODE_A;
        keys[0x1b] = KeyEvent.KEYCODE_S; 
        keys[0x23] = KeyEvent.KEYCODE_D;
        keys[0x2b] = KeyEvent.KEYCODE_F;
        keys[0x34] = KeyEvent.KEYCODE_G;
        keys[0x33] = KeyEvent.KEYCODE_H;
        keys[0x3b] = KeyEvent.KEYCODE_J;
        keys[0x42] = KeyEvent.KEYCODE_K;
        keys[0x4b] = KeyEvent.KEYCODE_L;
        keys[0x1a] = KeyEvent.KEYCODE_Z;
        keys[0x22] = KeyEvent.KEYCODE_X;
        keys[0x21] = KeyEvent.KEYCODE_C;
        keys[0x2a] = KeyEvent.KEYCODE_V;
        keys[0x32] = KeyEvent.KEYCODE_B;
        keys[0x31] = KeyEvent.KEYCODE_N;
        keys[0x3a] = KeyEvent.KEYCODE_M;
        keys[0x30] = KeyEvent.KEYCODE_ALT_RIGHT;
        keys[0x03] = KeyEvent.KEYCODE_MENU; // Windows key
        // The following won't work below Android 3.0, probably
        keys[0x02] = KeyEvent.KEYCODE_FUNCTION;
        keys[0x1f] = KeyEvent.KEYCODE_FORWARD_DEL;
        keys[0x58] = KeyEvent.KEYCODE_CAPS_LOCK;
        keys[0x14] = KeyEvent.KEYCODE_CTRL_LEFT;
}
 

实例 9


@SuppressWarnings("deprecation")
void findItemsWithShortcutForKey(List<MenuItemImpl> items, int keyCode, KeyEvent event) {
    final boolean qwerty = isQwertyMode();
    final int metaState = event.getMetaState();
    final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
    // Get the chars associated with the keyCode (i.e using any chording combo)
    final boolean isKeyCodeMapped = event.getKeyData(possibleChars);
    // The delete key is not mapped to 'b' so we treat it specially
    if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) {
        return;
    }
    // Look for an item whose shortcut is this key.
    final int N = mItems.size();
    for (int i = 0; i < N; i++) {
        MenuItemImpl item = mItems.get(i);
        if (item.hasSubMenu()) {
            ((MenuBuilder)item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event);
        }
        final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut();
        if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) &&
              (shortcutChar != 0) &&
              (shortcutChar == possibleChars.meta[0]
                  || shortcutChar == possibleChars.meta[2]
                  || (qwerty && shortcutChar == 'b' &&
                      keyCode == KeyEvent.KEYCODE_DEL)) &&
              item.isEnabled()) {
            items.add(item);
        }
    }
}
 

实例 10


@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
    // Get all items that can be associated directly or indirectly with the keyCode
    ArrayList<MenuItemImpl> items = mTempShortcutItemList;
    items.clear();
    findItemsWithShortcutForKey(items, keyCode, event);
    if (items.isEmpty()) {
        return null;
    }
    final int metaState = event.getMetaState();
    final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
    // Get the chars associated with the keyCode (i.e using any chording combo)
    event.getKeyData(possibleChars);
    // If we have only one element, we can safely returns it
    final int size = items.size();
    if (size == 1) {
        return items.get(0);
    }
    final boolean qwerty = isQwertyMode();
    // If we found more than one item associated with the key,
    // we have to return the exact match
    for (int i = 0; i < size; i++) {
        final MenuItemImpl item = items.get(i);
        final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
                item.getNumericShortcut();
        if ((shortcutChar == possibleChars.meta[0] &&
                (metaState & KeyEvent.META_ALT_ON) == 0)
            || (shortcutChar == possibleChars.meta[2] &&
                (metaState & KeyEvent.META_ALT_ON) != 0)
            || (qwerty && shortcutChar == 'b' &&
                keyCode == KeyEvent.KEYCODE_DEL)) {
            return item;
        }
    }
    return null;
}
 

实例 11


@Override
public void onReceive(Context context, Intent intent) {
        Log.v(SRPlayer.TAG, "MediabuttonReciever recived event.");
        String intentAction = intent.getAction();
        if(!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){                   
                return;
        }
        KeyEvent MediaButtonEvent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (MediaButtonEvent == null) {                 
           return;
       }
       int keycode = MediaButtonEvent.getKeyCode();
       int action = MediaButtonEvent.getAction();
       if (action == KeyEvent.ACTION_DOWN) {            
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                Boolean MediaButtonEnable = prefs.getBoolean("MediaButtonEnable", false);
                if (MediaButtonEnable)
                {               
               switch (keycode) {
               case KeyEvent.KEYCODE_HEADSETHOOK:
               case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                Log.v(SRPlayer.TAG, "MediabuttonReciever supported event.");
                Intent ServiceIntent = new Intent(context, PlayerService.class);
                        ServiceIntent.addFlags(PlayerService.TOGGLE_STREAMING_STATUS);
                        context.startService(ServiceIntent);
                   break;
               case KeyEvent.KEYCODE_MEDIA_NEXT:
               case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
               case KeyEvent.KEYCODE_MEDIA_STOP:
               default:
                Log.v(SRPlayer.TAG, "MediabuttonReciever got not yet supported media key enent.");
                return;
               }
                //Do not send the broadcast to the receivers with 
                //lower priority
                abortBroadcast();                       
                }
       }        
}
 

实例 12


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK && keyCode != KeyEvent.KEYCODE_VOLUME_UP && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && keyCode != KeyEvent.KEYCODE_MENU && keyCode != KeyEvent.KEYCODE_CALL && keyCode != KeyEvent.KEYCODE_ENDCALL;
  if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keyCode == KeyEvent.KEYCODE_SPACE) {
      if (mMediaPlayer.isPlaying()) {
        pause();
        mMediaController.show();
      } else {
        start();
        mMediaController.hide();
      }
      return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
      if (!mMediaPlayer.isPlaying()) {
          start();
          mMediaController.hide();
      }
      return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
      if (mMediaPlayer.isPlaying()) {
        pause();
        mMediaController.show();
      }
      return true;
    } else {
      toggleMediaControlsVisiblity();
    }
  }
  return super.onKeyDown(keyCode, event);
}
 

实例 13


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
                case KeyEvent.KEYCODE_FOCUS:
                        // Reset auto focus when dedicated photo button is completely released
                        _focusManager.resetFocus();
                        return true;
                case KeyEvent.KEYCODE_CAMERA:
                        if (event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_UP) {
                                // Take photo when the dedicated photo button is pressed
                                MotionEvent motevent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_UP, 0, 0, 0);
                                _captureListener.onTouch(null, motevent);
                                motevent.recycle();
                        }
                        return true;
        }
        return super.onKeyUp(keyCode, event);
}
 

实例 14


/**
 * Description : Method to play previous song. It uses ACTION_MEDIA_BUTTON intent.
 */
public final void playPrevSong()
{
        if(CAFConfig.isEnableDebugging())
        {
                Log.d(TAG,"playPrevSong Method");
        }       
        try
        {
                if(mAudioManager!=null)
                {
                        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
                        eventtime = SystemClock.uptimeMillis();
                        if(mAudioManager.isMusicActive())
                        {
                                Intent downIntentprev = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
                                KeyEvent downEventprev = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
                                downIntentprev.putExtra(Intent.EXTRA_KEY_EVENT, downEventprev);
                                localContext.sendOrderedBroadcast(downIntentprev, null);
                        }
                }
        }
        catch(Exception e)
        {
                e.printStackTrace();
        }
}
 

实例 15


/**
 * Description : Play the next Song. It uses ACTION_MEDIA_BUTTON intent.
 */
public final void playNextSong()
{
        if(CAFConfig.isEnableDebugging())
        {
                Log.d(TAG,"playNextSong Method");
        }       
        try
        {
                if(mAudioManager!=null)
                {
                        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
                        eventtime = SystemClock.uptimeMillis();
                        if(mAudioManager.isMusicActive())
                        {
                                Intent downIntentnext = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
                                KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN,   KeyEvent.KEYCODE_MEDIA_NEXT, 0);
                                downIntentnext.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
                                localContext.sendOrderedBroadcast(downIntentnext, null);
                        }
                }
        }
        catch(Exception e)
        {
                e.printStackTrace();
        }
}
 
讨论
淘淘あ西西 profile image