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:android.car.cluster.sample.MainClusterActivity.java

@Override
public void onKeyEvent(KeyEvent event) {
    Log.i(TAG, "onKeyEvent, event: " + event);
    dispatchKeyEvent(event); // TODO: dispatch event doesn't work for some reason.

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
            int nextItem = (mPager.getCurrentItem() + 1) % mButtonToFacet.size();
            mOrderToFacet.get(nextItem).button.requestFocus();
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
            int nextItem = (mPager.getCurrentItem() - 1);
            if (nextItem < 0)
                nextItem = mButtonToFacet.size() - 1;
            mOrderToFacet.get(nextItem).button.requestFocus();
        }//from   w  w w .java 2 s .c  o m
    }
}

From source file:com.doctoror.fuckoffmusicplayer.presentation.playback.PlaybackAndroidService.java

private void onActionMediaButton(@NonNull final Intent intent) {
    final KeyEvent keyEvent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (keyEvent != null && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            onActionPlayPause();/*from   ww w.  ja va 2  s.  c  o m*/
            break;

        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            onActionPause();
            break;

        case KeyEvent.KEYCODE_MEDIA_NEXT:
            onActionNext();
            break;

        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            onActionPrev();
            break;

        case KeyEvent.KEYCODE_MEDIA_STOP:
            onActionStop();
            break;

        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            onActionPlayPause();
            break;
        }
    }
}

From source file:de.da_sense.moses.client.LoginActivity.java

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

    mContext = this;

    getActionBar().hide();/*from  w w w.  j a v a 2 s.co  m*/

    setContentView(R.layout.login);

    lg = this;
    editTextEmail = (EditText) findViewById(R.id.login_email);
    editTextPassword = (EditText) findViewById(R.id.login_password);
    checkBoxRemember = (CheckBox) findViewById(R.id.checkbox_login_remember_me);
    buttonLogin = (Button) findViewById(R.id.login_button);

    // set the previously persisted credentials if any
    try {
        String[] credentials = readCredentials();
        if (credentials != null) {
            editTextEmail.setText(credentials[0]);
            editTextPassword.setText(credentials[1]);
        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "onCreate: file containing users credentials could not be read");
        e.printStackTrace();
    }

    // set the focus and cursor on an empty field (email or password)
    if (editTextEmail.getText().toString().isEmpty())
        editTextEmail.requestFocus();
    else if (editTextPassword.getText().toString().isEmpty())
        editTextPassword.requestFocus();

    /*
     * When user clicks the "Go" button on keyboard and the cursor is set on the password fields,
     * it should be equivalent to pressing the login button
     */
    editTextPassword.setOnEditorActionListener(new OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || (actionId == EditorInfo.IME_ACTION_DONE)) {
                lg.handleClick(buttonLogin);
            }
            return false;
        }
    });
}

From source file:com.ririjin.adminmobile.fragment.BasicFragment.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());
    assetIdLinear = (LinearLayout) rootView.findViewById(R.id.assetId_linear);
    assetIdLinear.setVisibility(View.VISIBLE);

    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);
    CarFactorAssess = (EditText) rootView.findViewById(R.id.car_factor_assess);
    AppraiserAsses = (EditText) rootView.findViewById(R.id.appraiser_asses);

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

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

    CarLinseceEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override//from   w  ww  . java 2  s. c  om
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if (!hasFocus) {
                if (CarLinseceEdit.getText().toString().length() != 7) {
                    Toast.makeText(getActivity(), "???", Toast.LENGTH_SHORT)
                            .show();
                    ContractFilter.setText("");
                } else {

                    carlicense = GetCarLincese(CarLinseceEdit.getText().toString());
                    ((AddCarAcitivity) 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;
        }
    });

    return rootView;
}

From source file:hku.fyp14017.blencode.ui.fragment.FormulaEditorVariableListFragment.java

@Override
public void onListItemClick(int position) {
    Log.d("catroid", "onListItemClick");
    if (!inContextMode) {
        FormulaEditorFragment formulaEditor = (FormulaEditorFragment) getSherlockActivity()
                .getSupportFragmentManager()
                .findFragmentByTag(FormulaEditorFragment.FORMULA_EDITOR_FRAGMENT_TAG);
        if (formulaEditor != null) {
            formulaEditor.addUserVariableToActiveFormula(adapter.getItem(position).getName());
            formulaEditor.updateButtonViewOnKeyboard();
        }//  w ww.j a  va  2 s  .  co  m
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
        onKey(null, keyEvent.getKeyCode(), keyEvent);
    }

}

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

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

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

    // Layout//from ww w .ja  v a  2s.  com
    setContentView(R.layout.activity_clinicaltrials);

    // Toolbar
    final Toolbar toolbar = (Toolbar) findViewById(R.id.clinicaltrials_toolbar);
    toolbar.setTitle(getString(R.string.clinicaltrials_title));

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

    mToolbarSearchLayout = (LinearLayout) findViewById(R.id.clinicaltrials_toolbar_search_layout);
    mToolbarSearchEditText = (EditText) findViewById(R.id.clinicaltrials_toolbar_search);

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

                search(mToolbarSearchEditText.getText().toString().trim());

                return true;
            }

            return false;
        }
    });

    ImageButton imageButton = (ImageButton) findViewById(R.id.clinicaltrials_toolbar_clear_search);

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mToolbarSearchEditText.setText("");
        }
    });

    // List
    mListView = (ListView) findViewById(R.id.clinicaltrials_list);

    View listViewEmpty = findViewById(R.id.clinicaltrials_list_empty);
    mListView.setEmptyView(listViewEmpty);

    View listViewHeader = getLayoutInflater().inflate(R.layout.activity_clinicaltrials_list_subheader,
            mListView, false);
    mListView.addHeaderView(listViewHeader, null, false);

    // Floating action button
    mFloatingActionButton = (FloatingActionButton) findViewById(R.id.clinicaltrials_fab);

    mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mToolbarSearchLayout.getVisibility() == View.VISIBLE) {
                mInputMethodManager.hideSoftInputFromWindow(mToolbarSearchEditText.getWindowToken(), 0);

                search(mToolbarSearchEditText.getText().toString().trim());
            } else {
                mToolbarSearchLayout.setVisibility(View.VISIBLE);
                mToolbarSearchEditText.requestFocus();

                mInputMethodManager.showSoftInput(mToolbarSearchEditText, 0);
            }
        }
    });
}

