Java Code Examples for android.view.KeyEvent

The following examples show how to use android.view.KeyEvent. These examples are extracted from open source projects. 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 Project: AndroidAnimationExercise   Source File: SuperTools.java    License: Apache License 2.0 7 votes vote down vote up
/**
 * 获取导航栏高度
 *
 * @param context
 * @return
 */
public static int getNavigationBarHeight(Context context) {

    boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    if (!hasMenuKey && !hasBackKey) {
        //没有物理按钮(虚拟导航栏)
        print("没有物理按钮(虚拟导航栏)");
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        //获取NavigationBar的高度
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    } else {
        print("有物理导航栏,小米非全面屏");
        //有物理导航栏,小米非全面屏
        return 0;
    }
}
 
Example 2
Source Project: mobile-manager-tool   Source File: QGallery.java    License: MIT License 6 votes vote down vote up
@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//		int position = MyGallery.this.getSelectedItemPosition();
//		int minV = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();	
//         
//		if(velocityX > minV){			
//			if(position > 0)
//				MyGallery.this.setSelection(position-1);
//		}else if(velocityX < -minV){
//			if(position < MyGallery.this.getCount()-1)
//				MyGallery.this.setSelection(position+1);
//		}
		if (e2.getX() > e1.getX()) {
			// 往左边滑动
			super.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
		} else {
			// 往右边滑动
			super.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
		}
		return false;
	}
 
Example 3
Source Project: cloud-cup-android   Source File: MainActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();

    firebase = new Firebase(Consts.FIREBASE_URL);
    username = (TextView) findViewById(R.id.username);
    userImage = (ImageView) findViewById(R.id.user_image);
    code = (EditText) findViewById(R.id.code);
    code.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            join();
            return true;
        }
    });
    code.requestFocus();
}
 
Example 4
Source Project: Camera2   Source File: CaptureModule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch (keyCode)
    {
        case KeyEvent.KEYCODE_CAMERA:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            if (mUI.isCountingDown())
            {
                cancelCountDown();
            } else if (event.getRepeatCount() == 0)
            {
                onShutterButtonClick();
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            // Prevent default.
            return true;
    }
    return false;
}
 
Example 5
Source Project: youqu_master   Source File: MaterialIntroActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
            if (messageButtonBehaviours.get(viewPager.getCurrentItem()) != null) {
                messageButton.performClick();
            }
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            int position = viewPager.getCurrentItem();
            if (adapter.isLastSlide(position) && adapter.getItem(position).canMoveFurther()) {
                performFinish();
            } else if (adapter.shouldLockSlide(position)) {
                errorOccurred(adapter.getItem(position));
            } else {
                viewPager.moveToNextPage();
            }
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            moveBack();
            break;
        default:
            return super.onKeyDown(keyCode, event);
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 6
Source Project: recent-images   Source File: TwoWayAbsListView.java    License: MIT License 6 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
		if (!isEnabled()) {
			return true;
		}
		if (isClickable() && isPressed() &&
				mSelectedPosition >= 0 && mAdapter != null &&
				mSelectedPosition < mAdapter.getCount()) {

			final View view = getChildAt(mSelectedPosition - mFirstPosition);
			if (view != null) {
				performItemClick(view, mSelectedPosition, mSelectedRowId);
				view.setPressed(false);
			}
			setPressed(false);
			return true;
		}
	}
	return super.onKeyUp(keyCode, event);
}
 
Example 7
Source Project: AndroidImageSlider   Source File: ViewPagerEx.java    License: MIT License 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 8
Source Project: android-recipes-app   Source File: TransportMediator.java    License: Apache License 2.0 6 votes vote down vote up
static boolean isMediaKey(int keyCode) {
    switch (keyCode) {
        case KEYCODE_MEDIA_PLAY:
        case KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
            return true;
        }
    }
    return false;
}
 
Example 9
Source Project: android_9.0.0_r45   Source File: InputMethodService.java    License: Apache License 2.0 6 votes vote down vote up
void reportExtractedMovement(int keyCode, int count) {
    int dx = 0, dy = 0;
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            dx = -count;
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            dx = count;
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            dy = -count;
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            dy = count;
            break;
    }
    onExtractedCursorMovement(dx, dy);
}
 
