@Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
   if (event != null && v.getId() == R.id.edittext_note_editor) {
     // if shift key is down, then we want to insert the '\n' char in the
     // TextView;
     // otherwise, the default action is to send the message.
     if (event.getAction() == KeyEvent.ACTION_DOWN) {
       Log.d(TAG, "keyCode is " + keyCode);
       if (keyCode == KeyEvent.KEYCODE_ENTER) {
         if (!event.isShiftPressed()) {
           performSubmitClick();
           return true;
         }
       }
       if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
         if (event.isShiftPressed() && event.isCtrlPressed()) {
           mCallbacks.onKeyNextTalk();
           return true;
         }
       }
       if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
         if (event.isShiftPressed() && event.isCtrlPressed()) {
           mCallbacks.onKeyPreviousTalk();
           return true;
         }
       }
     }
   }
   return false;
 }
Beispiel #2
0
 /**
  * Convert a KeyEvent into a long which can be kept in settings and compared to key presses when
  * the service is in use.
  *
  * @param keyEvent The key event to convert. The (non-extended) keycode must not be a modifier.
  * @return An extended key code that includes modifier information
  */
 public static long keyEventToExtendedKeyCode(KeyEvent keyEvent) {
   long returnValue = keyEvent.getKeyCode();
   returnValue |= (keyEvent.isShiftPressed()) ? (((long) KeyEvent.META_SHIFT_ON) << 32) : 0;
   returnValue |= (keyEvent.isCtrlPressed()) ? (((long) KeyEvent.META_CTRL_ON) << 32) : 0;
   returnValue |= (keyEvent.isAltPressed()) ? (((long) KeyEvent.META_ALT_ON) << 32) : 0;
   return returnValue;
 }
 @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
   KeyModifiers m = new KeyModifiers();
   m.shift = event.isShiftPressed();
   m.alt = event.isAltPressed();
   final char k = TranslateKey(keyCode, m);
   if (k == 0) return false;
   if (event.getAction() == KeyEvent.ACTION_DOWN) Emulator.the.OnKey(k, true, m.shift, m.alt);
   else Emulator.the.OnKey(k, false, false, false);
   return true;
 }
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
      // We don't want to swallow the back button press
      return false;
    }

    // NOTE: Most keyboards, and specifically the Android default keyboard when
    // entering non-ascii characters, will not trigger KeyEvent events as documented
    // here: http://developer.android.com/reference/android/view/KeyEvent.html

    // Log.e("KeyDown", "------------");
    // Log.e("KeyDown", "keyChar:" + (int) event.getDisplayLabel());
    // Log.e("KeyDown", "utfChar:" + (char)event.getUnicodeChar());
    // Log.e("KeyDown", "intUtfChar:" + event.getUnicodeChar());

    final NetworkPackage np = new NetworkPackage(MousePadPlugin.PACKAGE_TYPE_MOUSEPAD_REQUEST);

    boolean modifier = false;
    if (event.isAltPressed()) {
      np.set("alt", true);
      modifier = true;
    }

    if (Build.VERSION.SDK_INT >= 11) {
      if (event.isCtrlPressed()) {
        np.set("ctrl", true);
        modifier = true;
      }
    }

    if (event.isShiftPressed()) {
      np.set("shift", true);
    }

    int specialKey = SpecialKeysMap.get(keyCode, -1);

    if (specialKey != -1) {
      np.set("specialKey", specialKey);
    } else if (event.getDisplayLabel() != 0 && modifier) {
      // Alt will change the utf symbol to non-ascii characters, we want the plain original letter
      // Since getDisplayLabel will always have a value, we have to check for special keys before
      char keyCharacter = event.getDisplayLabel();
      np.set("key", new String(new char[] {keyCharacter}).toLowerCase());
    } else {
      // A normal key, but still not handled by the KeyInputConnection (happens with numbers)
      np.set("key", new String(new char[] {(char) event.getUnicodeChar()}));
    }

    sendKeyPressPackage(np);
    return true;
  }
  /**
   * 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) {
    mTempRect.setEmpty();

    if (!canScroll()) {
      if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
        View currentFocused = findFocus();
        if (currentFocused == this) currentFocused = null;
        View nextFocused =
            FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
        return nextFocused != null
            && nextFocused != this
            && nextFocused.requestFocus(View.FOCUS_DOWN);
      }
      return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
      switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
          if (!event.isAltPressed()) {
            handled = arrowScroll(View.FOCUS_UP);
          } else {
            handled = fullScroll(View.FOCUS_UP);
          }
          break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
          if (!event.isAltPressed()) {
            handled = arrowScroll(View.FOCUS_DOWN);
          } else {
            handled = fullScroll(View.FOCUS_DOWN);
          }
          break;
        case KeyEvent.KEYCODE_SPACE:
          pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
          break;
      }
    }

    return handled;
  }
Beispiel #6
0
  @SmallTest
  @Feature({"TextInput", "Main"})
  public void testGuessedKeyCodeFromTyping() throws Throwable {
    assertEquals(-1, getTypedKeycodeGuess(null, ""));
    assertEquals(KeyEvent.KEYCODE_X, getTypedKeycodeGuess(null, "x"));
    assertEquals(-1, getTypedKeycodeGuess(null, "xyz"));

    assertEquals(-1, getTypedKeycodeGuess("abc", "abc"));
    assertEquals(KeyEvent.KEYCODE_DEL, getTypedKeycodeGuess("abc", ""));

    assertEquals(KeyEvent.KEYCODE_H, getTypedKeycodeGuess("", "h"));
    assertEquals(KeyEvent.KEYCODE_DEL, getTypedKeycodeGuess("h", ""));
    assertEquals(KeyEvent.KEYCODE_E, getTypedKeycodeGuess("h", "he"));
    assertEquals(KeyEvent.KEYCODE_L, getTypedKeycodeGuess("he", "hel"));
    assertEquals(KeyEvent.KEYCODE_O, getTypedKeycodeGuess("hel", "helo"));
    assertEquals(KeyEvent.KEYCODE_DEL, getTypedKeycodeGuess("helo", "hel"));
    assertEquals(KeyEvent.KEYCODE_L, getTypedKeycodeGuess("hel", "hell"));
    assertEquals(KeyEvent.KEYCODE_L, getTypedKeycodeGuess("hell", "helll"));
    assertEquals(KeyEvent.KEYCODE_DEL, getTypedKeycodeGuess("helll", "hell"));
    assertEquals(KeyEvent.KEYCODE_O, getTypedKeycodeGuess("hell", "hello"));

    assertEquals(KeyEvent.KEYCODE_X, getTypedKeycodeGuess("xxx", "xxxx"));
    assertEquals(KeyEvent.KEYCODE_X, getTypedKeycodeGuess("xxx", "xxxxx"));
    assertEquals(KeyEvent.KEYCODE_DEL, getTypedKeycodeGuess("xxx", "xx"));
    assertEquals(KeyEvent.KEYCODE_DEL, getTypedKeycodeGuess("xxx", "x"));

    assertEquals(KeyEvent.KEYCODE_Y, getTypedKeycodeGuess("xxx", "xxxy"));
    assertEquals(KeyEvent.KEYCODE_Y, getTypedKeycodeGuess("xxx", "xxxxy"));
    assertEquals(-1, getTypedKeycodeGuess("xxx", "xy"));
    assertEquals(-1, getTypedKeycodeGuess("xxx", "y"));

    assertEquals(-1, getTypedKeycodeGuess("foo", "bar"));
    assertEquals(-1, getTypedKeycodeGuess("foo", "bars"));
    assertEquals(-1, getTypedKeycodeGuess("foo", "ba"));

    // Some characters also require modifiers so we have to check the full event.
    KeyEvent ev = ImeAdapter.getTypedKeyEventGuess(null, "!");
    assertEquals(KeyEvent.KEYCODE_1, ev.getKeyCode());
    assertTrue(ev.isShiftPressed());
  }
Beispiel #7
0
 private static int getMetaState(KeyEvent event) {
   return (event.isCtrlPressed() ? CTRL : 0)
       | (event.isAltPressed() ? ALT : 0)
       | (event.isShiftPressed() ? SHIFT : 0);
 }
  /**
   * Overrides default directional pad behavior:
   *
   * <ul>
   *   <li>Up/down: Increases/decreases text navigation granularity
   *   <li>Left/right: Moves to previous/next unit of text
   * </ul>
   *
   * <p>If one of the following conditions is met, default behavior is preserved:
   *
   * <ul>
   *   <li>No input connection available
   *   <li>Input view is hidden
   *   <li>Not currently editing text
   *   <li>Cannot move in the specified direction
   * </ul>
   */
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (mUserCommandHandler.onKeyUp(event)) {
      return true;
    }

    final AccessibleInputConnection aic = getCurrentInputConnection();
    if (aic == null || !aic.hasExtractedText()) {
      return super.onKeyUp(keyCode, event);
    }

    final KeyEvent downEvent = mPreviousDpadDownEvent;
    mPreviousDpadDownEvent = null;

    final KeyEvent metaDownEvent = mPreviousMetaDownEvent;
    mPreviousMetaDownEvent = null;

    if (downEvent != null) {
      boolean captureEvent = false;

      switch (downEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
          if (!event.isAltPressed()) {
            captureEvent = previousUnit(mGranularity, 1, event.isShiftPressed());
          } else {
            mWasUpDownPressed = true;
          }
          break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
          if (!event.isAltPressed()) {
            captureEvent = nextUnit(mGranularity, 1, event.isShiftPressed());
          } else {
            mWasUpDownPressed = true;
          }
          break;
        case KeyEvent.KEYCODE_DPAD_UP:
          if (event.isAltPressed()) {
            adjustGranularity(1);
            captureEvent = true;
          } else {
            mWasUpDownPressed = true;
          }
          break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
          if (event.isAltPressed()) {
            adjustGranularity(-1);
            captureEvent = true;
          } else {
            mWasUpDownPressed = true;
          }
          break;
      }

      if (captureEvent) {
        return true;
      }
    }

    // If we didn't capture the meta event, attempt to send the previous
    // meta down event and then preserve default behavior.
    if (metaDownEvent != null) {
      if (!super.onKeyDown(metaDownEvent.getKeyCode(), metaDownEvent)) {
        aic.sendKeyEvent(metaDownEvent);
      }
    }

    // If we didn't capture the event, attempt to send the previous down
    // event and then preserve default behavior.
    if (downEvent != null) {
      if (!super.onKeyDown(downEvent.getKeyCode(), downEvent)) {
        aic.sendKeyEvent(downEvent);
      }
    }

    if (!super.onKeyUp(keyCode, event)) {
      aic.sendKeyEvent(event);
    }

    return true;
  }
