Example usage for android.view KeyEvent ACTION_DOWN

List of usage examples for android.view KeyEvent ACTION_DOWN

Introduction

In this page you can find the example usage for android.view KeyEvent ACTION_DOWN.

Prototype

int ACTION_DOWN

To view the source code for android.view KeyEvent ACTION_DOWN.

Click Source Link

Document

#getAction value: the key has been pressed down.

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  ava  2  s  .c o  m*/
    }

    return super.dispatchKeyEvent(event);
}

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);//from  w w w  . j  a v  a 2 s .  co m
                finish();

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

From source file:org.puder.trs80.EmulatorActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (gameController.dispatchKeyEvent(event)) {
        return true;
    }/*from w  ww  .ja v a 2 s . c om*/
    boolean consumed = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
        consumed = keyboardManager.keyDown(event);
    }
    if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
        consumed = keyboardManager.keyUp(event);
    }
    return consumed || 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. jav  a2  s .c  o 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);
}

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

@SuppressLint({ "NewApi", "NewApi" })
@Override//from w  w  w .  j  a v a2  s.co  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.lchtime.safetyexpress.ui.chat.hx.activity.ContactActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        //exit();
        finish();/*  ww  w  .  ja  v a  2  s  . c  o m*/
        return true;
    }

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //moveTaskToBack(false);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

From source file:es.farfuteam.vncpp.controller.CanvasActivity.java

