Example usage for android.view KeyEvent getKeyCode

List of usage examples for android.view KeyEvent getKeyCode

Introduction

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

Prototype

public final int getKeyCode() 

Source Link

Document

Retrieve the key code of the key event.

Usage

From source file:com.hellofyc.base.app.activity.BaseActivity.java

@Override
public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
    final int keyCode = event.getKeyCode();
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (mPressTwoExit && mIsHighPriority) {
            final int action = event.getAction();
            if (action == KeyEvent.ACTION_UP) {
                pressBackToExit();/*w  ww .j  ava  2 s.com*/
                return true;
            }
        }
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.dwdesign.tweetings.activity.SearchActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    SharedPreferences mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

    if (mPreferences.getBoolean(PREFERENCE_KEY_VOLUME_NAVIGATION, false)) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_UP: {
                Intent broadcast = new Intent();
                broadcast.setAction(BROADCAST_VOLUME_UP);
                sendBroadcast(broadcast);
                //scrollToPrevious();
                return true;
            }/*from w ww .j  a  v a  2s.co  m*/
            case KeyEvent.KEYCODE_VOLUME_DOWN: {
                Intent broadcast = new Intent();
                broadcast.setAction(BROADCAST_VOLUME_DOWN);
                sendBroadcast(broadcast);
                //scrollToNext();
                return true;
            }
            }
        }
        if (event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
                || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)) {
            return true;
        }
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.kinvey.sample.kitchensink.account.LoginFragment.java

protected void addEditListeners() {
    login.setEnabled(validateInput());/*w  w  w. ja  va2 s. c  o  m*/

    username.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            login.setEnabled(validateInput());
        }
    });

    username.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE
                    || (event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    && username.getText().length() < MIN_USERNAME_LENGTH) {

                CharSequence text = "User name must contain at least " + MIN_USERNAME_LENGTH + " characters";
                Toast.makeText(getSherlockActivity().getApplicationContext(), text, Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    });

    password.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            login.setEnabled(validateInput());
        }
    });

    password.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE
                    || (event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    && password.getText().length() < MIN_USERNAME_LENGTH) {
                CharSequence text = "Password must contain at least " + MIN_PASSWORD_LENGTH + " characters";
                Toast.makeText(getSherlockActivity(), text, Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    });

}

From source file:net.olejon.mdapp.CalculatorsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Input manager
    final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
            Context.INPUT_METHOD_SERVICE);

    // Layout//  w  ww.  ja  v a2s.  co  m
    setContentView(R.layout.activity_calculators);

    // Toolbar
    final Toolbar toolbar = (Toolbar) findViewById(R.id.calculators_toolbar);
    toolbar.setTitle(getString(R.string.calculators_title));

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // BMI
    final EditText bmiEditText = (EditText) findViewById(R.id.calculators_bmi_height);
    final TextView bmiButton = (TextView) findViewById(R.id.calculators_bmi_button);

    bmiButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            inputMethodManager.hideSoftInputFromWindow(bmiEditText.getWindowToken(), 0);

            calculateBmi();
        }
    });

    bmiEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_GO || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                inputMethodManager.hideSoftInputFromWindow(bmiEditText.getWindowToken(), 0);

                calculateBmi();

                return true;
            }

            return false;
        }
    });

    // Waist measurement
    final EditText waistMeasurementEditText = (EditText) findViewById(R.id.calculators_waist_measurement);
    final TextView waistMeasurementButton = (TextView) findViewById(R.id.calculators_waist_measurement_button);

    waistMeasurementButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            inputMethodManager.hideSoftInputFromWindow(waistMeasurementEditText.getWindowToken(), 0);

            calculateWaistMeasurement();
        }
    });

    waistMeasurementEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_GO || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                inputMethodManager.hideSoftInputFromWindow(waistMeasurementEditText.getWindowToken(), 0);

                calculateWaistMeasurement();

                return true;
            }

            return false;
        }
    });

    // Corrected QT time
    final EditText correctedQtTimeEditText = (EditText) findViewById(
            R.id.calculators_corrected_qt_time_rr_interval);
    final TextView correctedQtTimeButton = (TextView) findViewById(R.id.calculators_corrected_qt_time_button);

    correctedQtTimeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            inputMethodManager.hideSoftInputFromWindow(correctedQtTimeEditText.getWindowToken(), 0);

            calculateCorrectedQtTime();
        }
    });

    correctedQtTimeEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_GO || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                inputMethodManager.hideSoftInputFromWindow(correctedQtTimeEditText.getWindowToken(), 0);

                calculateCorrectedQtTime();

                return true;
            }

            return false;
        }
    });
}

From source file:org.chromium.chrome.browser.media.remote.ExpandedControllerActivity.java

/**
 * Modify remote volume by handling volume keys.
 *
 * @param controller The remote controller through which the volume will be modified.
 * @param event The key event. Its keycode needs to be either {@code KEYCODE_VOLUME_DOWN} or
 *              {@code KEYCODE_VOLUME_UP} otherwise this method will return false.
 * @return True if the event is handled.
 *//* ww  w . jav a 2 s  .  com*/
