Example usage for android.view KeyEvent KEYCODE_BACK

List of usage examples for android.view KeyEvent KEYCODE_BACK

Introduction

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

Prototype

int KEYCODE_BACK

To view the source code for android.view KeyEvent KEYCODE_BACK.

Click Source Link

Document

Key code constant: Back key.

Usage

From source file:com.lemon.lime.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:

            if (mWebView.canGoBack()) {
                getSupportActionBar().show();
                mWebView.goBack();/* www. ja  va2 s .co  m*/
            } else {
                finish();
            }
            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}

From source file:com.justone.android.main.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    // TODO Auto-generated method stub  
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //exitBy2Click();      //  
        this.finish();
    }//from  w w w .ja v  a  2s. c o m
    return false;
}

From source file:com.app.blockydemo.ui.fragment.FormulaEditorFragment.java

@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
    Log.i("info", "onKey() in FE-Fragment! keyCode: " + keyCode);
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        if (formulaEditorEditText.hasChanges()) {
            AlertDialog.Builder builder = new CustomAlertDialogBuilder(getActivity());
            builder.setTitle(R.string.formula_editor_discard_changes_dialog_title)
                    .setMessage(R.string.formula_editor_discard_changes_dialog_message)
                    .setNegativeButton(R.string.no, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            showToast(R.string.formula_editor_changes_discarded);
                            currentFormula.setDisplayText(null);
                            onUserDismiss();
                        }//from   w  ww . ja  v  a  2 s.  c o m
                    }).setPositiveButton(R.string.yes, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (saveFormulaIfPossible()) {
                                onUserDismiss();
                            }
                        }
                    }).create().show();

        } else {
            onUserDismiss();
        }

        return true;
    }
    return false;
}

From source file:cn.fulldroid.lib.datetimepicker.time.TimePickerDialog.java

/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key./*from   ww w  .  j  a  v a 2s  . c o m*/
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if (mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker, String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1 || keyCode == KeyEvent.KEYCODE_2
            || keyCode == KeyEvent.KEYCODE_3 || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7 || keyCode == KeyEvent.KEYCODE_8
            || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode && (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}

From source file:com.hirecraft.controls.mylib.datetimepicker.time.TimePickerDialog.java

/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key./*  ww w . j av  a  2  s  . c  om*/
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if (mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                DateTimeUtils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1 || keyCode == KeyEvent.KEYCODE_2
            || keyCode == KeyEvent.KEYCODE_3 || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7 || keyCode == KeyEvent.KEYCODE_8
            || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode && (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}

From source file:com.azuyo.happybeing.fourmob.timepicker.TimePickerDialog.java

/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key./* w  w w. ja v a2s .c  om*/
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if (mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker, String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1 || keyCode == KeyEvent.KEYCODE_2
            || keyCode == KeyEvent.KEYCODE_3 || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7 || keyCode == KeyEvent.KEYCODE_8
            || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode && (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                //Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}

From source file:es.uja.photofirma.android.CameraActivity.java

/**
 * El botn back se configura para cerrar sesin
 *///from www .j  a v  a 2 s . c o  m
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent mainActivity = new Intent(CameraActivity.this, MainActivity.class);
        startActivity(mainActivity);
        overridePendingTransition(R.anim.right_in, R.anim.right_out);
        super.finish();
        Toast.makeText(getApplicationContext(), getString(R.string.close_session_message), Toast.LENGTH_LONG)
                .show();
    }
    return false;// super.onKeyDown(keyCode, event);
}

From source file:com.example.tuicool.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        if (System.currentTimeMillis() - preTime < 2000) // 
        {//from  ww  w  .  ja v a  2s.c om
            finish();
        } else
            Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
        // System.exit(0);
        preTime = System.currentTimeMillis(); //  
    }
    return true;// 
}

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy./*ww w  .  j a va2  s .  c o  m*/
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            break;
        }
    }

    return handled;
}

From source file:com.aretha.slidemenu.SlideMenu.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (KeyEvent.ACTION_UP == event.getAction()) {
        final boolean isOpen = isOpen();
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            if (isOpen) {
                close(true);//  w w  w. j a  va2 s.co m
                return true;
            }
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (STATE_OPEN_LEFT == mCurrentState) {
                close(true);
                return true;
            } else if (!isOpen) {
                open(true, true);
                return true;
            }
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (STATE_OPEN_RIGHT == mCurrentState) {
                close(true);
                return true;
            } else if (!isOpen) {
                open(false, true);
                return true;
            }
            break;
        }
    }
    return super.dispatchKeyEvent(event);
}