/**
 * @param event The event//from w  w  w .  j  a  va 2s . c  o m
 * @brief Captures the keys of the keyboard
 * @details Capture the keys of the keyboard. If the character has to be pressed with a combination of the shift key
 * and other key then adds 100 to the modKeyCount attribute to applies the offset
 */
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN || event.getAction() == KeyEvent.ACTION_UP
            || event.getAction() == KeyEvent.ACTION_MULTIPLE) {
        int keyunicode = event.getUnicodeChar(event.getMetaState());
        char character = (char) keyunicode;
        int key = event.getKeyCode();
        if (key == 59) {
            if (modKeyCount == 0) {
                modKeyCount = 100;
            } else {
                modKeyCount = 0;
            }
        } else if (key >= 7) {
            boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
            vnc.sendKey(modKeyCount + key, down);

        }

        Log.e(DEBUG_TAG, String.valueOf(event.getKeyCode()));
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.irccloud.android.activity.MainActivity.java

@SuppressLint("NewApi")
@SuppressWarnings({ "deprecation", "unchecked" })
@Override//from   w  w w  .  jav  a2s  . c o m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    suggestionsTimer = new Timer("suggestions-timer");
    countdownTimer = new Timer("messsage-countdown-timer");

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    registerReceiver(screenReceiver, filter);

    if (Build.VERSION.SDK_INT >= 21) {
        Bitmap cloud = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        if (cloud != null) {
            setTaskDescription(new ActivityManager.TaskDescription(getResources().getString(R.string.app_name),
                    cloud, 0xFFF2F7FC));
            cloud.recycle();
        }
    }
    setContentView(R.layout.activity_message);
    try {
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    } catch (Throwable t) {
    }

    suggestionsAdapter = new SuggestionsAdapter();
    progressBar = (ProgressBar) findViewById(R.id.progress);
    errorMsg = (TextView) findViewById(R.id.errorMsg);
    buffersListView = findViewById(R.id.BuffersList);
    messageContainer = (LinearLayout) findViewById(R.id.messageContainer);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

    redColor = getResources().getColor(R.color.highlight_red);
    blueColor = getResources().getColor(R.color.dark_blue);

    messageTxt = (ActionEditText) findViewById(R.id.messageTxt);
    messageTxt.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (sendBtn.isEnabled()
                    && NetworkConnection.getInstance().getState() == NetworkConnection.STATE_CONNECTED
                    && event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER
                    && messageTxt.getText() != null && messageTxt.getText().length() > 0) {
                sendBtn.setEnabled(false);
                new SendTask().execute((Void) null);
            } else if (keyCode == KeyEvent.KEYCODE_TAB) {
                if (event.getAction() == KeyEvent.ACTION_DOWN)
                    nextSuggestion();
                return true;
            }
            return false;
        }
    });
    messageTxt.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (drawerLayout != null && v == messageTxt && hasFocus) {
                drawerLayout.closeDrawers();
                update_suggestions(false);
            } else if (!hasFocus) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        suggestionsContainer.setVisibility(View.INVISIBLE);
                    }
                });
            }
        }
    });
    messageTxt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawerLayout != null) {
                drawerLayout.closeDrawers();
            }
        }
    });
    messageTxt.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            if (sendBtn.isEnabled()
                    && NetworkConnection.getInstance().getState() == NetworkConnection.STATE_CONNECTED
                    && actionId == EditorInfo.IME_ACTION_SEND && messageTxt.getText() != null
                    && messageTxt.getText().length() > 0) {
                sendBtn.setEnabled(false);
                new SendTask().execute((Void) null);
            }
            return true;
        }
    });
    textWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {
            Object[] spans = s.getSpans(0, s.length(), Object.class);
            for (Object o : spans) {
                if (((s.getSpanFlags(o) & Spanned.SPAN_COMPOSING) != Spanned.SPAN_COMPOSING)
                        && (o.getClass() == StyleSpan.class || o.getClass() == ForegroundColorSpan.class
                                || o.getClass() == BackgroundColorSpan.class
                                || o.getClass() == UnderlineSpan.class || o.getClass() == URLSpan.class)) {
                    s.removeSpan(o);
                }
            }
            if (s.length() > 0
                    && NetworkConnection.getInstance().getState() == NetworkConnection.STATE_CONNECTED) {
                sendBtn.setEnabled(true);
                if (Build.VERSION.SDK_INT >= 11)
                    sendBtn.setAlpha(1);
            } else {
                sendBtn.setEnabled(false);
                if (Build.VERSION.SDK_INT >= 11)
                    sendBtn.setAlpha(0.5f);
            }
            String text = s.toString();
            if (text.endsWith("\t")) { //Workaround for Swype
                text = text.substring(0, text.length() - 1);
                messageTxt.setText(text);
                nextSuggestion();
            } else if (suggestionsContainer != null && suggestionsContainer.getVisibility() == View.VISIBLE) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        update_suggestions(false);
                    }
                });
            } else {
                if (suggestionsTimer != null) {
                    if (suggestionsTimerTask != null)
                        suggestionsTimerTask.cancel();
                    suggestionsTimerTask = new TimerTask() {
                        @Override
                        public void run() {
                            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
                            update_suggestions(false);
                        }
                    };
                    suggestionsTimer.schedule(suggestionsTimerTask, 250);
                }
            }
        }

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

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    };
    messageTxt.addTextChangedListener(textWatcher);
    sendBtn = findViewById(R.id.sendBtn);
    sendBtn.setFocusable(false);
    sendBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (NetworkConnection.getInstance().getState() == NetworkConnection.STATE_CONNECTED)
                new SendTask().execute((Void) null);
        }
    });

    photoBtn = findViewById(R.id.photoBtn);
    if (photoBtn != null) {
        photoBtn.setFocusable(false);
        photoBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                insertPhoto();
            }
        });
    }
    userListView = findViewById(R.id.usersListFragment);

    View v = getLayoutInflater().inflate(R.layout.actionbar_messageview, null);
    v.findViewById(R.id.actionTitleArea).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            show_topic_popup();
        }
    });

    if (drawerLayout != null) {
        if (findViewById(R.id.usersListFragment2) == null) {
            upDrawable = new DrawerArrowDrawable(this);
            greyColor = upDrawable.getColor();
            ((Toolbar) findViewById(R.id.toolbar)).setNavigationIcon(upDrawable);
            ((Toolbar) findViewById(R.id.toolbar)).setNavigationContentDescription("Show navigation drawer");
            drawerLayout.setDrawerListener(mDrawerListener);
            if (refreshUpIndicatorTask != null)
                refreshUpIndicatorTask.cancel(true);
            refreshUpIndicatorTask = new RefreshUpIndicatorTask();
            refreshUpIndicatorTask.execute((Void) null);
        }
    }
    messageTxt.setDrawerLayout(drawerLayout);

    title = (TextView) v.findViewById(R.id.title);
    subtitle = (TextView) v.findViewById(R.id.subtitle);
    key = (ImageView) v.findViewById(R.id.key);
    getSupportActionBar().setCustomView(v);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    if (savedInstanceState != null && savedInstanceState.containsKey("cid")) {
        server = ServersDataSource.getInstance().getServer(savedInstanceState.getInt("cid"));
        buffer = BuffersDataSource.getInstance().getBuffer(savedInstanceState.getInt("bid"));
        backStack = (ArrayList<Integer>) savedInstanceState.getSerializable("backStack");
    }

    if (savedInstanceState != null && savedInstanceState.containsKey("imagecaptureuri"))
        imageCaptureURI = Uri.parse(savedInstanceState.getString("imagecaptureuri"));
    else
        imageCaptureURI = null;

    ConfigInstance config = (ConfigInstance) getLastCustomNonConfigurationInstance();
    if (config != null) {
        imgurTask = config.imgurUploadTask;
        fileUploadTask = config.fileUploadTask;
    }

    drawerLayout.setScrimColor(0);
    drawerLayout.closeDrawers();

    getSupportActionBar().setElevation(0);
}

