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.piusvelte.webcaster.MainActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_DOWN) {
            double currentVolume;

            if (mediaProtocolMessageStream != null) {
                currentVolume = mediaProtocolMessageStream.getVolume();

                if (currentVolume < 1.0) {
                    onSetVolume(currentVolume + VOLUME_INCREMENT);
                }/* www.ja  va  2  s.co  m*/
            } else {
                Log.e(TAG, "dispatchKeyEvent - volume up - mMPMS==null");
            }
        }

        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            double currentVolume;

            if (mediaProtocolMessageStream != null) {
                currentVolume = mediaProtocolMessageStream.getVolume();

                if (currentVolume > 0.0) {
                    onSetVolume(currentVolume - VOLUME_INCREMENT);
                }
            } else {
                Log.e(TAG, "dispatchKeyEvent - volume down - mMPMS==null");
            }
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

From source file:com.mozilla.simplepush.simplepushdemoapp.MainActivity.java

/** Initialize the app from the saved state
 *
 * @param savedInstanceState The previously stored instance
 *///from ww w.j a v  a  2  s  . c  o  m
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Properties config;
    try {
        config = loadProperties();
        SENDER_ID = config.getProperty("sender_id");
        if (SENDER_ID == null) {
            Log.e(TAG, "sender_id not definied in configuration file. Aborting");
            return;
        }
    } catch (IOException x) {
        Log.e(TAG, "Could not load properties");
        return;
    }
    setContentView(R.layout.activity_main);

    // Set the convenience globals.
    mDisplay = (TextView) findViewById(R.id.display);
    hostUrl = (EditText) findViewById(R.id.host_edit);
    pingData = (EditText) findViewById(R.id.message);
    sendButton = (Button) findViewById(R.id.send);
    connectButton = (Button) findViewById(R.id.connect);

    context = getApplicationContext();
    // Check that GCM is available on this device.
    if (checkPlayServices()) {
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(context);
    } else {
        Log.i(TAG, "No valid Google Play Services APK found");
    }

    // detect the "enter/submit" key for the editor views.
    hostUrl.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            // yes, be very careful about this, else you can send multiple actions.
            if (actionId == EditorInfo.IME_ACTION_SEND || (actionId == EditorInfo.IME_ACTION_UNSPECIFIED
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN)) {
                registerInBackground();
                handled = true;
            }
            return handled;
        }
    });

    pingData.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND || (actionId == EditorInfo.IME_ACTION_UNSPECIFIED
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN)) {
                SendNotification(getMessage());
                handled = true;
            }
            return handled;
        }
    });
}

From source file:com.markupartist.sthlmtraveling.PlaceSearchActivity.java

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

    setContentView(R.layout.place_search_layout);

    registerScreen("Place Search");

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        if (extras.containsKey(ARG_ONLY_STOPS)) {
            mSearchOnlyStops = extras.getBoolean(ARG_ONLY_STOPS);
        }//from ww  w  .  j a  va  2  s.co m
    }

    initGoogleApiClient(false);

    createSearchHandler();

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey(STATE_SEARCH_FILTER)) {
            mCurrentSearchFilterType = savedInstanceState.getInt(STATE_SEARCH_FILTER);
        }
    }

    mHistoryDbAdapter = new HistoryDbAdapter(this).open();

    ImageButton backButton = (ImageButton) findViewById(R.id.search_back);
    ViewHelper.tint(backButton, ContextCompat.getColor(this, R.color.primary_dark));
    ViewHelper.flipIfRtl(backButton);

    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    mSearchEdit = (EditText) findViewById(R.id.search_edit);
    mSearchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || (actionId == EditorInfo.IME_ACTION_DONE)) {
                mSearchResultAdapter.getFilter().filter(mSearchEdit.getText());
            }
            return false;
        }
    });
    mSearchEdit.requestFocus();

    mClearButton = (ImageButton) findViewById(R.id.search_clear);
    ViewHelper.tintIcon(mClearButton.getDrawable(), Color.GRAY);
    mClearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSearchEdit.setText("");
            mSearchResultAdapter.clear();
        }
    });

    mSearchFailed = findViewById(R.id.search_result_error);
    mProgressBar = (ContentLoadingProgressBar) findViewById(R.id.search_progress_bar);

    setupHistoryViews();
    if (!mSearchOnlyStops) {
        getSupportLoaderManager().initLoader(LOADER_HISTORY, null, this);
    }
    setupSearchResultViews();

    if (!shouldSearchGooglePlaces()) {
        setSearchFilter(FILTER_TYPE_STHLM_TRAVELING);
    }

    mMyLocationManager = new LocationManager(this, getGoogleApiClient());
    mMyLocationManager.setLocationListener(this);
    mMyLocationManager.setAccuracy(false);
    registerPlayService(mMyLocationManager);
    if (!mSearchOnlyStops) {
        verifyLocationPermission();
        mMyLocationManager.requestLocation();
    }
}

