Java Code Examples for android.view.KeyEvent#ACTION_DOWN

The following examples show how to use android.view.KeyEvent#ACTION_DOWN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VerticalViewPager.java    From ankihelper with GNU General Public License v3.0 8 votes vote down vote up
/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_TAB:
                if (Build.VERSION.SDK_INT >= 11) {
                    // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                    // before Android 3.0. Ignore the tab key on those devices.
                    if (KeyEvent.metaStateHasNoModifiers(event.getMetaState())) {
                        handled = arrowScroll(FOCUS_FORWARD);
                    } else if (KeyEvent.metaStateHasNoModifiers(event.getMetaState())) {
                        handled = arrowScroll(FOCUS_BACKWARD);
                    }
                }
                break;
        }
    }
    return handled;
}
 
Example 2
Source File: ExFilePickerActivity.java    From ExFilePicker with MIT License 6 votes vote down vote up
@Override
public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (mIsMultiChoiceModeEnabled) {
                setMultiChoiceModeEnabled(false);
                setupOkButtonVisibility();
            } else {
                if (isTopDirectory(mCurrentDirectory)) {
                    finish();
                } else {
                    readUpDirectory();
                }
            }
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS) {
            finish();
        }
        return true;
    }
    return super.dispatchKeyEvent(event);
}
 
Example 3
Source File: ImeAdapter.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
boolean translateAndSendNativeEvents(KeyEvent event) {
    if (mNativeImeAdapterAndroid == 0) return false;

    int action = event.getAction();
    if (action != KeyEvent.ACTION_DOWN &&
        action != KeyEvent.ACTION_UP) {
        // action == KeyEvent.ACTION_MULTIPLE
        // TODO(bulach): confirm the actual behavior. Apparently:
        // If event.getKeyCode() == KEYCODE_UNKNOWN, we can send a
        // composition key down (229) followed by a commit text with the
        // string from event.getUnicodeChars().
        // Otherwise, we'd need to send an event with a
        // WebInputEvent::IsAutoRepeat modifier. We also need to verify when
        // we receive ACTION_MULTIPLE: we may receive it after an ACTION_DOWN,
        // and if that's the case, we'll need to review when to send the Char
        // event.
        return false;
    }
    mViewEmbedder.onImeEvent(false);
    return nativeSendKeyEvent(mNativeImeAdapterAndroid, event, event.getAction(),
            getModifiers(event.getMetaState()), event.getEventTime(), event.getKeyCode(),
                            event.isSystem(), event.getUnicodeChar());
}
 
Example 4
Source File: VerticalViewPager.java    From DoubleViewPager with Apache License 2.0 6 votes vote down vote up
/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_TAB:
                if (Build.VERSION.SDK_INT >= 11) {
                    // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                    // before Android 3.0. Ignore the tab key on those devices.
                    if (KeyEventCompat.hasNoModifiers(event)) {
                        handled = arrowScroll(FOCUS_FORWARD);
                    } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                        handled = arrowScroll(FOCUS_BACKWARD);
                    }
                }
                break;
        }
    }
    return handled;
}
 
Example 5
Source File: HorizontalViewPager.java    From DoubleViewPager with Apache License 2.0 6 votes vote down vote up
/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_TAB:
                if (Build.VERSION.SDK_INT >= 11) {
                    // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                    // before Android 3.0. Ignore the tab key on those devices.
                    if (KeyEventCompat.hasNoModifiers(event)) {
                        handled = arrowScroll(FOCUS_FORWARD);
                    } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                        handled = arrowScroll(FOCUS_BACKWARD);
                    }
                }
                break;
        }
    }
    return handled;
}
 
Example 6
Source File: CustomViewAbove.java    From appcan-android with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_TAB:
                if (Build.VERSION.SDK_INT >= 11) {
                    // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                    // before Android 3.0. Ignore the tab key on those devices.
                    if (event.hasNoModifiers()) {
                        handled = arrowScroll(FOCUS_FORWARD);
                    } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
                        handled = arrowScroll(FOCUS_BACKWARD);
                    }
                }
                break;
        }
    }
    return handled;
}
 
Example 7
Source File: MainActivity.java    From Mp3Cutter with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 监听后退退出
 *
 * @param keyCode 按键码
 * @param event   时间
 * @return 是否处理完成
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        if ((System.currentTimeMillis() - time > 1000)) {
            Toast.makeText(this, getResources().getString(R.string.main_finish_tip),
                    Toast.LENGTH_SHORT).show();
            time = System.currentTimeMillis();
        } else {
            moveTaskToBack(false);
        }
        return true;
    }
    else {
        return super.onKeyDown(keyCode, event);
    }
}
 