From source file:com.mobicage.rogerthat.plugins.friends.RecommendServiceActivity.java

private void configureMailView() {
    T.UI();//from   w w  w  . j  a  va 2  s  .  c o  m
    final AutoCompleteTextView emailText = (AutoCompleteTextView) findViewById(R.id.recommend_email_text_field);
    emailText.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, new ArrayList<String>()));
    emailText.setThreshold(1);

    if (mService.isPermitted(Manifest.permission.READ_CONTACTS)) {
        mService.postAtFrontOfBIZZHandler(new SafeRunnable() {

            @SuppressWarnings("unchecked")
            @Override
            protected void safeRun() throws Exception {
                L.d("RecommendServiceActivity getEmailAddresses");
                List<String> emailList = ContactListHelper.getEmailAddresses(RecommendServiceActivity.this);
                ArrayAdapter<String> a = (ArrayAdapter<String>) emailText.getAdapter();
                for (int i = 0; i < emailList.size(); i++) {
                    a.add(emailList.get(i));
                }
                a.notifyDataSetChanged();
                L.d("RecommendServiceActivity gotEmailAddresses");
            }
        });
    }

    final SafeViewOnClickListener onClickListener = new SafeViewOnClickListener() {
        @Override
        public void safeOnClick(View v) {
            String email = emailText.getText().toString().trim();
            if (RegexPatterns.EMAIL.matcher(email).matches()) {
                mFriendsPlugin.shareService(mServiceEmail, email);
                emailText.setText(null);
                UIUtils.hideKeyboard(RecommendServiceActivity.this, emailText);

                AlertDialog.Builder builder = new AlertDialog.Builder(RecommendServiceActivity.this);
                builder.setMessage(R.string.service_recommendation_sent);
                builder.setPositiveButton(R.string.rogerthat, null);
                builder.create().show();
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(RecommendServiceActivity.this);
                builder.setMessage(R.string.registration_email_not_valid);
                builder.setPositiveButton(R.string.rogerthat, null);
                builder.create().show();
            }
        }
    };

    ((Button) findViewById(R.id.recommend_email_button)).setOnClickListener(onClickListener);

    emailText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN)) {
                onClickListener.onClick(view);
                return true;
            }
            return false;
        }
    });
}

From source file:android.support.v17.leanback.app.PlaybackSupportFragment.java

private boolean onInterceptInputEvent(InputEvent event) {
    final boolean controlsHidden = !mControlVisible;
    if (DEBUG)//  w w  w.j  a  v a  2  s . co  m
        Log.v(TAG, "onInterceptInputEvent hidden " + controlsHidden + " " + event);
    boolean consumeEvent = false;
    int keyCode = KeyEvent.KEYCODE_UNKNOWN;
    int keyAction = 0;

    if (event instanceof KeyEvent) {
        keyCode = ((KeyEvent) event).getKeyCode();
        keyAction = ((KeyEvent) event).getAction();
        if (mInputEventHandler != null) {
            consumeEvent = mInputEventHandler.onKey(getView(), keyCode, (KeyEvent) event);
        }
    }

    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_DPAD_DOWN:
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_DPAD_LEFT:
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        // Event may be consumed; regardless, if controls are hidden then these keys will
        // bring up the controls.
        if (controlsHidden) {
            consumeEvent = true;
        }
        if (keyAction == KeyEvent.ACTION_DOWN) {
            tickle();
        }
        break;
    case KeyEvent.KEYCODE_BACK:
    case KeyEvent.KEYCODE_ESCAPE:
        if (mInSeek) {
            // when in seek, the SeekUi will handle the BACK.
            return false;
        }
        // If controls are not hidden, back will be consumed to fade
        // them out (even if the key was consumed by the handler).
        if (!controlsHidden) {
            consumeEvent = true;

            if (((KeyEvent) event).getAction() == KeyEvent.ACTION_UP) {
                hideControlsOverlay(true);
            }
        }
        break;
    default:
        if (consumeEvent) {
            if (keyAction == KeyEvent.ACTION_DOWN) {
                tickle();
            }
        }
    }
    return consumeEvent;
}