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.mobicage.rogerthat.plugins.friends.ServiceSearchActivity.java

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

    setContentView(R.layout.service_search);

    mSearchCategoryLabels = (LinearLayout) findViewById(R.id.search_category);
    mSearchCategoryViewFlipper = (SafeViewFlipper) findViewById(R.id.search_result_lists);

    mSearchInfoByCategory = new HashMap<String, ServiceSearchActivity.SearchInfo>();
    mSearchInfoByListView = new HashMap<AbsListView, ServiceSearchActivity.SearchInfo>();

    mGestureScanner = new GestureDetector(new ViewFlipperSlider(mOnSwipeLeft, mOnSwipeRight));

    final Context ctx = this;
    final EditText editText = (EditText) findViewById(R.id.search_text);
    editText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override//from  w  w  w  . j ava2  s. c om
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN)) {
                if (!TextUtils.isEmptyOrWhitespace(mSearchString)) {
                    UIUtils.hideKeyboard(ServiceSearchActivity.this, v);
                    launchFindServiceCall();
                    return true;
                }
            }
            return false;
        }
    });
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            mSearchString = s.toString();
            clearSearches();
        }
    });

    findViewById(R.id.search_button).setOnClickListener(new SafeViewOnClickListener() {
        @Override
        public void safeOnClick(View v) {
            if (TextUtils.isEmptyOrWhitespace(mSearchString)) {
                mSearchString = "";
            }
            UIUtils.hideKeyboard(ctx, editText);
            clearSearches();
            launchFindServiceCall();
        }
    });
}

From source file:com.activiti.android.ui.fragments.form.picker.IdmPickerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (getArguments() != null) {
        onRetrieveParameters(getArguments());
    }/*from www. j  a  v a2  s  .c  o  m*/

    // Create View
    setRootView(inflater.inflate(R.layout.fr_idm_picker, container, false));

    // Init list
    init(getRootView(), emptyListMessageId);
    gv.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
    setListShown(true);

    searchView = UIUtils.setActionBarCustomView(getActivity(), R.layout.person_picker_header, true);

    View searchBack = searchView.findViewById(R.id.search_back);
    searchBack.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().getSupportFragmentManager().popBackStackImmediate();
        }
    });

    if (keywords != null && !keywords.isEmpty()) {
        search(keywords);
    } else {
        // Speech to Text
        hasTextToSpeech = FeatureUtils.hasSpeechToText(getActivity());
        speechToText = (ImageButton) searchView.findViewById(R.id.search_microphone);
        if (hasTextToSpeech) {
            speechToText.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    speechToText();
                }
            });
        } else {
            speechToText.setVisibility(View.GONE);
        }

        searchAction = (ImageButton) searchView.findViewById(R.id.search_start);
        searchAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchForm.getText().length() > 0) {
                    keywords = searchForm.getText().toString();
                    search(keywords);
                } else {
                    // TODO Snackbar
                }
            }
        });
        searchAction.setVisibility(View.GONE);

        // Init form search
        searchForm = (EditText) searchView.findViewById(R.id.search_query);
        searchForm.requestFocus();
        UIUtils.showKeyboard(getActivity(), searchForm);
        searchForm.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        searchForm.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    searchAction.setVisibility(View.GONE);
                    if (hasTextToSpeech) {
                        speechToText.setVisibility(View.VISIBLE);
                    }
                } else {
                    speechToText.setVisibility(View.GONE);
                    searchAction.setVisibility(View.VISIBLE);
                }
            }
        });

        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)
                                || (actionId == EditorInfo.IME_ACTION_DONE))) {
                    if (searchForm.getText().length() > 0) {
                        keywords = searchForm.getText().toString();
                        search(keywords);
                    } else {
                        // TODO Snackbar
                    }
                    return true;
                }
                return false;
            }
        });
    }

    if (getMode() == MODE_PICK) {
        Button cancel = UIUtils.initCancel(getRootView(), R.string.general_action_cancel);
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getDialog() != null) {
                    getDialog().dismiss();
                } else {
                    getFragmentManager().popBackStack();
                }
            }
        });
    } else {
        hide(R.id.validation_panel);
    }

    return getRootView();
}

From source file:com.yi4all.rupics.ImageDetailActivity.java