From source file:de.questmaster.fatremote.fragments.RemoteFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    /* start audio */
    mAudioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.loadSoundEffects();//from   w w  w.  ja va 2  s .c o  m

    /* read Settings */
    mSettings.readSettings(c);

    /* setup onClickListener for remote buttons */
    ((ImageView) c.findViewById(R.id.remote_button_power)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_up)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_home)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_left)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_ok)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_right)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_back)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_down)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_info)).setOnClickListener(this);

    ((ImageView) c.findViewById(R.id.textButton)).setOnClickListener(this);

    ((ImageView) c.findViewById(R.id.remote_button_rew)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_menu)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_fwd)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_prev)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_play)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_next)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_volup)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_stop)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_zoomup)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_voldown)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_mute)).setOnClickListener(this);
    ((ImageView) c.findViewById(R.id.remote_button_zoomdown)).setOnClickListener(this);

    /* setup key listener */
    EditText text = (EditText) c.findViewById(R.id.enterText);
    text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                hideKeyboard();

                return true;
            }
            return false;
        }
    });
    text.addTextChangedListener(determineKeyboardTextChangedListener());
    text.setFocusable(true);

    // check if WIFI available
    if (!NetworkProxy.getInstance(c).isWifiEnabled()) {
        Toast.makeText(c, R.string.app_err_enwifi, Toast.LENGTH_LONG).show();
    }
}

From source file:singh.amandeep.musicplayer.MediaButtonIntentReceiver.java

/**
 * {@inheritDoc}//from ww  w.j a v a 2s . co  m
 */
@Override
public void onReceive(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
        startService(context, MusicService.CMDPAUSE);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventtime = event.getEventTime();

        String command = null;
        switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MusicService.CMDSTOP;
            break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MusicService.CMDTOGGLEPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MusicService.CMDNEXT;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MusicService.CMDPREVIOUS;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MusicService.CMDPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MusicService.CMDPLAY;
            break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (mDown) {
                    if (MusicService.CMDTOGGLEPAUSE.equals(command) || MusicService.CMDPLAY.equals(command)) {
                        if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
                            acquireWakeLockAndSendMessage(context,
                                    mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0);
                        }
                    }
                } else if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventtime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0,
                                context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventtime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    mLaunched = false;
                    mDown = true;
                }
            } else {
                mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
                mDown = false;
            }
            if (isOrderedBroadcast()) {
                abortBroadcast();
            }
            releaseWakeLockIfHandlerIdle();
        }
    }
}

From source file:com.av.remusic.receiver.MediaButtonIntentReceiver.java

@Override
public void onReceive(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
        if (true)
            startService(context, MediaService.CMDPAUSE);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }/*from ww  w. j a va 2 s  . c o  m*/

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventtime = event.getEventTime();

        String command = null;
        switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MediaService.CMDSTOP;
            break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MediaService.CMDTOGGLEPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MediaService.CMDNEXT;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MediaService.CMDPREVIOUS;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MediaService.CMDPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MediaService.CMDPLAY;
            break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (mDown) {
                    if (MediaService.CMDTOGGLEPAUSE.equals(command) || MediaService.CMDPLAY.equals(command)) {
                        if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
                            acquireWakeLockAndSendMessage(context,
                                    mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0);
                        }
                    }
                } else if (event.getRepeatCount() == 0) {

                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventtime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG)
                            Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0,
                                context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventtime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    mLaunched = false;
                    mDown = true;
                }
            } else {
                mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
                mDown = false;
            }
            if (isOrderedBroadcast()) {
                abortBroadcast();
            }
            releaseWakeLockIfHandlerIdle();
        }
    }
}

From source file:com.battlelancer.seriesguide.ui.MoviesSearchFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_movies_search, container, false);
    ButterKnife.inject(this, v);

    // setup grid view
    resultsGridView.setEmptyView(emptyView);
    resultsGridView.setOnItemClickListener(this);

    // setup search box
    searchBox.setThreshold(1);/*from   w w w. j a  va  2s.c  o  m*/
    searchBox.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH
                    || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                search();
                return true;
            }
            return false;
        }
    });
    searchBox.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ((AutoCompleteTextView) v).showDropDown();
        }
    });
    searchBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            search();
        }
    });
    // set in code as XML is overridden
    searchBox.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    searchBox.setInputType(EditorInfo.TYPE_CLASS_TEXT);

    // setup clear button
    clearButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            searchBox.setText(null);
            searchBox.requestFocus();
        }
    });

    setProgressVisible(false, false);

    return v;
}