Example 10
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN
            && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
            currentBackPressedTime = System.currentTimeMillis();
            ToastUtils.showToast(getString(R.string.exit_tips));
            return true;
        } else {
            finish(); // 退出
        }
    } else if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
        return true;
    }
    return super.dispatchKeyEvent(event);
}
 
Example 11
Source Project: android-test   Source File: DelegatingEditText.java    License: Apache License 2.0 6 votes vote down vote up
public DelegatingEditText(Context context, AttributeSet attrs) {
  super(context, attrs);
  setOrientation(VERTICAL);
  mContext = context;
  LayoutInflater inflater = LayoutInflater.from(context);
  inflater.inflate(R.layout.delegating_edit_text, this, /* attachToRoot */ true);
  messageView = (TextView) findViewById(R.id.edit_text_message);
  delegateEditText = (EditText) findViewById(R.id.delegate_edit_text);
  delegateEditText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionCode, KeyEvent event) {
      messageView.setText("typed: " + delegateEditText.getText());
      messageView.setVisibility(View.VISIBLE);
      InputMethodManager imm =
          (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(delegateEditText.getWindowToken(), 0);
      return true;
    }
  });
}
 
Example 12
private void initViews() {

        getVerifiCodeButton = getView(R.id.btn_send_verifi_code);
        getVerifiCodeButton.setOnClickListener(this);
        phoneEdit = getView(R.id.et_phone);
        phoneEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);// 下一步
        verifyCodeEdit = getView(R.id.et_verifiCode);
        verifyCodeEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);// 下一步
        passwordEdit = getView(R.id.et_password);
        passwordEdit.setImeOptions(EditorInfo.IME_ACTION_DONE);
        passwordEdit.setImeOptions(EditorInfo.IME_ACTION_GO);
        passwordEdit.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                // 点击虚拟键盘的done
                if (actionId == EditorInfo.IME_ACTION_DONE
                        || actionId == EditorInfo.IME_ACTION_GO) {
                    commit();
                }
                return false;
            }
        });
    }
 
Example 13
private void onActionMediaButton(@NonNull final Intent intent) {
    final KeyEvent keyEvent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (keyEvent != null && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyEvent.getKeyCode()) {
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                onActionPlayPause();
                break;

            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                onActionPause();
                break;

            case KeyEvent.KEYCODE_MEDIA_NEXT:
                onActionNext();
                break;

            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                onActionPrev();
                break;

            case KeyEvent.KEYCODE_MEDIA_STOP:
                onActionStop();
                break;

            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                onActionPlayPause();
                break;
        }
    }
}
 
Example 14
Source Project: ki4a   Source File: ForwardActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        //Let's go back to main Activity
        dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
        dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 15
Source Project: DeviceConnect-Android   Source File: ThetaFeatureActivity.java    License: MIT License 5 votes vote down vote up
@SuppressLint("RestrictedApi")
@Override
public boolean dispatchKeyEvent(final KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK:
                finish();
                break;
            default:
                break;
        }
    }
    return super.dispatchKeyEvent(event);
}
 
Example 16
Source Project: zapp   Source File: FullscreenActivity.java    License: MIT License 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
	switch (keyCode) {
		case KeyEvent.KEYCODE_ENTER:
		case KeyEvent.KEYCODE_DPAD_CENTER:
			toggle();
			return true;
	}

	return super.onKeyUp(keyCode, event);
}
 
Example 17
Source Project: NIM_Android_UIKit   Source File: InputPanel.java    License: MIT License 5 votes vote down vote up
/**
 * *************** IEmojiSelectedListener ***************
 */
@Override
public void onEmojiSelected(String key) {
    Editable mEditable = messageEditText.getText();
    if (key.equals("/DEL")) {
        messageEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
    } else {
        int start = messageEditText.getSelectionStart();
        int end = messageEditText.getSelectionEnd();
        start = (start < 0 ? 0 : start);
        end = (start < 0 ? 0 : end);
        mEditable.replace(start, end, key);
    }
}
 
Example 18
/**
 * Redispatches unhandled media keys. This allows bluetooth headphones with play/pause or
 * other buttons to function correctly.
 */
