Java Code Examples for android.view.KeyEvent#getDeviceId()

The following examples show how to use android.view.KeyEvent#getDeviceId() . 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 File: LatinIME.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onKeyUp(final int keyCode, final KeyEvent keyEvent) {
    if (mEmojiAltPhysicalKeyDetector == null) {
        mEmojiAltPhysicalKeyDetector = new EmojiAltPhysicalKeyDetector(
                getApplicationContext().getResources());
    }
    mEmojiAltPhysicalKeyDetector.onKeyUp(keyEvent);
    if (!ProductionFlags.IS_HARDWARE_KEYBOARD_SUPPORTED) {
        return super.onKeyUp(keyCode, keyEvent);
    }
    final long keyIdentifier = keyEvent.getDeviceId() << 32 + keyEvent.getKeyCode();
    if (mInputLogic.mCurrentlyPressedHardwareKeys.remove(keyIdentifier)) {
        return true;
    }
    return super.onKeyUp(keyCode, keyEvent);
}
 
Example 2
Source File: LatinIME.java    From Android-Keyboard with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyUp(final int keyCode, final KeyEvent keyEvent) {
    if (mEmojiAltPhysicalKeyDetector == null) {
        mEmojiAltPhysicalKeyDetector = new EmojiAltPhysicalKeyDetector(
                getApplicationContext().getResources());
    }
    mEmojiAltPhysicalKeyDetector.onKeyUp(keyEvent);
    if (!ProductionFlags.IS_HARDWARE_KEYBOARD_SUPPORTED) {
        return super.onKeyUp(keyCode, keyEvent);
    }
    final long keyIdentifier = keyEvent.getDeviceId() << 32 + keyEvent.getKeyCode();
    if (mInputLogic.mCurrentlyPressedHardwareKeys.remove(keyIdentifier)) {
        return true;
    }
    return super.onKeyUp(keyCode, keyEvent);
}
 
Example 3
Source File: LatinIME.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyUp(final int keyCode, final KeyEvent keyEvent) {
    if (mEmojiAltPhysicalKeyDetector == null) {
        mEmojiAltPhysicalKeyDetector = new EmojiAltPhysicalKeyDetector(
                getApplicationContext().getResources());
    }
    mEmojiAltPhysicalKeyDetector.onKeyUp(keyEvent);
    if (!ProductionFlags.IS_HARDWARE_KEYBOARD_SUPPORTED) {
        return super.onKeyUp(keyCode, keyEvent);
    }
    final long keyIdentifier = keyEvent.getDeviceId() << 32 + keyEvent.getKeyCode();
    if (mInputLogic.mCurrentlyPressedHardwareKeys.remove(keyIdentifier)) {
        return true;
    }
    return super.onKeyUp(keyCode, keyEvent);
}
 
Example 4
Source File: LatinIME.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyUp(final int keyCode, final KeyEvent keyEvent) {
    if (mEmojiAltPhysicalKeyDetector == null) {
        mEmojiAltPhysicalKeyDetector = new EmojiAltPhysicalKeyDetector(
                getApplicationContext().getResources());
    }
    mEmojiAltPhysicalKeyDetector.onKeyUp(keyEvent);
    if (!ProductionFlags.IS_HARDWARE_KEYBOARD_SUPPORTED) {
        return super.onKeyUp(keyCode, keyEvent);
    }
    final long keyIdentifier = keyEvent.getDeviceId() << 32 + keyEvent.getKeyCode();
    if (mInputLogic.mCurrentlyPressedHardwareKeys.remove(keyIdentifier)) {
        return true;
    }
    return super.onKeyUp(keyCode, keyEvent);
}
 
Example 5
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 6
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 7
Source File: Instrumentation.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 8
Source File: KeyListener.java    From yubikit-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    InputDevice device = event.getDevice();
    if (device.getVendorId() != YUBICO_VID) {
        // do not handle anything that not from yubikey
        return false;
    }

    if (event.getAction() == KeyEvent.ACTION_UP) {
        // use id of keyboard device to distinguish current input device
        // in case of multiple keys inserted
        final int deviceId = event.getDeviceId();
        final StringBuilder otpBuffer = inputBuffers.get(deviceId, new StringBuilder());
        if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER) {
            // Carriage return seen. Assume this is the end of the OTP credential and notify immediately.
            listener.onOtpReceived(otpBuffer.toString());
            inputBuffers.delete(deviceId);
        } else {
            if (otpBuffer.length() == 0) {
                // in case if we never get keycode enter (which is pretty generic scenario) we set timer for 1 sec
                // upon expiration we assume that we have no more input from key
                handler.postDelayed(new InputTimerTask(deviceId), DEFAULT_KEY_DELAY_MS);
            }
            otpBuffer.append((char) event.getUnicodeChar());
            inputBuffers.put(deviceId, otpBuffer);
        }
    }
    return true;
}
 
Example 9
Source File: TalkBackService.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Intended to mimic the behavior of onKeyEvent if this were the only service running. It will be
 * called from onKeyEvent, both from this service and from others in this apk (TalkBack). This
 * method must not block, since it will block onKeyEvent as well.
 *
 * @param keyEvent A key event
 * @return {@code true} if the event is handled, {@code false} otherwise.
 */
@Override
public boolean onKeyEventShared(KeyEvent keyEvent) {
  if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_UNKNOWN) {
    // Tapping on fingerprint sensor somehow files KeyEvent with KEYCODE_UNKNOW, which will change
    // input mode to keyboard, and cancel pending accessibility hints. It is OK to just ignore
    // these KeyEvents since they're unused in TalkBack.
    return false;
  }
  Performance perf = Performance.getInstance();
  EventId eventId = perf.onEventReceived(keyEvent);

  if (isServiceActive()) {
    // Stop the TTS engine when any key (except for volume up/down key) is pressed on physical
    // keyboard.
    if (shouldInterruptByAnyKeyEvent()
        && keyEvent.getDeviceId() != 0
        && keyEvent.getAction() == KeyEvent.ACTION_DOWN
        && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_DOWN
        && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_UP) {
      interruptAllFeedback(false /* stopTtsSpeechCompletely */);
    }
  }

  for (ServiceKeyEventListener listener : keyEventListeners) {
    if (!isServiceActive() && !listener.processWhenServiceSuspended()) {
      continue;
    }

    if (listener.onKeyEvent(keyEvent, eventId)) {
      perf.onHandlerDone(eventId);
      return true;
    }
  }

  return false;
}
 
Example 10
Source File: Instrumentation.java    From droidel with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 11
Source File: GameView.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    int deviceId = event.getDeviceId();
    if (deviceId != -1) {
        Ship currentShip = getShipForId(deviceId);
        if (currentShip.onKeyDown(keyCode, event)) {
            step(event.getEventTime());
            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 12
Source File: GameView.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    int deviceId = event.getDeviceId();
    if (deviceId != -1) {
        Ship currentShip = getShipForId(deviceId);
        if (currentShip.onKeyUp(keyCode, event)) {
            step(event.getEventTime());
            return true;
        }
    }

    return super.onKeyUp(keyCode, event);
}