示例#1
0
 void sendKeyEventWithKeyCode(int keyCode, int flags) {
   long eventTime = System.currentTimeMillis();
   translateAndSendNativeEvents(
       new KeyEvent(
           eventTime,
           eventTime,
           KeyEvent.ACTION_DOWN,
           keyCode,
           0,
           0,
           KeyCharacterMap.VIRTUAL_KEYBOARD,
           0,
           flags));
   translateAndSendNativeEvents(
       new KeyEvent(
           System.currentTimeMillis(),
           eventTime,
           KeyEvent.ACTION_UP,
           keyCode,
           0,
           0,
           KeyCharacterMap.VIRTUAL_KEYBOARD,
           0,
           flags));
 }
  /** @see BaseInputConnection#sendKeyEvent(android.view.KeyEvent) */
  @Override
  public boolean sendKeyEvent(KeyEvent event) {
    if (DEBUG) {
      Log.w(
          TAG,
          "sendKeyEvent ["
              + event.getAction()
              + "] ["
              + event.getKeyCode()
              + "] ["
              + event.getUnicodeChar()
              + "]");
    }

    int action = event.getAction();
    int keycode = event.getKeyCode();
    int unicodeChar = event.getUnicodeChar();

    // If this isn't a KeyDown event, no need to update composition state; just pass the key
    // event through and return. But note that some keys, such as enter, may actually be
    // handled on ACTION_UP in Blink.
    if (action != KeyEvent.ACTION_DOWN) {
      mImeAdapter.translateAndSendNativeEvents(event);
      return true;
    }

    // If this is backspace/del or if the key has a character representation,
    // need to update the underlying Editable (i.e. the local representation of the text
    // being edited).  Some IMEs like Jellybean stock IME and Samsung IME mix in delete
    // KeyPress events instead of calling deleteSurroundingText.
    if (keycode == KeyEvent.KEYCODE_DEL) {
      deleteSurroundingTextImpl(1, 0, true);
    } else if (keycode == KeyEvent.KEYCODE_FORWARD_DEL) {
      deleteSurroundingTextImpl(0, 1, true);
    } else if (keycode == KeyEvent.KEYCODE_ENTER) {
      // Finish text composition when pressing enter, as that may submit a form field.
      // TODO(aurimas): remove this workaround when crbug.com/278584 is fixed.
      finishComposingText();
    } else if ((unicodeChar & KeyCharacterMap.COMBINING_ACCENT) != 0) {
      // Store a pending accent character and make it the current composition.
      int pendingAccent = unicodeChar & KeyCharacterMap.COMBINING_ACCENT_MASK;
      StringBuilder builder = new StringBuilder();
      builder.appendCodePoint(pendingAccent);
      setComposingText(builder.toString(), 1);
      mPendingAccent = pendingAccent;
      return true;
    } else if (mPendingAccent != 0 && unicodeChar != 0) {
      int combined = KeyEvent.getDeadChar(mPendingAccent, unicodeChar);
      if (combined != 0) {
        StringBuilder builder = new StringBuilder();
        builder.appendCodePoint(combined);
        commitText(builder.toString(), 1);
        return true;
      }
      // Noncombinable character; commit the accent character and fall through to sending
      // the key event for the character afterwards.
      finishComposingText();
    }
    replaceSelectionWithUnicodeChar(unicodeChar);
    mImeAdapter.translateAndSendNativeEvents(event);
    return true;
  }