public void onDownEvent(int x, int y, long eventTime) {
   if (DEBUG) debugLog("onDownEvent:", x, y);
   int keyIndex = mKeyState.onDownKey(x, y, eventTime);
   mKeyboardLayoutHasBeenChanged = false;
   mKeyAlreadyProcessed = false;
   mIsRepeatableKey = false;
   mIsInSlidingKeyInput = false;
   checkMultiTap(eventTime, keyIndex);
   if (mListener != null) {
     if (isValidKeyIndex(keyIndex)) {
       Key key = mKeys[keyIndex];
       if (key.codes != null) mListener.onPress(key.getPrimaryCode());
       // This onPress call may have changed keyboard layout. Those cases are detected at
       // {@link #setKeyboard}. In those cases, we should update keyIndex according to the
       // new keyboard layout.
       if (mKeyboardLayoutHasBeenChanged) {
         mKeyboardLayoutHasBeenChanged = false;
         keyIndex = mKeyState.onDownKey(x, y, eventTime);
       }
     }
   }
   if (isValidKeyIndex(keyIndex)) {
     if (mKeys[keyIndex].repeatable) {
       repeatKey(keyIndex);
       mHandler.startKeyRepeatTimer(mDelayBeforeKeyRepeatStart, keyIndex, this);
       mIsRepeatableKey = true;
     }
     startLongPressTimer(keyIndex);
   }
   showKeyPreviewAndUpdateKey(keyIndex);
 }
  private void detectAndSendKey(Key key, int x, int y, long eventTime) {
    final OnKeyboardActionListener listener = mListener;

    if (key == null) {
      if (listener != null) listener.onCancel();
    } else {
      if (key.text != null) {
        if (listener != null) {
          listener.onText(key.text);
          listener.onRelease(0); // dummy key code
        }
      } else {
        if (key.codes == null) return;
        int code = key.getPrimaryCode();
        int[] codes = mKeyDetector.newCodeArray();
        mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes);
        // Multi-tap
        if (mInMultiTap) {
          if (mTapCount != -1) {
            mListener.onKey(Keyboard.KEYCODE_DELETE, KEY_DELETE, x, y);
          } else {
            mTapCount = 0;
          }
          code = key.codes[mTapCount];
        }
        /*
         * Swap the first and second values in the codes array if the primary code is not
         * the first value but the second value in the array. This happens when key
         * debouncing is in effect.
         */
        if (codes.length >= 2 && codes[0] != code && codes[1] == code) {
          codes[1] = codes[0];
          codes[0] = code;
        }
        if (listener != null) {
          listener.onKey(code, codes, x, y);
          listener.onRelease(code);
        }
      }
      mLastTapTime = eventTime;
    }
  }
 public void onMoveEvent(int x, int y, long eventTime) {
   if (DEBUG_MOVE) debugLog("onMoveEvent:", x, y);
   if (mKeyAlreadyProcessed) return;
   final KeyState keyState = mKeyState;
   int keyIndex = keyState.onMoveKey(x, y);
   final Key oldKey = getKey(keyState.getKeyIndex());
   if (isValidKeyIndex(keyIndex)) {
     boolean isMinorMoveBounce = isMinorMoveBounce(x, y, keyIndex);
     if (DEBUG_MOVE)
       Log.i(
           TAG,
           "isMinorMoveBounce="
               + isMinorMoveBounce
               + " oldKey="
               + (oldKey == null ? "null" : oldKey));
     if (oldKey == null) {
       // The pointer has been slid in to the new key, but the finger was not on any keys.
       // In this case, we must call onPress() to notify that the new key is being pressed.
       if (mListener != null) {
         Key key = getKey(keyIndex);
         if (key.codes != null) mListener.onPress(key.getPrimaryCode());
         // This onPress call may have changed keyboard layout. Those cases are detected
         // at {@link #setKeyboard}. In those cases, we should update keyIndex according
         // to the new keyboard layout.
         if (mKeyboardLayoutHasBeenChanged) {
           mKeyboardLayoutHasBeenChanged = false;
           keyIndex = keyState.onMoveKey(x, y);
         }
       }
       keyState.onMoveToNewKey(keyIndex, x, y);
       startLongPressTimer(keyIndex);
     } else if (!isMinorMoveBounce) {
       // The pointer has been slid in to the new key from the previous key, we must call
       // onRelease() first to notify that the previous key has been released, then call
       // onPress() to notify that the new key is being pressed.
       mIsInSlidingKeyInput = true;
       if (mListener != null && oldKey.codes != null) mListener.onRelease(oldKey.getPrimaryCode());
       resetMultiTap();
       if (mListener != null) {
         Key key = getKey(keyIndex);
         if (key.codes != null) mListener.onPress(key.getPrimaryCode());
         // This onPress call may have changed keyboard layout. Those cases are detected
         // at {@link #setKeyboard}. In those cases, we should update keyIndex according
         // to the new keyboard layout.
         if (mKeyboardLayoutHasBeenChanged) {
           mKeyboardLayoutHasBeenChanged = false;
           keyIndex = keyState.onMoveKey(x, y);
         }
         addSlideKey(oldKey);
       }
       keyState.onMoveToNewKey(keyIndex, x, y);
       startLongPressTimer(keyIndex);
     }
   } else {
     if (oldKey != null && !isMinorMoveBounce(x, y, keyIndex)) {
       // The pointer has been slid out from the previous key, we must call onRelease() to
       // notify that the previous key has been released.
       mIsInSlidingKeyInput = true;
       if (mListener != null && oldKey.codes != null) mListener.onRelease(oldKey.getPrimaryCode());
       resetMultiTap();
       keyState.onMoveToNewKey(keyIndex, x, y);
       mHandler.cancelLongPressTimer();
     }
   }
   showKeyPreviewAndUpdateKey(keyState.getKeyIndex());
 }