public boolean processKey(
      InputConnection inputContext, KeyEvent event, boolean upperCase, boolean realAction) {
    if (null == inputContext || null == event) return false;

    int keyCode = event.getKeyCode();

    CharSequence prefix = null;
    prefix = inputContext.getTextBeforeCursor(2, 0);

    int keyChar;
    keyChar = 0;
    if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
      keyChar = keyCode - KeyEvent.KEYCODE_A + 'a';
      if (upperCase) {
        keyChar = keyChar + 'A' - 'a';
      }
    } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9)
      keyChar = keyCode - KeyEvent.KEYCODE_0 + '0';
    else if (keyCode == KeyEvent.KEYCODE_COMMA) keyChar = ',';
    else if (keyCode == KeyEvent.KEYCODE_PERIOD) keyChar = '.';
    else if (keyCode == KeyEvent.KEYCODE_APOSTROPHE) keyChar = '\'';
    else if (keyCode == KeyEvent.KEYCODE_AT) keyChar = '@';
    else if (keyCode == KeyEvent.KEYCODE_SLASH) keyChar = '/';

    if (0 == keyChar) {
      mLastKeyCode = keyCode;

      String insert = null;
      if (KeyEvent.KEYCODE_DEL == keyCode) {
        if (realAction) {
          inputContext.deleteSurroundingText(1, 0);
        }
      } else if (KeyEvent.KEYCODE_ENTER == keyCode) {
        insert = "\n";
      } else if (KeyEvent.KEYCODE_SPACE == keyCode) {
        insert = " ";
      } else {
        return false;
      }

      if (null != insert && realAction) inputContext.commitText(insert, insert.length());

      return true;
    }

    if (!realAction) return true;

    if (KeyEvent.KEYCODE_SHIFT_LEFT == mLastKeyCode
        || KeyEvent.KEYCODE_SHIFT_LEFT == mLastKeyCode) {
      if (keyChar >= 'a' && keyChar <= 'z') keyChar = keyChar - 'a' + 'A';
    } else if (KeyEvent.KEYCODE_ALT_LEFT == mLastKeyCode) {
    }

    String result = String.valueOf((char) keyChar);
    inputContext.commitText(result, result.length());
    mLastKeyCode = keyCode;
    return true;
  }
Beispiel #2
0
 /** Helper function to commit any text being composed in to the editor. */
 private void commitTyped(InputConnection inputConnection) {
   if (mComposing.length() > 0) {
     inputConnection.commitText(mComposing, mComposing.length());
     mComposing.setLength(0);
     updateCandidates();
   }
 }
 public void onKey(int primaryCode, int[] keyCodes) {
   InputConnection ic = getCurrentInputConnection();
   playClick(primaryCode);
   switch (primaryCode) {
     case Keyboard.KEYCODE_DELETE:
       ic.deleteSurroundingText(1, 0);
       break;
     case Keyboard.KEYCODE_SHIFT:
       if (caps) {
         keyboard = new Keyboard(this, R.xml.qwerty);
         kv.setKeyboard(keyboard);
         kv.invalidateAllKeys();
         kv.setOnKeyboardActionListener(this);
       } else {
         keyboard = new Keyboard(this, R.xml.qwertytwo);
         kv.setKeyboard(keyboard);
         kv.invalidateAllKeys();
         kv.setOnKeyboardActionListener(this);
       }
       caps = !caps;
       keyboard.setShifted(caps);
       kv.invalidateAllKeys();
       break;
     case Keyboard.KEYCODE_DONE:
       ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
       break;
     default:
       char code = (char) primaryCode;
       if (Character.isLetter(code) && caps) {
         code = Character.toUpperCase(code);
       }
       ic.commitText(String.valueOf(code), 1);
   }
 }
 private void commitText(@NotNull InputConnection ic, @Nullable CharSequence text, int position) {
   ic.commitText(text, position);
   if (!Strings.isEmpty(text)) {
     history.addState(
         new KeyboardInputHistoryState(AndroidKeyboardUtils.getTextFromInputConnection(ic), 0));
   }
 }
Beispiel #5
0
 /** Commits the given text to the editing field. */
 public boolean commitText(InputConnection ic, CharSequence text) {
   if (ic != null) {
     if (text.length() > 1) {
       // Batch edit a sequence of characters.
       ic.beginBatchEdit();
       ic.commitText(text, 1);
       ic.endBatchEdit();
     } else {
       ic.commitText(text, 1);
     }
     // Composing-text in the editor has been cleared.
     composingText.setLength(0);
     return true;
   }
   return false;
 }
 private void restoreFromHistory(@Nullable KeyboardInputHistoryState state) {
   if (state != null) {
     final InputConnection ic = getCurrentInputConnection();
     ic.deleteSurroundingText(MAX_INT, MAX_INT);
     ic.commitText(state.getCharSequence(), 1);
   }
 }
Beispiel #7
0
 private void pickSuggestion(CharSequence suggestion) {
   InputConnection ic = mService.getCurrentInputConnection();
   if (ic != null) {
     ic.commitText(suggestion, 1);
   }
   mService.setCommittedLength(suggestion.length());
   mService.setSuggestions(null, false, false, false);
 }
