Example usage for android.view KeyEvent getAction

List of usage examples for android.view KeyEvent getAction

Introduction

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

Prototype

public final int getAction() 

Source Link

Document

Retrieve the action of this key event.

Usage

From source file:Main.java

/**
 * Checks whether the given event is an ACTION_DOWN event.
 * @param event Event to be checked./*from w  w  w .java  2 s  .c o m*/
 * @return Whether the event is an ACTION_DOWN event.
 */
public static boolean isActionDown(KeyEvent event) {
    return event.getAction() == KeyEvent.ACTION_DOWN;
}

From source file:Main.java

/**
 * Checks whether the given event is an ACTION_UP event.
 * @param event Event to be checked.//from  w  w w  .  j av a2s  .  co m
 * @return Whether the event is an ACTION_UP event.
 */
public static boolean isActionUp(KeyEvent event) {
    return event.getAction() == KeyEvent.ACTION_UP;
}

From source file:com.jefftharris.passwdsafe.lib.view.GuiUtils.java

/**
 * Setup the keyboard on a form.  The final field performs the supplied
 * runnable when enter is pressed./*from  w  ww. ja  v a2s  .c  o m*/
 */
public static void setupFormKeyboard(TextView firstField, TextView finalField, Context ctx,
        final Runnable enterRunnable) {
    if (firstField != null) {
        GuiUtilsFroyo.showKeyboard(firstField, ctx);
    }

    finalField.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER: {
                    enterRunnable.run();
                    return true;
                }
                }
            }
            return false;
        }
    });
}

From source file:com.dragedy.playermusic.service.MediaButtonIntentReceiver.java

public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }/*w  w  w . ja  v a2  s  .  c o  m*/

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime();

        String command = null;
        switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MusicService.ACTION_STOP;
            break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MusicService.ACTION_TOGGLE_PAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MusicService.ACTION_SKIP;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MusicService.ACTION_REWIND;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MusicService.ACTION_PAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MusicService.ACTION_PLAY;
            break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG)
                            Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0,
                                context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.vuze.android.remote.AndroidUtilsUI.java

public static boolean handleCommonKeyDownEvents(Activity a, int keyCode, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return false;
    }/* w w  w  .ja v  a  2s  .co  m*/
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_NEXT:
    case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
        ViewGroup vg = (ViewGroup) a.findViewById(android.R.id.content);
        ArrayList list = AndroidUtilsUI.findByClass(vg, ViewPager.class, new ArrayList<View>(0));

        if (list.size() > 0) {
            ViewPager viewPager = (ViewPager) list.get(0);
            viewPager.arrowScroll(View.FOCUS_RIGHT);
        }
        break;
    }

    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
    case KeyEvent.KEYCODE_MEDIA_REWIND: {
        ViewGroup vg = (ViewGroup) a.findViewById(android.R.id.content);
        ArrayList list = AndroidUtilsUI.findByClass(vg, ViewPager.class, new ArrayList<View>(0));

        if (list.size() > 0) {
            ViewPager viewPager = (ViewPager) list.get(0);
            viewPager.arrowScroll(View.FOCUS_LEFT);
        }
        break;
    }

    case KeyEvent.KEYCODE_DPAD_LEFT: {
        if (a instanceof DrawerActivity) {
            DrawerActivity da = (DrawerActivity) a;
            DrawerLayout drawerLayout = da.getDrawerLayout();
            View viewFocus = a.getCurrentFocus();
            boolean canOpenDrawer = viewFocus != null && "leftmost".equals(viewFocus.getTag());
            if (canOpenDrawer) {
                drawerLayout.openDrawer(Gravity.LEFT);
                drawerLayout.requestFocus();
                return true;
            }
        }
        break;
    }

    }

    return false;
}

From source file:org.deviceconnect.android.deviceplugin.theta.activity.ThetaFeatureActivity.java

@Override
public boolean dispatchKeyEvent(final KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            finish();//from  ww w.  ja v a2 s.  c  o m
            break;
        default:
            break;
        }
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.commonsware.android.abf.ActionBarFragment.java

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
        adapter.add(v.getText().toString());
        v.setText("");

        InputMethodManager imm = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }/*  w ww . j a  v a  2s.co m*/

    return (true);
}

From source file:com.stu.zdy.weather.ui.fragment.InfoFragment.java

public void onResume() {
    super.onResume();
    MobclickAgent.onPageStart(InfoFragment.TAG);
    getView().setFocusableInTouchMode(true);
    getView().requestFocus();/*from w  ww .j  a va  2  s .  co  m*/
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                fragmentCallBack.callbackInfoFragment(null);
                return true;
            }
            return false;
        }
    });
}

From source file:hai.tran.mywallet.fragment.CustomFragment.java

@Override
public void onResume() {
    super.onResume();
    getView().setFocusableInTouchMode(true);
    getView().requestFocus();//from  w  ww.  j a  v a  2s  . c  om
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                Fragment frag = new DateFragment();
                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.setCustomAnimations(R.transition.sli_re_in, R.transition.sli_re_out);
                fragmentTransaction.replace(R.id.fragment_main, frag).commit();
                return true;
            }
            return false;
        }
    });
}

From source file:bander.notepad.About.java

@NonNull
@Override/*  ww  w.jav a 2 s  .  c  om*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity())

            .setIcon(android.R.drawable.ic_dialog_info)

            .setTitle(R.string.about1)

            .setMessage(R.string.about2)

            .setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (event.getAction() == 1) {
                        if (keyCode == 24) {
                            Toast.makeText(getActivity(),
                                    "\u0057\u006f\u0077\u002c \u0079\u006f"
                                            + "\u0075\u0027\u0072\u0065 \u0067\u006f\u006f\u0064\u0021",
                                    Toast.LENGTH_LONG).show();
                            return true;
                        }
                    }
                    return false;
                }
            })

            .setPositiveButton(R.string.licences, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // LGPLv3 is not included in the library, so we have to register it.
                    LicenseResolver.registerLicense(new GnuLesserGeneralPublicLicense30());
                    final LicensesDialogFragment fragment = LicensesDialogFragment.newInstance(R.raw.notices,
                            false);
                    fragment.show(getActivity().getSupportFragmentManager(), null);
                }
            })

            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }).create();
}