@Override
public boolean onKeyDown(int keyCoder, KeyEvent event) {
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        if (popMenuPanel.getVisibility() == View.VISIBLE) {
            popMenuPanel.setVisibility(View.GONE);
            return true;
        } else {/*w  w  w . j  a va 2s. c o m*/
            return super.onKeyDown(keyCoder, event);
        }

    default:
        return false;
    }
}

From source file:com.gao.im.ui.ECLauncherUI.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    LogUtil.d(LogUtil.getLogUtilsTag(ECLauncherUI.class), " onKeyDown");

    if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && event.getAction() == KeyEvent.ACTION_UP) {
        // dismiss PlusSubMenuHelper
        if (mOverflowHelper != null && mOverflowHelper.isOverflowShowing()) {
            mOverflowHelper.dismiss();//from   ww w.  j a va 2 s. c  o  m
            return true;
        }
    }

    // ?menu??
    if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && event.getAction() == KeyEvent.ACTION_DOWN) {
        doTaskToBackEvent();
    }

    try {

        return super.dispatchKeyEvent(event);
    } catch (Exception e) {
        LogUtil.e(LogUtil.getLogUtilsTag(ECLauncherUI.class),
                "dispatch key event catch exception " + e.getMessage());
    }

    return false;
}

From source file:com.google.android.apps.flexbox.FlexItemEditFragment.java

private void setNextFocusesOnEnterDown(final TextView... textViews) {
    // This can be done by setting android:nextFocus* as in 
    // https://developer.android.com/training/keyboard-input/navigation.html
    // But it requires API level 11 as a minimum sdk version. To support the lower level devices,
    // doing it programatically.
    for (int i = 0; i < textViews.length; i++) {
        final int index = i;
        textViews[index].setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override//from   ww w .ja  v a 2 s. co  m
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE
                        || (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN
                                && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    if (index + 1 < textViews.length) {
                        textViews[index + 1].requestFocus();
                    } else if (index == textViews.length - 1) {
                        InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
                                .getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                }
                return true;
            }
        });

        // Suppress the key focus change by KeyEvent.ACTION_UP of the enter key
        textViews[index].setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP;
            }
        });
    }

}

From source file:io.github.yavski.fabspeeddial.FabSpeedDial.java

@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (isMenuOpen() && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP
            && event.getRepeatCount() == 0) {
        closeMenu();/*w  w w  .j  av a 2s .  com*/
        return true;
    }

    return super.dispatchKeyEventPreIme(event);
}

From source file:com.example.castsample.CastSampleActivity.java

/**
 * Processes volume up and volume down actions upon receiving them as key events.
 */// w  ww .j a  v a  2  s .c om
@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 (mMessageStream != null) {
                currentVolume = mMessageStream.getVolume();
                logVIfEnabled(TAG, "Volume up from " + currentVolume);
                if (currentVolume < 1.0) {
                    logVIfEnabled(TAG, "New volume: " + (currentVolume + VOLUME_INCREMENT));
                    onSetVolume(currentVolume + VOLUME_INCREMENT);
                }
            } else {
                Log.e(TAG, "dispatchKeyEvent - volume up - mMPMS==null");
            }
        }

        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            double currentVolume;
            if (mMessageStream != null) {
                currentVolume = mMessageStream.getVolume();
                logVIfEnabled(TAG, "Volume down from: " + currentVolume);
                if (currentVolume > 0.0) {
                    logVIfEnabled(TAG, "New volume: " + (currentVolume - VOLUME_INCREMENT));
                    onSetVolume(currentVolume - VOLUME_INCREMENT);
                }
            } else {
                Log.e(TAG, "dispatchKeyEvent - volume down - mMPMS==null");
            }
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

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

