android 模拟按键

/**
	 * 模拟硬键盘发送
	 */
	private void sendToApp() {
		String currentPkgName = mImeService.getImeService().getCurrentInputEditorInfo().packageName;
		if (mContext.getString(R.string.setting_tencent_mm_package).equals(currentPkgName)) {
			InputConnection inputconnection = mImeService.getInputConnection();
			CharSequence cb = inputconnection.getTextAfterCursor(1000, 0);
            CharSequence ca = inputconnection.getTextBeforeCursor(1000, 0);
            int len = cb.length() + ca.length();
			inputconnection.setSelection(len, len);
			mImeService.sendKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT);
			mImeService.sendKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT);
			try {
				TimeUnit.MILLISECONDS.sleep(50);
			} catch (InterruptedException e) {
				// just ignore
			}
			mImeService.sendKeyEvent(KeyEvent.KEYCODE_DPAD_CENTER);
//			try {
//				TimeUnit.MILLISECONDS.sleep(50);
//			} catch (InterruptedException e) {
//				// just ignore
//			}
//			mImeService.sendKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT);
//			mImeService.sendKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT);
		} else {
			mImeService.setHcrFullCheckMode();
			showEditPopWindow();
		}
	}

你可能感兴趣的:(android)