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.ruesga.rview.SearchActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
            mBinding.searchView.openMenu(true);
            return true;
        }/*from  w  w  w .j av a2 s  . c o m*/
    }

    return super.dispatchKeyEvent(event);
}

From source file:com.trellmor.berrytubechat.ChatActivity.java

@SuppressLint("NewApi")
@Override//  w ww . j av a2 s  . c om
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_chat);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(false);
    }

    mEditChatMsg = (EditText) findViewById(R.id.edit_chat_msg);
    TextView.OnEditorActionListener chatMsgListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendChatMsg();
            } else if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                sendChatMsg();
            }
            return true;
        }
    };
    mEditChatMsg.setOnEditorActionListener(chatMsgListener);

    mTextDrinks = (TextView) findViewById(R.id.text_drinks);
    registerForContextMenu(mTextDrinks);

    mCurrentVideo = (TextView) findViewById(R.id.text_video);
    mCurrentVideo.setMovementMethod(LinkMovementMethod.getInstance());

    mTextNick = (TextView) findViewById(R.id.text_nick);
    mTextNick.setText("Anonymous");

    mListChat = (ListView) findViewById(R.id.list_chat);

    Intent intent = getIntent();
    mUsername = intent.getStringExtra(MainActivity.KEY_USERNAME);
    mPassword = intent.getStringExtra(MainActivity.KEY_PASSWORD);

    if (savedInstanceState != null) {
        mDrinkCount = savedInstanceState.getInt(KEY_DRINKCOUT);
        mMyDrinkCount = savedInstanceState.getInt(KEY_MYDRINKCOUNT);
        if (mUsername == null)
            mUsername = savedInstanceState.getString(MainActivity.KEY_USERNAME);
        if (mPassword == null)
            mPassword = savedInstanceState.getString(MainActivity.KEY_PASSWORD);
    }

    startService(new Intent(this, BerryTube.class));
    bindService(new Intent(this, BerryTube.class), mService, BIND_ABOVE_CLIENT);
}

From source file:com.supremainc.biostar2.main.HomeActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_MENU:
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            mLayout.onDrawMenu();//from   w ww.  j a  va2  s.c  om
        }
        return true;
    }
    return super.dispatchKeyEvent(event);
}

From source file:org.akvo.caddisfly.ui.activity.MainActivity.java

public void onSaveCalibration() {
    final Context context = this;
    final MainApp mainApp = (MainApp) this.getApplicationContext();

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    final EditText input = new EditText(context);
    input.setInputType(InputType.TYPE_CLASS_TEXT);
    input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(22) });

    alertDialogBuilder.setView(input);//from ww w  .  ja  v  a  2  s  . co  m
    alertDialogBuilder.setCancelable(false);

    alertDialogBuilder.setTitle(R.string.saveCalibration);
    alertDialogBuilder.setMessage(R.string.giveNameForCalibration);

    alertDialogBuilder.setPositiveButton(R.string.ok, null);
    alertDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            closeKeyboard(input);
            dialog.cancel();
        }
    });
    final AlertDialog alertDialog = alertDialogBuilder.create(); //create the box

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    if (!input.getText().toString().trim().isEmpty()) {
                        final ArrayList<String> exportList = new ArrayList<String>();

                        for (ColorInfo aColorList : mainApp.colorList) {
                            exportList.add(ColorUtils.getColorRgbString(aColorList.getColor()));
                        }

                        File external = Environment.getExternalStorageDirectory();
                        final String path = external.getPath() + Config.CALIBRATE_FOLDER_NAME;

                        File file = new File(path + input.getText());
                        if (file.exists()) {
                            AlertUtils.askQuestion(context, R.string.overwriteFile, R.string.nameAlreadyExists,
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            FileUtils.saveToFile(path, input.getText().toString(),
                                                    exportList.toString());
                                        }
                                    });
                        } else {
                            FileUtils.saveToFile(path, input.getText().toString(), exportList.toString());
                        }

                        closeKeyboard(input);
                        alertDialog.dismiss();
                    } else {
                        input.setError(getString(R.string.invalidName));
                    }
                }
            });
        }
    });

    input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || (actionId == EditorInfo.IME_ACTION_DONE)) {

            }
            return false;
        }
    });

    alertDialog.show();
    input.requestFocus();
    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

}

From source file:org.profeda.dictionary.SearchActivity.java

/**
 * Called when the activity is first created.
 *//*from ww  w. ja  va  2s  . c  om*/
