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.letsgood.sampleapp.ChatActivity.java

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

    if (Synergykit.getLoggedUser() != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        sendMessage(v);/*from w w  w .j a v  a2 s .c o  m*/
    }
    return false;

}

From source file:edu.stanford.mobisocial.dungbeetle.ui.fragments.AppsViewFragment.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
        if (!event.isTracking()) {
            return true;
        }/*from  www.  j  a v  a  2 s  .  co m*/
        if (!event.isLongPress()) {
            AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            audio.adjustVolume(AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;
        }
    }
    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if (!event.isTracking()) {
            return true;
        }
        if (!event.isLongPress()) {
            AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            audio.adjustVolume(AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;
        }
    }
    return false;
}

From source file:com.appsimobile.appsii.Sidebar.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        closeSidebar();//from w  w w. ja v  a 2  s . co  m
        return true;
    }
    return super.onKeyUp(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 . j av  a  2  s .  c o  m
            finish();
        } else
            Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
        // System.exit(0);
        preTime = System.currentTimeMillis(); //  
    }
    return true;// 
}

From source file:com.anjalimacwan.activity.NoteEditActivity.java

@Override
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;
    }//from ww  w .j  a  v a 2 s . c o m
    return super.dispatchKeyShortcutEvent(event);
}

From source file:com.darly.im.ui.CCPActivityBase.java

/**
 *
 * @param keyCode// w  w w  .  j a  v  a 2 s  . c o  m
 * @param event
 * @return
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        if (streamVolume >= mMusicMaxVolume) {
            LogUtil.d(LogUtil.getLogUtilsTag(BaseFragment.class), "has set the max volume");
            return true;
        }

        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }

        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume + mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }

        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume - mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }
    return false;
}

From source file:org.xbmc.kore.ui.SendTextDialogFragment.java

/**
 * Create the dialog//from   w  w  w .  ja  v a  2s  .c  o m
 * @param savedInstanceState Saved state
 * @return Created dialog
 */
@Override
@SuppressWarnings("InflateParams")
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final String title = getArguments().getString(TITLE_KEY, getString(R.string.send_text));
    View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_send_text, null);

    textToSend = (EditText) dialogView.findViewById(R.id.text_to_send);
    finishAfterSend = (CheckBox) dialogView.findViewById(R.id.send_text_done);

    builder.setTitle(title).setView(dialogView)
            .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    mListener.onSendTextFinished(textToSend.getText().toString(), finishAfterSend.isChecked());
                }
            }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mListener.onSendTextCancel();
                }
            });

    final Dialog dialog = builder.create();
    textToSend.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
    textToSend.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                onSendTextFinished();
            } // handles enter key on external keyboard, issue #99
            else if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED
                    && (event != null && event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                onSendTextFinished();
            }
            dialog.dismiss();
            return false;
        }

        private void onSendTextFinished() {
            mListener.onSendTextFinished(textToSend.getText().toString(), finishAfterSend.isChecked());
        }
    });
    return dialog;
}

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // long press of power button will end the call
    if (KeyEvent.KEYCODE_POWER == event.getKeyCode()) {
        TelephonyManager telephony = (TelephonyManager) getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        try {/*from  w  w  w .  j  a v  a  2  s.co  m*/
            Class<?> c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            ITelephony telephonyService = (ITelephony) m.invoke(telephony);
            telephonyService.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        // do nothing
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:org.xbmc.kore.ui.generic.SendTextDialogFragment.java

/**
 * Create the dialog/*w  w  w  . j  a v a2 s .c o m*/
 * @param savedInstanceState Saved state
 * @return Created dialog
 */
@NonNull
@Override
@SuppressWarnings("InflateParams")
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final String title = getArguments().getString(TITLE_KEY, getString(R.string.send_text));
    View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_send_text, null);

    textToSend = (EditText) dialogView.findViewById(R.id.text_to_send);
    finishAfterSend = (CheckBox) dialogView.findViewById(R.id.send_text_done);

    builder.setTitle(title).setView(dialogView)
            .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    mListener.onSendTextFinished(textToSend.getText().toString(), finishAfterSend.isChecked());
                }
            }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mListener.onSendTextCancel();
                }
            });

    final Dialog dialog = builder.create();
    textToSend.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
    textToSend.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                onSendTextFinished();
            } // handles enter key on external keyboard, issue #99
            else if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED
                    && (event != null && event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                onSendTextFinished();
            }
            dialog.dismiss();
            return false;
        }

        private void onSendTextFinished() {
            mListener.onSendTextFinished(textToSend.getText().toString(), finishAfterSend.isChecked());
        }
    });
    return dialog;
}

From source file:de.vanita5.twittnuker.fragment.support.DirectMessagesConversationFragment.java

@Override
public boolean onEditorAction(final TextView view, final int actionId, final KeyEvent event) {
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_ENTER: {
        sendDirectMessage();//from w w  w  . ja  va  2s.c  om
        return true;
    }
    }
    return false;
}