Beispiel #8
0
 @Override
 public void onText(CharSequence text) {
   InputConnection ic = mService.getCurrentInputConnection();
   if (ic == null) return;
   ic.beginBatchEdit();
   ic.commitText(text, 1);
   ic.endBatchEdit();
 }
Beispiel #9
0
 public void onText(CharSequence text) {
   InputConnection ic = getCurrentInputConnection();
   if (ic == null) return;
   ic.beginBatchEdit();
   if (mComposing.length() > 0) {
     commitTyped(ic);
   }
   ic.commitText(text, 0);
   ic.endBatchEdit();
   updateShiftKeyState(getCurrentInputEditorInfo());
 }
Beispiel #10
0
 public void commitTyped(InputConnection inputConnection) {
   if (mComposing.length() > 0) {
     if (inputConnection != null) {
       inputConnection.commitText(mComposing, 1);
     }
     mService.setCommittedLength(mComposing.length());
     mComposing.setLength(0);
     mWord.reset();
     updateSuggestions();
   }
 }
Beispiel #11
0
  public static boolean inputCharacter(char character) {
    InputConnection connection = getInputConnection();

    if (connection != null) {
      if (connection.commitText(Character.toString(character), 1)) {
        return true;
      }
    }

    return false;
  }
 boolean setText(String text) {
   // FIXME: need feedback if the input was lost
   InputConnection conn = getCurrentInputConnection();
   if (conn == null) {
     //      Debug.d("connection closed");
     return false;
   }
   conn.beginBatchEdit();
   // FIXME: hack
   conn.deleteSurroundingText(100000, 100000);
   conn.commitText(text, text.length());
   conn.endBatchEdit();
   return true;
 }
  private void keyDel(InputConnection conn) {
    // if control key used -- delete word right
    if (pressedKeys.contains(KEY_CONTROL)) {
      deleteWordRight(conn);
      return;
    }

    if (pressedKeys.contains(KeyEvent.KEYCODE_SHIFT_LEFT)) {
      cut(conn);
      return;
    }
    conn.deleteSurroundingText(0, 1);
    conn.commitText("", 0);
  }
 @Override
 public boolean commitText(CharSequence text, int newCursorPosition) {
   if (InputMethods.shouldCommitCharAsKey(mCurrentInputMethod)
       && text.length() == 1
       && newCursorPosition > 0) {
     if (DEBUG) {
       Log.d(LOGTAG, "committing \"" + text + "\" as key");
     }
     // mKeyInputConnection is a BaseInputConnection that commits text as keys;
     // but we first need to replace any composing span with a selection,
     // so that the new key events will generate characters to replace
     // text from the old composing span
     return replaceComposingSpanWithSelection()
         && mKeyInputConnection.commitText(text, newCursorPosition);
   }
   return super.commitText(text, newCursorPosition);
 }
  void receivedChar(int code) {
    wakeLock.acquire();
    wakeLock.release();
    InputConnection conn = getCurrentInputConnection();
    if (conn == null) {
      //      Debug.d("connection closed");
      return;
    }

    if (pressedKeys.contains(KEY_CONTROL)) {
      switch (code) {
        case 'a':
        case 'A':
          selectAll(conn);
          return;
        case 'x':
        case 'X':
          cut(conn);
          return;
        case 'c':
        case 'C':
          copy(conn);
          return;
        case 'v':
        case 'V':
          paste(conn);
          return;
      }
    }

    String text = null;
    if (code >= 0 && code <= 65535) {
      text = new String(new char[] {(char) code});
    } else {
      int HI_SURROGATE_START = 0xD800;
      int LO_SURROGATE_START = 0xDC00;
      int hi = ((code >> 10) & 0x3FF) - 0x040 | HI_SURROGATE_START;
      int lo = LO_SURROGATE_START | (code & 0x3FF);
      text = new String(new char[] {(char) hi, (char) lo});
    }
    conn.commitText(text, 1);
  }
Beispiel #16
0
  @Override
  public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    // Log.d(TAG, "Strokes size: " + gesture.getStrokesCount());

    InputConnection ic = getCurrentInputConnection();

    for (GestureStroke stroke : gesture.getStrokes()) {
      GesturePoint[] points = ShorthandUtils.extractGesturePointsFromStroke(stroke);
      String result = mRecognizer.recognize(points);
      if (mParameters.isDebugEnabled()) {
        StenoCanvas canvas = (StenoCanvas) overlay;
        canvas.setDebugPaths(mRecognizer.getDebugPaths());
        canvas.setDebugPoints(points);
      }
      if (result != null && result.length() > 0) {
        ic.commitText(result, 1);
      } else if (mParameters.isPopupsEnabled()) {
        Toast.makeText(mContext, R.string.not_found, Toast.LENGTH_SHORT).show();
      }
    }
  }
Beispiel #17
0
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {

    // This handles the hardware keyboard input
    if (event.isPrintingKey()) {
      if (event.getAction() == KeyEvent.ACTION_DOWN) {
        ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
      }
      return true;
    }

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
      SDLActivity.onNativeKeyDown(keyCode);
      return true;
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
      SDLActivity.onNativeKeyUp(keyCode);
      return true;
    }

    return false;
  }