From source file:net.lacolaco.smileessence.activity.MainActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return super.dispatchKeyEvent(event);
    }/* w  ww .  j  av  a  2s. c  om*/
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_BACK: {
        finish();
        return false;
    }
    default: {
        return super.dispatchKeyEvent(event);
    }
    }
}

From source file:com.android.tv.settings.accessories.AddAccessoryActivity.java

@Override
public void onNewIntent(Intent intent) {
    if (ACTION_CONNECT_INPUT.equals(intent.getAction())
            && (intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) == 0) {
        // We were the front most app and we got a new intent.
        // If screen saver is going, stop it.
        try {//from w  ww  .  ja va 2 s  .co  m
            if (mDreamManager != null && mDreamManager.isDreaming()) {
                mDreamManager.awaken();
            }
        } catch (RemoteException e) {
            // Do nothing.
        }

        KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_PAIRING) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                onHwKeyEvent(false);
            } else if (event.getAction() == KeyEvent.ACTION_DOWN) {
                onHwKeyEvent(true);
            }
        }
    } else {
        setIntent(intent);
    }
}

From source file:org.smilec.smile.ui.GeneralActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_BACK:
        AlertDialog.Builder builderBack = new AlertDialog.Builder(GeneralActivity.this);
        builderBack.setMessage("Are you sure you want to exit? This operation will reset the game and exit.")
                .setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override//from w  w w .ja  v a 2 s  .  c  om
                    public void onClick(DialogInterface dialog, int id) {
                        try {
                            new SmilePlugServerManager().resetGame(ip, GeneralActivity.this);
                        } catch (NetworkErrorException e) {
                            Log.e(Constants.LOG_CATEGORY, "Error: ", e);
                        }

                        // QuestionsManager.resetListOfDeletedQuestions(GeneralActivity.this);
                        GeneralActivity.this.finish();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alertBack = builderBack.create();
        alertBack.show();

        break;
    }

    return super.onKeyDown(keyCode, event);
}

From source file:org.alfresco.mobile.android.application.fragments.search.SearchFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setRetainInstance(true);/*from  ww  w  .jav  a 2s  .co m*/

    setRootView(inflater.inflate(R.layout.app_search_history, container, false));
    init(getRootView(), emptyListMessageId);

    // Folder path
    if (getArguments() != null && getArguments().containsKey(ARGUMENT_FOLDER)) {
        pathView = (TextView) viewById(R.id.search_path);
        show(R.id.search_path_panel);
        displayPathOption();
    } else {
        hide(R.id.search_path_panel);
    }

    // Search Icon
    searchIcon = (ImageView) viewById(R.id.search_icon);

    // Search Input
    searchForm = (EditText) viewById(R.id.search_query);
    searchForm.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    searchForm.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null && (event.getAction() == KeyEvent.ACTION_DOWN)
                    && ((actionId == EditorInfo.IME_ACTION_SEARCH)
                            || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))) {
                if (searchForm.getText().length() > 0) {
                    search(searchForm.getText().toString());
                    UIUtils.hideKeyboard(getActivity());
                } else {
                    AlfrescoNotificationManager.getInstance(getActivity())
                            .showLongToast(getString(R.string.search_form_hint));
                }
                return true;
            }
            return false;
        }
    });

    // Speech to Text
    Boolean hasTextToSpeech = ActionUtils.hasSpeechToText(getActivity());
    ImageButton speechToText = (ImageButton) viewById(R.id.search_microphone);
    if (hasTextToSpeech) {
        speechToText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                speechToText();
            }
        });
    } else {
        hide(R.id.search_microphone);
    }

    return getRootView();
}

From source file:org.telegram.ui.ActionBar.ActionBarMenuItem.java

