void resetModifiers() { InputConnection conn = getCurrentInputConnection(); if (conn == null) { return; } conn.clearMetaKeyStates(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON); }
@Override public void run() { final EditorInfo info = new EditorInfo(); final InputConnection ic = getView().onCreateInputConnection(info); fAssertNotNull("Must have an InputConnection", ic); // Restore the IC to a clean state ic.clearMetaKeyStates(-1); ic.finishComposingText(); mTest.test(ic, info); synchronized (this) { // Test finished; return from launch(). mDone = true; notify(); } }
/** * Use this to monitor key events being delivered to the application. We get first crack at them, * and can either resume them or let them continue to the app. */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: // The InputMethodService already takes care of the back // key for us, to dismiss the input method if it is shown. // However, our keyboard could be showing a pop-up window // that back should dismiss, so we first allow it to do that. if (event.getRepeatCount() == 0 && mInputView != null) { if (mInputView.handleBack()) { return true; } } break; case KeyEvent.KEYCODE_DEL: // Special handling of the delete key: if we currently are // composing text for the user, we want to modify that instead // of let the application to the delete itself. if (mComposing.length() > 0) { onKey(Keyboard.KEYCODE_DELETE, null); return true; } break; case KeyEvent.KEYCODE_ENTER: // Let the underlying text editor always handle these. return false; default: // For all other keys, if we want to do transformations on // text being entered with a hard keyboard, we need to process // it and do the appropriate action. if (PROCESS_HARD_KEYS) { if (keyCode == KeyEvent.KEYCODE_SPACE && (event.getMetaState() & KeyEvent.META_ALT_ON) != 0) { // A silly example: in our input method, Alt+Space // is a shortcut for 'android' in lower case. InputConnection ic = getCurrentInputConnection(); if (ic != null) { // First, tell the editor that it is no longer in the // shift state, since we are consuming this. ic.clearMetaKeyStates(KeyEvent.META_ALT_ON); keyDownUp(KeyEvent.KEYCODE_A); keyDownUp(KeyEvent.KEYCODE_N); keyDownUp(KeyEvent.KEYCODE_D); keyDownUp(KeyEvent.KEYCODE_R); keyDownUp(KeyEvent.KEYCODE_O); keyDownUp(KeyEvent.KEYCODE_I); keyDownUp(KeyEvent.KEYCODE_D); // And we consume this event. return true; } } if (mPredictionOn && translateKeyDown(keyCode, event)) { return true; } } } return super.onKeyDown(keyCode, event); }
void sendKey(int code, boolean down, boolean resetModifiers) { long time = System.currentTimeMillis(); if (time - lastWake > 5000) { wakeLock.acquire(); wakeLock.release(); lastWake = time; } InputConnection conn = getCurrentInputConnection(); if (conn == null) { // Debug.d("connection closed"); return; } if (code < 0) { if (down == false) return; switch (code) { case KEY_HOME: keyHome(conn); break; case KEY_END: keyEnd(conn); break; case KEY_DEL: keyDel(conn); break; } return; } if (pressedKeys.contains(KEY_CONTROL)) { switch (code) { case KeyEvent.KEYCODE_DPAD_LEFT: if (!down) return; wordLeft(conn); return; case KeyEvent.KEYCODE_DPAD_RIGHT: if (!down) return; wordRight(conn); return; case KeyEvent.KEYCODE_DEL: if (!down) return; deleteWordLeft(conn); return; case KeyEvent.KEYCODE_FORWARD_DEL: deleteWordRight(conn); return; case KeyEvent.KEYCODE_DPAD_CENTER: if (!down) return; copy(conn); return; } } if (pressedKeys.contains(KeyEvent.KEYCODE_SHIFT_LEFT)) { switch (code) { case KeyEvent.KEYCODE_DPAD_CENTER: if (!down) return; paste(conn); return; } } if (code == KeyEvent.KEYCODE_ENTER) { if (shouldSend()) { if (!down) return; Log.d("ivan", "sending submit action"); conn.performEditorAction(EditorInfo.IME_ACTION_SEND); return; } } // if (pressedKeys.contains(KEY_CONTROL)) { // if (down == false) return; // switch (code) { // case KeyEvent.KEYCODE_A: selectAll(conn); break; // case KeyEvent.KEYCODE_X: cut(conn); break; // case KeyEvent.KEYCODE_C: copy(conn); break; // case KeyEvent.KEYCODE_V: paste(conn); break; // } // return; // } conn.sendKeyEvent( new KeyEvent( android.os.SystemClock.uptimeMillis(), android.os.SystemClock.uptimeMillis(), down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP, code, 0, (pressedKeys.contains(KeyEvent.KEYCODE_SHIFT_LEFT) ? KeyEvent.META_SHIFT_LEFT_ON : 0) + (pressedKeys.contains(KEY_CONTROL) ? KeyEvent.META_CTRL_ON : 0) + (pressedKeys.contains(KeyEvent.KEYCODE_ALT_LEFT) ? KeyEvent.META_ALT_LEFT_ON : 0))); if (resetModifiers) { conn.clearMetaKeyStates(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON); } }