Example 8
Source File: EditTextUtil.java    From RxAndroidBootstrap with Apache License 2.0 6 votes vote down vote up
public static View.OnKeyListener getSoftInPutHideListener(final Context context) {

        View.OnKeyListener onKeyList = new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    EditTextUtil.hideSoftInPut((EditText) v, context);
                    return true;
                }
                return false;
            }
        };

        return onKeyList;
    }
 
Example 9
Source File: CHMFrag.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (event.getAction() == KeyEvent.ACTION_DOWN) {
          switch (keyCode) {
              case KeyEvent.KEYCODE_BACK:
                  if (webview.canGoBack()) {
                      webview.goBack();
                  }
                  return true;
          }
      }
      //return super.onKeyDown(keyCode, event);
return false;
  }
 
Example 10
Source File: SearchViewLayout.java    From Search-View-Layout with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
            isExpanded()) {
        boolean keyboardHidden = Utils.hideInputMethod(v);
        if (keyboardHidden) return true;
        collapse();
        return true;
    }
    return false;
}
 
Example 11
Source File: MEditText.java    From EditTag with MIT License 5 votes vote down vote up
@Override
public boolean sendKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN
            && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
        // Un-comment if you wish to cancel the backspace:
        // return false;
    }
    return super.sendKeyEvent(event);
}
 
Example 12
Source File: ProcessorAccessibilityHints.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyEvent(KeyEvent event, EventId eventId) {
  if (event.getAction() == KeyEvent.ACTION_DOWN) {
    // Mainly to prevent hints from being activated when typing, attempting to perform
    // shortcuts, etc. Doesn't cancel in-progress hint, user can use normal actions to
    // cancel (e.g. Ctrl).
    cancelA11yHint();
  }
  return false;
}
 
Example 13
Source File: WebAcitivity.java    From coderfun with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();
            } else {
                finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 14
Source File: TextInputProxyEditTextbox.java    From ForgePE with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean sendKeyEvent(KeyEvent event) {
    if (this.textbox.getText().length() == 0 && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
        if (TextInputProxyEditTextbox.this._mcpeKeyWatcher != null) {
            TextInputProxyEditTextbox.this._mcpeKeyWatcher.onDeleteKeyPressed();
        }
        return false;
    }
    return super.sendKeyEvent(event);
}
 
Example 15
Source File: Main.java    From iSCAU-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
        if((System.currentTimeMillis()-exitTime) > 2000){
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        }
        else{
            MobclickAgent.onKillProcess(this);
            System.exit(0);
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 16
Source File: CustomPlayBackController.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (player == null || event.getAction() != KeyEvent.ACTION_DOWN) {
        return super.dispatchKeyEvent(event);
    }
    switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            fastForward();
            break;
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_DPAD_LEFT:
            rewind();
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            player.setPlayWhenReady(!player.getPlayWhenReady());
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            player.setPlayWhenReady(true);
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            player.setPlayWhenReady(false);
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            next();
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            previous();
            break;
        default:
            return false;
    }
    show();
    return true;
}
 
Example 17
Source File: MainActivity.java    From JianDan with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK
            && event.getAction() == KeyEvent.ACTION_DOWN) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ShowToast.Short("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 18
Source File: AwContents.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isDpadEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            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:
                return true;
        }
    }
    return false;
}
 