private boolean handleVolumeKeyEvent(MediaRouteController controller, KeyEvent event) {
    if (!controller.isBeingCast())
        return false;

    int action = event.getAction();
    int keyCode = event.getKeyCode();
    // Intercept the volume keys to affect only remote volume.
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN)
            controller.setRemoteVolume(-1);
        return true;
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_DOWN)
            controller.setRemoteVolume(1);
        return true;
    default:
        return false;
    }
}

From source file:org.easyaccess.phonedialer.CallingScreen.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (KeyEvent.KEYCODE_VOLUME_UP == event.getKeyCode()) {
        if (Utils.ringing == 1) {
            // announce the caller name/number
            announceCaller(this.callerDetails);
            return true;
        } else if (Utils.ringing == 0 && Utils.off_hook == 1) {
            // activate loudspeaker
            Utils.audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
            if (Utils.audioManager.isSpeakerphoneOn() == false) {
                Utils.audioManager.setSpeakerphoneOn(true);
            } else {
                // deactivate loudspeaker
                Utils.audioManager.setSpeakerphoneOn(false);
            }/*w  w  w. j  av  a  2 s .  c o m*/
            return true;
        }
    } else if (KeyEvent.KEYCODE_VOLUME_DOWN == event.getKeyCode()) {
        if (Utils.ringing == 1) {
            // mute the ringtone
            muteRingtone();
            return true;
        } else if (Utils.ringing == 0 && Utils.off_hook == 1) {

            if (Utils.audioManager.isMicrophoneMute() == true) {
                Utils.audioManager.setMicrophoneMute(false);
            } else {
                Utils.audioManager.setMicrophoneMute(true);
            }
            return true;
        }
    }
    return super.onKeyUp(keyCode, event);
}

From source file:com.bydavy.card.receipts.activities.ReceiptListActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    final MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_receipt_list, menu);
    final MenuItem searchItem = menu.findItem(R.id.menu_search);
    mSearch = (EditText) searchItem.getActionView();
    mSearch.setOnEditorActionListener(new OnEditorActionListener() {
        @Override/*from w w  w  . java2 s.  co  m*/
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((EditorInfo.IME_ACTION_SEARCH == actionId)
                    || ((event != null) && (KeyEvent.KEYCODE_ENTER == event.getKeyCode()))) {
                search(v.getText().toString());
                return true;
            }
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

From source file:com.ririjin.adminmobile.fragment.UpdateBasiceFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.basic_fragment, container, false);// ?

    dialog = new ProgressDialog(getActivity());
    carlicense = ((UpdateCarActivity) getActivity()).gettcarLicense();

    assetIdLinear = (LinearLayout) rootView.findViewById(R.id.assetId_linear);
    assetIdLinear.setVisibility(View.VISIBLE);
    AssetIdText = (TextView) rootView.findViewById(R.id.asset_id);
    AssetIdText.setText(assetId);/* w  w w .j  ava 2  s  .  c  o m*/

    SaveButton = (Button) rootView.findViewById(R.id.save_btn);
    CalculateButton = (Button) rootView.findViewById(R.id.calculate_btn);

    CarLinseceEdit = (EditText) rootView.findViewById(R.id.carlinsece_edit);
    ContractFilter = (EditText) rootView.findViewById(R.id.contract_filter);
    AppraiserAsses = (EditText) rootView.findViewById(R.id.appraiser_asses);

    radioGroup = (RadioGroup) rootView.findViewById(R.id.cartype_radio);
    rZRadioButton = (RadioButton) rootView.findViewById(R.id.rongzi_btn);
    dYRadioButton = (RadioButton) rootView.findViewById(R.id.diya_btn);

    SaveButton.setOnClickListener(listener);
    CalculateButton.setOnClickListener(listener);

    postData = new RJPostData(getActivity().getApplicationContext(), apiUrl);

    CarLinseceEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if (!hasFocus) {
                carlicense = GetCarLincese(CarLinseceEdit.getText().toString());
                ((UpdateCarActivity) getActivity()).setcarLicense(carlicense);
                ContractFilter.setText(SFTP.GetNowTime() + carlicense);
            }
        }
    });

    CarLinseceEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.d(TAG, "" + actionId + "," + event);
            if (actionId == EditorInfo.IME_ACTION_DONE
                    || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                // do something;
                CarLinseceEdit.clearFocus();
                return true;
            }
            return false;
        }
    });
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup arg0, int arg1) {
            // TODO Auto-generated method stub

        }
    });
    dialog.setMessage("?...");
    dialog.setCancelable(false);
    dialog.show();
    GetCarData(assetId);

    return rootView;
}

From source file:lewa.support.v7.app.ActionBarActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() != KeyEvent.ACTION_UP) {
        if (getDelegate().onBackPressed()) {
            return true;
        }/* ww  w.jav  a2  s . co m*/
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.notepadlite.NoteEditActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override//from   w w  w.ja v  a  2 s. co  m
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
    super.dispatchKeyShortcutEvent(event);
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.isCtrlPressed()) {
        NoteEditFragment fragment = (NoteEditFragment) getSupportFragmentManager()
                .findFragmentByTag("NoteEditFragment");
        fragment.dispatchKeyShortcutEvent(event.getKeyCode());

        return true;
    }
    return super.dispatchKeyShortcutEvent(event);
}