Search code examples
androidandroid-sourceonkeydownandroid-keypadandroid-hardware

Where in Android source does the KeyEvent dispatcher send to onKeyDown?


Where in the Android source code does Android call onKeyDown() when it receives notification from the hardware that a key was pressed?


Solution

  • In KeyEvent.java

     /**
         * Deliver this key event to a {@link Callback} interface.  If this is
         * an ACTION_MULTIPLE event and it is not handled, then an attempt will
         * be made to deliver a single normal event.
         *
         * @param receiver The Callback that will be given the event.
         * @param state State information retained across events.
         * @param target The target of the dispatch, for use in tracking.
         *
         * @return The return value from the Callback method that was called.
         */
        public final boolean dispatch(Callback receiver, DispatcherState state,
                Object target) {
            switch (mAction) {
                case ACTION_DOWN: {
                    mFlags &= ~FLAG_START_TRACKING;
                    if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
                            + ": " + this);
                    boolean res = receiver.onKeyDown(mKeyCode, this);
                    if (state != null) {
                        if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
                            if (DEBUG) Log.v(TAG, "  Start tracking!");
                            state.startTracking(this, target);
                        } else if (isLongPress() && state.isTracking(this)) {
                            try {
                                if (receiver.onKeyLongPress(mKeyCode, this)) {
                                    if (DEBUG) Log.v(TAG, "  Clear from long press!");
                                    state.performedLongPress(this);
                                    res = true;
                                }
                            } catch (AbstractMethodError e) {
                            }
                        }
                    }
                    return res;
                }