Example usage for android.view KeyEvent startTracking

List of usage examples for android.view KeyEvent startTracking

Introduction

In this page you can find the example usage for android.view KeyEvent startTracking.

Prototype

public final void startTracking() 

Source Link

Document

Call this during Callback#onKeyDown to have the system track the key through its final up (possibly including a long press).

Usage

From source file:com.strathclyde.highlightingkeyboard.SoftKeyboardService.java

/**
 * Use this to monitor key events being delivered to the application.
 * We get first crack at them, and can either resume them or let them
 * continue to the app./*from   w  ww .  j av a 2s  .  com*/
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    //event.
    //Log.i("OnKeyDown", "Keycode: "+keyCode);
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        // The InputMethodService already takes care of the back
        // key for us, to dismiss the input method if it is shown.
        // However, our keyboard could be showing a pop-up window
        // that back should dismiss, so we first allow it to do that.
        if (event.getRepeatCount() == 0 && mInputView != null) {
            if (mInputView.handleBack()) {
                return true;
            }
        }
        break;

    case KeyEvent.KEYCODE_DEL:
        // Special handling of the delete key: if we currently are
        // composing text for the user, we want to modify that instead
        // of let the application to the delete itself.
        if (mComposing.length() > 0) {
            onKey(Keyboard.KEYCODE_DELETE, null);
            return true;
        }
        break;

    case -2: //123 button
        //Log.i("KeyDown", "Keycode: "+keyCode);
        event.startTracking();
        return true;

    case KeyEvent.KEYCODE_ENTER:
        // Let the underlying text editor always handle these.
        return false;

    default:
        // For all other keys, if we want to do transformations on
        // text being entered with a hard keyboard, we need to process
        // it and do the appropriate action.
        if (PROCESS_HARD_KEYS) {
            if (keyCode == KeyEvent.KEYCODE_SPACE && (event.getMetaState() & KeyEvent.META_ALT_ON) != 0) {
                // A silly example: in our input method, Alt+Space
                // is a shortcut for 'android' in lower case.
                //InputConnection ic = ic;
                if (ic != null) {
                    // First, tell the editor that it is no longer in the
                    // shift state, since we are consuming this.
                    ic.clearMetaKeyStates(KeyEvent.META_ALT_ON);
                    keyDownUp(KeyEvent.KEYCODE_A);
                    keyDownUp(KeyEvent.KEYCODE_N);
                    keyDownUp(KeyEvent.KEYCODE_D);
                    keyDownUp(KeyEvent.KEYCODE_R);
                    keyDownUp(KeyEvent.KEYCODE_O);
                    keyDownUp(KeyEvent.KEYCODE_I);
                    keyDownUp(KeyEvent.KEYCODE_D);
                    // And we consume this event.
                    return true;
                }
            }
            if (mPredictionOn && translateKeyDown(keyCode, event)) {
                return true;
            }
        }
    }

    return super.onKeyDown(keyCode, event);
}

From source file:android.app.Activity.java

/**
 * Called when a key was pressed down and not handled by any of the views
 * inside of the activity. So, for example, key presses while the cursor 
 * is inside a TextView will not trigger the event (unless it is a navigation
 * to another object) because TextView handles its own key presses.
 * /*w  w w  .j a va2 s.  c  o m*/
 * <p>If the focused view didn't want this event, this method is called.
 *
 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
 * by calling {@link #onBackPressed()}, though the behavior varies based
 * on the application compatibility mode: for
 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
 * it will set up the dispatch to call {@link #onKeyUp} where the action
 * will be performed; for earlier applications, it will perform the
 * action immediately in on-down, as those versions of the platform
 * behaved.
 * 
 * <p>Other additional default key handling may be performed
 * if configured with {@link #setDefaultKeyMode}.
 * 
 * @return Return <code>true</code> to prevent this event from being propagated
 * further, or <code>false</code> to indicate that you have not handled 
 * this event and it should continue to be propagated.
 * @see #onKeyUp
 * @see android.view.KeyEvent
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.ECLAIR) {
            event.startTracking();
        } else {
            onBackPressed();
        }
        return true;
    }

    if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
        return false;
    } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
        if (getWindow().performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
                Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
            return true;
        }
        return false;
    } else {
        // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
        boolean clearSpannable = false;
        boolean handled;
        if ((event.getRepeatCount() != 0) || event.isSystem()) {
            clearSpannable = true;
            handled = false;
        } else {
            handled = TextKeyListener.getInstance().onKeyDown(null, mDefaultKeySsb, keyCode, event);
            if (handled && mDefaultKeySsb.length() > 0) {
                // something useable has been typed - dispatch it now.

                final String str = mDefaultKeySsb.toString();
                clearSpannable = true;

                switch (mDefaultKeyMode) {
                case DEFAULT_KEYS_DIALER:
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    break;
                case DEFAULT_KEYS_SEARCH_LOCAL:
                    startSearch(str, false, null, false);
                    break;
                case DEFAULT_KEYS_SEARCH_GLOBAL:
                    startSearch(str, false, null, true);
                    break;
                }
            }
        }
        if (clearSpannable) {
            mDefaultKeySsb.clear();
            mDefaultKeySsb.clearSpans();
            Selection.setSelection(mDefaultKeySsb, 0);
        }
        return handled;
    }
}