Example 19
Source File: VolumePanel.java    From Noyze with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKey(View v, final int keyCode, KeyEvent event) {
       LOGI("VolumePanel", "onKey(" + keyCode + ")");

       // Don't handle ANYTHING when a call is in progress!
       if (mCallState != TelephonyManager.CALL_STATE_IDLE)
           return super.onKey(v, keyCode, event);

	switch (keyCode) {
		// Handle the DOWN + MULTIPLE action (holding down).
           case KeyEvent.KEYCODE_VOLUME_UP:
           case KeyEvent.KEYCODE_VOLUME_DOWN:
           	final int adjust = ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) ?
                           	 AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER);
           	switch (event.getAction()) {
                   case KeyEvent.ACTION_DOWN:
                       // If another key was pressed while holding on to
                       // one volume key, we'll need to abort mission.
                       if (mKeyCodeDown != 0) {
                           mIgnoreNextKeyUp = true;
                           mHandler.removeMessages(MSG_VOLUME_LONG_PRESS);
                           return super.onKey(v, keyCode, event);
                       }

                       mKeyCodeDown = event.getKeyCode();
                       mKeyTimeDown = System.currentTimeMillis();
                       event.startTracking();

                       // NOTE: we'll allow long press events if we've set an action.
                       boolean callIdle = (mCallState == TelephonyManager.CALL_STATE_IDLE);
                       if (!noLongPress || hasLongPressAction(keyCode)) {
                           mHandler.sendMessageDelayed(mHandler.obtainMessage(
                                   MSG_VOLUME_LONG_PRESS, event), ((callIdle && hasLongPressAction(keyCode)) ?
                                   mLongPressTimeout : mLongPressTimeout / 2));
                       }
                       break;
           		case KeyEvent.ACTION_UP:
           		case KeyEvent.ACTION_MULTIPLE:
                       boolean hasLongPress = mHandler.hasMessages(MSG_VOLUME_LONG_PRESS);
                       mHandler.removeMessages(MSG_VOLUME_LONG_PRESS);
                       boolean ignoreNextKeyUp = mIgnoreNextKeyUp;
                       mIgnoreNextKeyUp = false;
                       mKeyCodeDown = 0;

                       // We've been told to let this one go.
                       if (ignoreNextKeyUp || event.isCanceled()) {
                           mKeyTimeDown = 0;
                           return true;
                       }

                       if ((hasLongPress || noLongPress) && (System.currentTimeMillis() -
                               mKeyTimeDown) < mLongPressTimeout) {
                           mVolumeDirty = true;
                           mKeyTimeDown = 0;
                           if (!firstReveal || (firstReveal && isShowing())) {
                               adjustVolume(adjust);
                               show();
                           } else {
                               reveal();
                           }
                       }
           			break;
           	}
               break;
           case KeyEvent.KEYCODE_VOLUME_MUTE:
               switch (event.getAction()) {
                   case KeyEvent.ACTION_UP:
                       boolean mute = isMuted(STREAM_RING);
                       mAudioManager.setStreamMute(STREAM_RING, !mute);
                       mAudioManager.setStreamMute(STREAM_NOTIFICATION, !mute);
                       mVolumeDirty = true;
                       show();
                       break;
               }
               break;
       }

       return super.onKey(v, keyCode, event);
}
 
Example 20
Source File: AndroidControllers.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKey (View view, int keyCode, KeyEvent keyEvent) {
	if (ignoreNoGamepadButtons && !keyEvent.isGamepadButton(keyCode)) {
		return false;
	}
	AndroidController controller = controllerMap.get(keyEvent.getDeviceId());
	if(controller != null) {
		if(controller.getButton(keyCode) && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
			return true;
		}
		synchronized(eventQueue) {
			AndroidControllerEvent event = eventPool.obtain();
			event.controller = controller;
			if(keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
				if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
					event.type = AndroidControllerEvent.POV;
					controller.pov |= 0x00000001;
					event.povDirection = controller.getPov(0);
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
					event.type = AndroidControllerEvent.POV;
					controller.pov |= 0x00000010;
					event.povDirection = controller.getPov(0);
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
					event.type = AndroidControllerEvent.POV;
					controller.pov |= 0x00000100;
					event.povDirection = controller.getPov(0);
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
					event.type = AndroidControllerEvent.POV;
					controller.pov |= 0x00001000;
					event.povDirection = controller.getPov(0);
				} else {
					event.type = AndroidControllerEvent.BUTTON_DOWN;
					event.code = keyCode;
				}
			} else {
				if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
					event.type = AndroidControllerEvent.POV;
					controller.pov &= 0x00001110;
					event.povDirection = controller.getPov(0);
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
					event.type = AndroidControllerEvent.POV;
					controller.pov &= 0x00001101;
					event.povDirection = controller.getPov(0);
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
					event.type = AndroidControllerEvent.POV;
					controller.pov &= 0x00001011;
					event.povDirection = controller.getPov(0);
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
					event.type = AndroidControllerEvent.POV;
					controller.pov &= 0x00000111;
					event.povDirection = controller.getPov(0);
				} else {
					event.type = AndroidControllerEvent.BUTTON_UP;
					event.code = keyCode;
				}
			}
			eventQueue.add(event);
		}
		if (keyCode == KeyEvent.KEYCODE_BACK && !Gdx.input.isCatchBackKey()) {
			return false;
		}
		return true;
	} else {
		return false;
	}
}