@TargetApi(19)
private void handleMediaKey(KeyEvent e) {
    if (Build.VERSION.SDK_INT < 19) return;
    switch (e.getKeyCode()) {
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        case KeyEvent.KEYCODE_MEDIA_CLOSE:
        case KeyEvent.KEYCODE_MEDIA_EJECT:
        case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
            AudioManager am = (AudioManager) mTab.getApplicationContext().getSystemService(
                    Context.AUDIO_SERVICE);
            am.dispatchMediaKeyEvent(e);
            break;
        default:
            break;
    }
}
 
Example 19
Source Project: zhizhihu   Source File: WebBrowserActivity.java    License: Apache License 2.0 5 votes vote down vote up
public boolean onKeyDown(int keyCoder, KeyEvent event) {
    if (mWebView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK) {
        mWebView.goBack();   //goBack()表示返回webView的上一页面
        return true;
    }
    return super.onKeyDown(keyCoder, event);
}
 
Example 20
Source Project: AssistantBySDK   Source File: TimePickerDialog.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP) {
        return processKeyUp(keyCode);
    }
    return false;
}
 
Example 21
Source Project: Dashchan   Source File: DialogStack.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
		return keyBackHandler.onBackKey(event, !visibileViews.isEmpty());
	}
	return false;
}
 
Example 22
Source Project: mollyim-android   Source File: BaseActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_MENU && isMenuWorkaroundRequired()) {
    openOptionsMenu();
    return true;
  }
  return super.onKeyUp(keyCode, event);
}
 
Example 23
public boolean onKeyDown(int keyCode, KeyEvent event) {
	// TODO Auto-generated method stub
	if (keyCode == KeyEvent.KEYCODE_BACK) {
		ExitApp(); // ����˫���˳�����
	}
	return false;
}
 
Example 24
Source Project: XposedSmsCode   Source File: InputHelper.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void sendKeyEvent(int inputSource, int keyCode, boolean longpress) throws Throwable {
    long now = SystemClock.uptimeMillis();
    injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0, 0,
            KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, inputSource));
    if (longpress) {
        injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 1, 0,
                KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_LONG_PRESS,
                inputSource));
    }
    injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0, 0,
            KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, inputSource));
}
 
Example 25
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
    boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
    boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
            && event.getAction() != KeyEvent.ACTION_MULTIPLE;

    if (isKeyOfInterest && isReleased) {
        stopUpdating();
    } else if (isKeyOfInterest && isPressed) {
        startUpdating(v == plusImage);
    }
    return false;
}
 
Example 26
Source Project: JianDan_OkHttpWithVolley   Source File: MainActivity.java    License: 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 27
Source Project: KeyboardView   Source File: EditView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        hide(false);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 28
Source Project: Camera2   Source File: PhotoModule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch (keyCode)
    {
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_FOCUS:
            if (/* TODO: mActivity.isInCameraApp() && */mFirstTimeInitialized && !mActivity.getCameraAppUI()
                    .isInIntentReview())
            {
                if (event.getRepeatCount() == 0)
                {
                    onShutterButtonFocus(true);
                }
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_CAMERA:
            if (mFirstTimeInitialized && event.getRepeatCount() == 0)
            {
                onShutterButtonClick();
            }
            return true;
        case KeyEvent.KEYCODE_DPAD_CENTER:
            // If we get a dpad center event without any focused view, move
            // the focus to the shutter button and press it.
            if (mFirstTimeInitialized && event.getRepeatCount() == 0)
            {
                // Start auto-focus immediately to reduce shutter lag. After
                // the shutter button gets the focus, onShutterButtonFocus()
                // will be called again but it is fine.
                onShutterButtonFocus(true);
            }
            return true;
    }
    return false;
}
 
Example 29
Source Project: AndroidLinkup   Source File: ConfirmDialog.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 处理返回键
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Button btn = (Button) findViewById(R.id.dialog_button_cancel);
        btn.performClick();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 30
Source Project: Gizwits-SmartBuld_Android   Source File: GosUserLoginActivity.java    License: MIT License 5 votes vote down vote up
/**
 * 菜单、返回键响应
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
		exitBy2Click(); // 调用双击退出函数
	}
	return false;
}