public TextView addSubItem(int id, String text, int icon) {
    if (popupLayout == null) {
        rect = new Rect();
        location = new int[2];
        popupLayout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(getContext());
        popupLayout.setOnTouchListener(new OnTouchListener() {
            @Override/*from w w w.  j a  va2s  . com*/
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                    if (popupWindow != null && popupWindow.isShowing()) {
                        v.getHitRect(rect);
                        if (!rect.contains((int) event.getX(), (int) event.getY())) {
                            popupWindow.dismiss();
                        }
                    }
                }
                return false;
            }
        });
        popupLayout.setDispatchKeyEventListener(new ActionBarPopupWindow.OnDispatchKeyEventListener() {
            @Override
            public void onDispatchKeyEvent(KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0
                        && popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
    }
    TextView textView = new TextView(getContext());
    textView.setTextColor(ContextCompat.getColor(getContext(), R.color.primary_text));
    textView.setBackgroundResource(R.drawable.list_selector);
    if (!LocaleController.isRTL) {
        textView.setGravity(Gravity.CENTER_VERTICAL);
    } else {
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
    }
    textView.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    textView.setMinWidth(AndroidUtilities.dp(196));
    textView.setTag(id);
    textView.setText(text);
    if (icon != 0) {
        textView.setCompoundDrawablePadding(AndroidUtilities.dp(12));
        if (!LocaleController.isRTL) {
            textView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(icon), null, null,
                    null);
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(icon),
                    null);
        }
    }
    popupLayout.setShowedFromBotton(showFromBottom);
    popupLayout.addView(textView);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
    if (LocaleController.isRTL) {
        layoutParams.gravity = Gravity.RIGHT;
    }
    layoutParams.width = LayoutHelper.MATCH_PARENT;
    layoutParams.height = AndroidUtilities.dp(48);
    textView.setLayoutParams(layoutParams);
    textView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (popupWindow != null && popupWindow.isShowing()) {
                if (processedPopupClick) {
                    return;
                }
                processedPopupClick = true;
                popupWindow.dismiss(allowCloseAnimation);
            }
            if (parentMenu != null) {
                parentMenu.onItemClick((Integer) view.getTag());
            } else if (delegate != null) {
                delegate.onItemClick((Integer) view.getTag());
            }
        }
    });
    menuHeight += layoutParams.height;
    return textView;
}

From source file:com.app.azza.ocr.OcrCaptureActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();

    Text text = null;//from  www  .  j  a va  2 s . com
    List mtextList = getAllGraphicsText();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_DOWN) {

            Toast toast = Toast.makeText(this, "volume up pressed", Toast.LENGTH_SHORT);
            toast.show();

            Log.v("upVolume", "up " + lineNumberToSpeak);
            //        for(Object block:textList){
            //             text=(Text)block;
            //
            //            tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT");
            //        }
            if (mtextList.size() > 0) {
                // for (int i = 0; i < textList.size(); i++) {
                if (lineNumberToSpeak < mtextList.size()) {
                    text = (Text) mtextList.get(lineNumberToSpeak);
                    if (text != null && text.getValue() != null) {
                        tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT");
                        if (lineNumberToSpeak < mtextList.size())
                            lineNumberToSpeak++;

                    } else {
                        Log.d(TAG, "text data is null");
                    }
                    // }
                } else {
                    // al list al gdeda lines feha 22al
                    lineNumberToSpeak = 0;
                }
            } else {
                Log.d(TAG, "no text detected");
            }

        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            Toast toast = Toast.makeText(this, "volume down pressed", Toast.LENGTH_SHORT);
            toast.show();
            if (mtextList.size() > 0) {
                // for (int i = 0; i < textList.size(); i++) {
                if (lineNumberToSpeak > -1) {
                    text = (Text) mtextList.get(lineNumberToSpeak);
                    if (text != null && text.getValue() != null) {
                        tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT");
                        if (lineNumberToSpeak > 0)
                            lineNumberToSpeak--;

                    } else {
                        Log.d(TAG, "text data is null");
                    }
                } else {
                    lineNumberToSpeak = 0;
                }
            } else {
                Log.d(TAG, "no text detected");
            }
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

From source file:com.ruesga.rview.BaseActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
            if (getContentBinding().drawerLayout != null && getContentBinding().drawerLayout
                    .getDrawerLockMode(GravityCompat.START) != DrawerLayout.LOCK_MODE_LOCKED_CLOSED) {
                if (getContentBinding().drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    getContentBinding().drawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    getContentBinding().drawerLayout.openDrawer(GravityCompat.START);
                }/*from w  ww  .  j  av a 2s . co m*/
                return true;
            } else if (getOptionsMenu() != null && getOptionsMenu().getMenu() != null
                    && getOptionsMenu().getMenu().size() > 0) {
                if (getContentBinding().drawerLayout.isDrawerOpen(GravityCompat.END)) {
                    closeOptionsDrawer();
                } else {
                    openOptionsDrawer();
                }
                return true;
            }
            break;
        }
    }
    return super.dispatchKeyEvent(event);
}