public ActionBarMenuItem setIsSearchField(boolean value) {
    if (parentMenu == null) {
        return this;
    }//  w  w  w.j av  a2 s  . c o  m
    if (value && searchContainer == null) {
        searchContainer = new FrameLayout(getContext());
        parentMenu.addView(searchContainer, 0);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) searchContainer.getLayoutParams();
        layoutParams.weight = 1;
        layoutParams.width = 0;
        layoutParams.height = LayoutHelper.MATCH_PARENT;
        layoutParams.leftMargin = AndroidUtilities.dp(6);
        searchContainer.setLayoutParams(layoutParams);
        searchContainer.setVisibility(GONE);

        searchField = new EditText(getContext());
        searchField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        searchField.setHintTextColor(0x88ffffff);
        searchField.setTextColor(0xffffffff);
        searchField.setSingleLine(true);
        searchField.setBackgroundResource(0);
        searchField.setPadding(0, 0, 0, 0);
        int inputType = searchField.getInputType() | EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
        searchField.setInputType(inputType);
        searchField.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public void onDestroyActionMode(ActionMode mode) {

            }

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        });
        searchField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (/*actionId == EditorInfo.IME_ACTION_SEARCH || */event != null
                        && (event.getAction() == KeyEvent.ACTION_UP
                                && event.getKeyCode() == KeyEvent.KEYCODE_SEARCH
                                || event.getAction() == KeyEvent.ACTION_DOWN
                                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    AndroidUtilities.hideKeyboard(searchField);
                    if (listener != null) {
                        listener.onSearchPressed(searchField);
                    }
                }
                return false;
            }
        });
        searchField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (listener != null) {
                    listener.onTextChanged(searchField);
                }
                if (clearButton != null) {
                    clearButton.setAlpha(s == null || s.length() == 0 ? 0.6f : 1.0f);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        try {
            Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
            mCursorDrawableRes.setAccessible(true);
            mCursorDrawableRes.set(searchField, R.drawable.search_carret);
        } catch (Exception e) {
            //nothing to do
        }
        searchField.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_ACTION_SEARCH);
        searchField.setTextIsSelectable(false);
        searchContainer.addView(searchField);
        FrameLayout.LayoutParams layoutParams2 = (FrameLayout.LayoutParams) searchField.getLayoutParams();
        layoutParams2.width = LayoutHelper.MATCH_PARENT;
        layoutParams2.gravity = Gravity.CENTER_VERTICAL;
        layoutParams2.height = AndroidUtilities.dp(36);
        layoutParams2.rightMargin = AndroidUtilities.dp(48);
        searchField.setLayoutParams(layoutParams2);

        clearButton = new ImageView(getContext());
        clearButton.setImageResource(R.drawable.ic_close_white);
        clearButton.setScaleType(ImageView.ScaleType.CENTER);
        clearButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                searchField.setText("");
                searchField.requestFocus();
                AndroidUtilities.showKeyboard(searchField);
            }
        });
        searchContainer.addView(clearButton);
        layoutParams2 = (FrameLayout.LayoutParams) clearButton.getLayoutParams();
        layoutParams2.width = AndroidUtilities.dp(48);
        layoutParams2.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        layoutParams2.height = LayoutHelper.MATCH_PARENT;
        clearButton.setLayoutParams(layoutParams2);
    }
    isSearchField = value;
    return this;
}

From source file:com.paic.zhifu.wallet.activity.modules.register.PhoneValidationActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        backBtnFunction();/*from   w  ww  .j  a  va2 s.c  o m*/
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.mobilevue.vod.VideoControllerView.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mPlayer == null) {
        return true;
    }//  ww w  .j av  a2s .  c  o  m

    int keyCode = event.getKeyCode();
    final boolean uniqueDown = event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_DOWN;
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
            || keyCode == KeyEvent.KEYCODE_SPACE) {
        if (uniqueDown) {
            doPauseResume();
            show(sDefaultTimeout);
            if (mPauseButton != null) {
                mPauseButton.requestFocus();
            }
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
        if (uniqueDown && !mPlayer.isPlaying()) {
            mPlayer.start();
            updatePausePlay();
            show(sDefaultTimeout);
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
        if (uniqueDown && mPlayer.isPlaying()) {
            mPlayer.pause();
            updatePausePlay();
            show(sDefaultTimeout);
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP
            || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
        // don't show the controls for volume adjustment
        return super.dispatchKeyEvent(event);
    } else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
        if (uniqueDown) {
            hide();
        }
        return true;
    }

    show(sDefaultTimeout);
    return super.dispatchKeyEvent(event);
}