@Override
public void onCreate(Bundle savedInstanceState) {
    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(SearchActivity.this);

    if (pref.getBoolean("show_changelog", true) == true) {
        MessageBox.showChangeLog(this);
    }

    loadDictionaries(pref);

    if (pref.getBoolean("p_invert_colors", false) == true) {
        setTheme(R.style.Light);
    } else {
        setTheme(R.style.Dark);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    dict = new Dict(this, pref.getString("dictFile", null));

    updateTitleBar();
    readMRU();

    final EditText searchbox = (EditText) findViewById(R.id.searchbox);
    final Button dictselectbutton = (Button) findViewById(R.id.dictselectbutton);
    final ListView listview = (ListView) findViewById(R.id.contentlist);

    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView l, View v, int position, long id) {
            ListAdapter la = ((ListView) l).getAdapter();
            if (la instanceof GenericStringAdapter) {
                final String data = (String) la.getItem(position);
                if (data.startsWith("> ")) {
                    searchbox.setText(data.substring(2));
                    doSearch(true);
                }
            } else if (la instanceof DictListAdapter) {
                final String[] data = (String[]) la.getItem(position);

                final List<Dict> langs = Dict.searchForLanguage(SearchActivity.this, dict.toLang, null);

                String[] menu = new String[langs.size() + 1];
                menu[0] = getString(R.string.copy_translation_to_clipboard);
                for (int i = 1; i < menu.length; i++)
                    menu[i] = String.format(getString(R.string.search_translation_in_language),
                            langs.get(i - 1).toLang);

                Matcher m = Pattern.compile("<b>(.*)<\\/b>.*").matcher(data[1]);
                if (!m.matches())
                    return;
                final String translation = m.group(1).trim();

                new AlertDialog.Builder(SearchActivity.this).setIcon(android.R.drawable.ic_dialog_info)
                        .setTitle(translation).setItems(menu, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                switch (arg1) {
                                case 0:
                                    ClipboardManager cm = (ClipboardManager) getSystemService(
                                            CLIPBOARD_SERVICE);
                                    cm.setText(translation);
                                    break;
                                default:
                                    dict = langs.get(arg1 - 1);
                                    readMRU();
                                    updateTitleBar();
                                    searchbox.setText(translation);
                                    doSearch(true);
                                }
                            }
                        }).show();
            }
        }
    });

    searchbox.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
                    || event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                doSearch(true);
                return true;
            } else {
                return false;
            }
        }
    });

    final Drawable deleteIcon = getResources().getDrawable(R.drawable.textbox_clear);
    //deleteIcon.setBounds(0, 0, deleteIcon.getIntrinsicWidth(), deleteIcon.getIntrinsicHeight());
    deleteIcon.setBounds(0, 0, 51, 30); // wtf??? die Zahlen sind empirisch ermittelt... ab right>=52 springt die hhe des editText

    searchbox.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            searchbox.setCompoundDrawables(null, null, s.toString().equals("") ? null : deleteIcon, null);
            //searchbox.setCompoundDrawablesWithIntrinsicBounds(null, null, s.toString().equals("") ? null : deleteIcon, null);
            searchbox.setCompoundDrawablePadding(0);
        }

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

        @Override
        public void afterTextChanged(Editable s) {
            if (enableLivesearch && s.length() > 1 && dict.filePrefix != null && dictSearchInAction == false) {
                DictionarySearcher ds = new DictionarySearcher();
                ds.targetList = (ListView) findViewById(R.id.contentlist);
                ds.execute(dict.filePrefix, s.toString());
            }
            if (s.length() == 0) {
                displayMRU();
            }
        }
    });

    //searchbox.setCompoundDrawables(null, null, deleteIcon, null);
    searchbox.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (searchbox.getCompoundDrawables()[2] == null) {
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_UP) {
                return false;
            }
            if (event.getX() > searchbox.getWidth() - searchbox.getPaddingRight()
                    - deleteIcon.getIntrinsicWidth()) {
                searchbox.setText("");
                searchbox.setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
    });

    dictselectbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectDictionary();
        }
    });

    ((Button) findViewById(R.id.dictswapbutton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            final List<Dict> langs = Dict.searchForLanguage(SearchActivity.this, dict.toLang, dict.fromLang);
            if (langs.size() > 0) {
                dict = langs.get(0);
                readMRU();
                updateTitleBar();
                searchbox.setText("");
            } else {
                MessageBox.alert(SearchActivity.this, getString(R.string.no_reverse_dict_found));
            }
        }
    });

}

