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.koma.music.service.MediaButtonIntentReceiver.java

@Override
public void onReceive(final Context context, final Intent intent) {
    LogUtils.i(TAG, "Received intent: " + intent);
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
        startService(context, MusicServiceConstants.CMDPAUSE, System.currentTimeMillis());
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null || event.getAction() != KeyEvent.ACTION_UP) {
            return;
        }//  www  .j  a  va 2  s.c  o m

        String command = null;
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_HEADSETHOOK:
            command = MusicServiceConstants.CMDHEADSETHOOK;
            break;
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MusicServiceConstants.CMDSTOP;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MusicServiceConstants.CMDTOGGLEPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MusicServiceConstants.CMDNEXT;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MusicServiceConstants.CMDPREVIOUS;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MusicServiceConstants.CMDPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MusicServiceConstants.CMDPLAY;
            break;
        }
        if (command != null) {
            startService(context, command, event.getEventTime());
            if (isOrderedBroadcast()) {
                abortBroadcast();
            }
        }
    }
}

From source file:org.interactiverobotics.headset_launcher.MediaButtonMonitorService.java

@Override
public void onCreate() {

    Log.d(TAG, "Create");

    //   ?   //from   w  w w.  ja  va  2 s.  co  m

    mMediaButtonReceiver = new ComponentName(this, MediaButtonReceiver.class);

    // ,  ?   API 18+
    //
    //        final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    //
    //        mediaButtonIntent.setComponent(mMediaButtonReceiver);
    //
    //        mPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);

    mMediaSession = new MediaSessionCompat(this, TAG, mMediaButtonReceiver, /* pendingIntent */ null);

    mMediaSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {

            if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonEvent.getAction())) {

                final KeyEvent keyEvent = (KeyEvent) mediaButtonEvent
                        .getParcelableExtra(Intent.EXTRA_KEY_EVENT);

                if (KeyEvent.KEYCODE_HEADSETHOOK == keyEvent.getKeyCode()) {

                    if (KeyEvent.ACTION_DOWN == keyEvent.getAction()) {

                        Log.d(TAG, "Media button down!");

                        // ? 

                        startService(new Intent(MediaButtonMonitorService.this, LauncherService.class));
                    }

                } else {

                    Log.e(TAG, "Unknown keycode: " + keyEvent.getKeyCode());
                }

            } else {

                Log.e(TAG, "Unknown intent: " + mediaButtonEvent.getAction());
            }

            return true;
        }

    });

    mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);

    mMediaSession.setActive(true);
}

From source file:org.videolan.vlc.RemoteControlClientReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (event != null && action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {

        if (event.getKeyCode() != KeyEvent.KEYCODE_HEADSETHOOK
                && event.getKeyCode() != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
                && event.getAction() != KeyEvent.ACTION_DOWN) {
            super.onReceive(context, intent);
            return;
        }/*  ww w .j  a  v a  2s.  c  o  m*/

        Intent i = null;
        switch (event.getKeyCode()) {
        /*
         * one click => play/pause
         * long click => previous
         * double click => next
         */
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            long time = SystemClock.uptimeMillis();
            switch (event.getAction()) {
            case KeyEvent.ACTION_DOWN:
                if (event.getRepeatCount() <= 0)
                    mHeadsetDownTime = time;
                break;
            case KeyEvent.ACTION_UP:
                if (AndroidDevices.hasTsp()) { //no backward/forward on TV
                    if (time - mHeadsetDownTime >= 1000) { // long click
                        i = new Intent(PlaybackService.ACTION_REMOTE_BACKWARD, null,
                                VLCApplication.getAppContext(), PlaybackService.class);
                        break;
                    } else if (time - mHeadsetUpTime <= 500) { // double click
                        i = new Intent(PlaybackService.ACTION_REMOTE_FORWARD, null,
                                VLCApplication.getAppContext(), PlaybackService.class);
                        break;
                    }
                }
                // one click
                i = new Intent(PlaybackService.ACTION_REMOTE_PLAYPAUSE, null, VLCApplication.getAppContext(),
                        PlaybackService.class);
                mHeadsetUpTime = time;
                break;
            }
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            context.startService(new Intent(PlaybackService.ACTION_REMOTE_PLAY, null,
                    VLCApplication.getAppContext(), PlaybackService.class));
            return;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            i = new Intent(PlaybackService.ACTION_REMOTE_PAUSE, null, VLCApplication.getAppContext(),
                    PlaybackService.class);
            break;
        case KeyEvent.KEYCODE_MEDIA_STOP:
            i = new Intent(PlaybackService.ACTION_REMOTE_STOP, null, VLCApplication.getAppContext(),
                    PlaybackService.class);
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            i = new Intent(PlaybackService.ACTION_REMOTE_FORWARD, null, VLCApplication.getAppContext(),
                    PlaybackService.class);
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            i = new Intent(PlaybackService.ACTION_REMOTE_BACKWARD, null, VLCApplication.getAppContext(),
                    PlaybackService.class);
            break;
        }

        if (isOrderedBroadcast())
            abortBroadcast();
        if (i != null) {
            context.startService(i);
            return;
        }
    } else if (action.equals(PlaybackService.ACTION_REMOTE_PLAYPAUSE)) {
        intent = new Intent(context, PlaybackService.class);
        intent.setAction(PlaybackService.ACTION_REMOTE_PLAYPAUSE);
        context.startService(intent);
        return;
    }
    super.onReceive(context, intent);
}