Beispiel #9
0
  public boolean executeKeyEvent(KeyEvent var1) {
    boolean var4 = false;
    this.mTempRect.setEmpty();
    boolean var3;
    if (!this.canScroll()) {
      var3 = var4;
      if (this.isFocused()) {
        var3 = var4;
        if (var1.getKeyCode() != 4) {
          View var5 = this.findFocus();
          View var6 = var5;
          if (var5 == this) {
            var6 = null;
          }

          var6 = FocusFinder.getInstance().findNextFocus(this, var6, 130);
          var3 = var4;
          if (var6 != null) {
            var3 = var4;
            if (var6 != this) {
              var3 = var4;
              if (var6.requestFocus(130)) {
                var3 = true;
              }
            }
          }
        }
      }

      return var3;
    } else {
      var4 = false;
      var3 = var4;
      if (var1.getAction() == 0) {
        switch (var1.getKeyCode()) {
          case 19:
            if (!var1.isAltPressed()) {
              var3 = this.arrowScroll(33);
            } else {
              var3 = this.fullScroll(33);
            }
            break;
          case 20:
            if (!var1.isAltPressed()) {
              var3 = this.arrowScroll(130);
            } else {
              var3 = this.fullScroll(130);
            }
            break;
          case 62:
            short var2;
            if (var1.isShiftPressed()) {
              var2 = 33;
            } else {
              var2 = 130;
            }

            this.pageScroll(var2);
            var3 = var4;
            break;
          default:
            var3 = var4;
        }
      }

      return var3;
    }
  }