From source file:com.example.angel.parkpanda.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        switch (event.getAction()) {
        case KeyEvent.ACTION_DOWN:
            if (event.getDownTime() - lastPressedTime < PERIOD) {
                System.exit(0);//  www  .  j  a v a  2 s  .  c  om
                finish();

            } else {
                Toast.makeText(getApplicationContext(), "Press again to exit.", Toast.LENGTH_SHORT).show();
                lastPressedTime = event.getEventTime();
            }
            return true;
        }
    }
    return false;
}

From source file:com.notepadlite.MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override/*from   ww w  .  j av  a  2  s. c o  m*/
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
    super.dispatchKeyShortcutEvent(event);
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.isCtrlPressed()) {
        if (getSupportFragmentManager().findFragmentById(R.id.noteViewEdit) instanceof NoteListFragment) {
            NoteListFragment fragment = (NoteListFragment) getSupportFragmentManager()
                    .findFragmentByTag("NoteListFragment");
            fragment.dispatchKeyShortcutEvent(event.getKeyCode());
        } else if (getSupportFragmentManager()
                .findFragmentById(R.id.noteViewEdit) instanceof NoteViewFragment) {
            NoteViewFragment fragment = (NoteViewFragment) getSupportFragmentManager()
                    .findFragmentByTag("NoteViewFragment");
            fragment.dispatchKeyShortcutEvent(event.getKeyCode());
        } else if (getSupportFragmentManager()
                .findFragmentById(R.id.noteViewEdit) instanceof NoteEditFragment) {
            NoteEditFragment fragment = (NoteEditFragment) getSupportFragmentManager()
                    .findFragmentByTag("NoteEditFragment");
            fragment.dispatchKeyShortcutEvent(event.getKeyCode());
        } else if (getSupportFragmentManager().findFragmentById(R.id.noteViewEdit) instanceof WelcomeFragment) {
            WelcomeFragment fragment = (WelcomeFragment) getSupportFragmentManager()
                    .findFragmentByTag("NoteListFragment");
            fragment.dispatchKeyShortcutEvent(event.getKeyCode());
        }

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

From source file:org.mariotaku.twidere.activity.ComposeActivity.java

@Override
public boolean onEditorAction(final TextView view, final int actionId, final KeyEvent event) {
    if (event == null)
        return false;
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_ENTER: {
        send();//from ww  w . j a  v a 2  s.c o  m
        return true;
    }
    }
    return false;
}

From source file:com.example.android.home.Home.java

@SuppressLint({ "NewApi", "NewApi" })
@Override//from   w ww  .  j a v  a  2 s. c  o  m
public boolean dispatchKeyEvent(KeyEvent event) {
    Toast.makeText(getApplicationContext(), "dispatchKeyEvent", Toast.LENGTH_SHORT).show();

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            mBackDown = true;
            Toast.makeText(getApplicationContext(), "BACK 1", Toast.LENGTH_SHORT).show();
            myPager.setCurrentItem(HomePagerAdapter.SCREEN_CENTER);
            return true;
        case KeyEvent.KEYCODE_HOME:
            mHomeDown = true;
            //myPager.setCurrentItem(HomePagerAdapter.SCREEN_CENTER); 
            //myPager.invalidate();
            Toast.makeText(getApplicationContext(), "HOME", Toast.LENGTH_SHORT).show();
            Log.e("PUSH", "Home 2");
            return true;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            if (!event.isCanceled()) {
                // Do BACK behavior.
                Toast.makeText(getApplicationContext(), "BACK 2", Toast.LENGTH_SHORT).show();
            }
            mBackDown = true;
            return true;
        case KeyEvent.KEYCODE_HOME:
            Toast.makeText(getApplicationContext(), "HOME", Toast.LENGTH_SHORT).show();
            Log.e("PUSH", "Home 2");
            if (!event.isCanceled()) {
                // Do HOME behavior.
                //myPager.setCurrentItem(HomePagerAdapter.SCREEN_CENTER); 
                Toast.makeText(getApplicationContext(), "HOME", Toast.LENGTH_SHORT).show();
                Log.e("PUSH", "Home 2");
            }
            mHomeDown = true;
            return true;
        }
    }

    return super.dispatchKeyEvent(event);
}

From source file:com.supremainc.biostar2.main.HomeActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
            mLayout.onDrawMenu();/*w  ww  . j av  a2s.c o m*/
            return true;
        case KeyEvent.KEYCODE_BACK:
            if (mFragment != null && mFragment.onBack()) {
                return true;
            }
        default:
            break;
        }
    }
    return super.onKeyDown(keyCode, event);

}