From source file:com.glanznig.beepme.view.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_UP) {
            audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
        }//from  w w  w . j  a va  2  s .co m
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

From source file:com.veniosg.dir.android.fragment.SearchListFragment.java

private void setupStaticViews() {
    mQueryView.setHint(hintText);/*w  w  w . j a va  2s. co m*/
    mQueryView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean pressedDpadOrEnter = false;
            if (event != null) {
                int eventCode = event.getKeyCode();
                boolean dpadCenterOrEnter = eventCode == KEYCODE_DPAD_CENTER || eventCode == KEYCODE_ENTER;
                pressedDpadOrEnter = event.getAction() == ACTION_UP && dpadCenterOrEnter;
            }

            if (actionId == IME_ACTION_SEARCH || pressedDpadOrEnter) {
                boolean startedNewSearch = viewModel.updateQuery(v.getText().toString());
                if (startedNewSearch)
                    searchIdlingResource.setBusy();
                return true;
            }
            return false;
        }
    });
    mUpView.setOnClickListener(v -> getActivity().finish());
}

From source file:com.jxlc.tajiproject.ui.MainActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() != KeyEvent.ACTION_UP) {
        finish();/*from w ww  .j  av  a  2  s  .c  om*/
        return true;
    }

    if (event.getAction() == KeyEvent.ACTION_MULTIPLE) {
        return mUnityPlayer.injectEvent(event);
    }
    return super.dispatchKeyEvent(event);
}

From source file:tv.acfun.video.fragment.SearchFragment.java

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_SEARCH
            || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
        startSearch(v.getText().toString(), 1);
    }/*from   www  .  j  ava 2s  .c  om*/
    return false;
}

From source file:org.catrobat.catroid.ui.fragment.FormulaEditorVariableListFragment.java

@Override
public void onListItemClick(int position) {

    if (!inContextMode) {
        formulaEditorEditText.handleKeyEvent(0, "" + adapter.getItem(position).getName());
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
        onKey(null, keyEvent.getKeyCode(), keyEvent);
    }//from   w  ww  . ja  v a  2s .  co  m

}

From source file:com.mercandalli.android.apps.files.user.ConversationActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conversation);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);//from   w ww.  j av  a2s . co  m
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        Log.e("" + getClass().getName(), "extras == null");
        this.finish();
        this.overridePendingTransition(R.anim.right_in, R.anim.right_out);
        return;
    }

    mInputEditText = (EditText) findViewById(R.id.input);

    mIdConversation = extras.getString("ID_CONVERSATION");
    mUrl = Constants.URL_DOMAIN + Config.ROUTE_USER_MESSAGE + "/" + mIdConversation;

    mProgressBar = (ProgressBar) findViewById(R.id.circularProgressBar);
    mMessageTextView = (TextView) findViewById(R.id.message);

    mRecyclerView = (RecyclerView) findViewById(R.id.listView);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setStackFromEnd(true);
    mRecyclerView.setLayoutManager(linearLayoutManager);

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light, android.R.color.holo_orange_light,
            android.R.color.holo_red_light);

    this.mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshList();
        }
    });

    mAdapterModelConversationMessage = new AdapterModelConversationMessage(mUserConversationMessageModels);
    mRecyclerView.setAdapter(mAdapterModelConversationMessage);
    mRecyclerView.setItemAnimator(/*new SlideInFromLeftItemAnimator(mRecyclerView)*/new DefaultItemAnimator());
    mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

    mAdapterModelConversationMessage
            .setOnItemClickListener(new AdapterModelConversationMessage.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }
            });

    mAdapterModelConversationMessage
            .setOnItemLongClickListener(new AdapterModelConversationMessage.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(View view, int position) {

                    return true;
                }
            });

    mInputEditText.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)) {
                String url = Constants.URL_DOMAIN + Config.ROUTE_USER_MESSAGE + "/" + mIdConversation;
                List<StringPair> parameters = new ArrayList<>();
                parameters.add(new StringPair("message", "" + mInputEditText.getText().toString()));
                mInputEditText.setText("");

                new TaskPost(ConversationActivity.this, url, new IPostExecuteListener() {
                    @Override
                    public void onPostExecute(JSONObject json, String body) {
                        refreshList();
                    }
                }, parameters).execute();

                return true;
            }
            return false;
        }
    });

    refreshList();
}

From source file:com.fusionx.lightirc.ui.IRCFragment.java

@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
    final CharSequence text = mMessageBox.getText();
    if ((event == null || actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
            || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
            && StringUtils.isNotEmpty(text)) {
        final String message = text.toString();
        mMessageBox.setText("");
        onSendMessage(message);/*from  w  ww .j  ava 2  s  .  c  o m*/
        return true;
    }
    return false;
}