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

The following examples show how to use android.view.KeyEvent#isModifierKey() . 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: AppCompatPreferenceActivity.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
/**
 * KeyEvents with non-default modifiers are not dispatched to menu's performShortcut in API 25
 * or lower. Here, we check if the keypress corresponds to a menuitem's shortcut combination
 * and perform the corresponding action.
 */
private boolean performMenuItemShortcut(int keycode, KeyEvent event) {
    if (!(Build.VERSION.SDK_INT >= 26) && !event.isCtrlPressed()
        && !KeyEvent.metaStateHasNoModifiers(event.getMetaState())
        && event.getRepeatCount() == 0
        && !KeyEvent.isModifierKey(event.getKeyCode())) {
        final Window currentWindow = getWindow();
        if (currentWindow != null && currentWindow.getDecorView() != null) {
            final View decorView = currentWindow.getDecorView();
            if (decorView.dispatchKeyShortcutEvent(event)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 2
Source File: AppCompatPreferenceActivity.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
/**
 * KeyEvents with non-default modifiers are not dispatched to menu's performShortcut in API 25
 * or lower. Here, we check if the keypress corresponds to a menuitem's shortcut combination
 * and perform the corresponding action.
 */
private boolean performMenuItemShortcut(int keycode, KeyEvent event) {
    if (!(Build.VERSION.SDK_INT >= 26) && !event.isCtrlPressed()
        && !KeyEvent.metaStateHasNoModifiers(event.getMetaState())
        && event.getRepeatCount() == 0
        && !KeyEvent.isModifierKey(event.getKeyCode())) {
        final Window currentWindow = getWindow();
        if (currentWindow != null && currentWindow.getDecorView() != null) {
            final View decorView = currentWindow.getDecorView();
            if (decorView.dispatchKeyShortcutEvent(event)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: EmojiAltPhysicalKeyDetector.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
public void onKeyUp(@Nonnull final KeyEvent keyEvent) {
    if (DEBUG) {
        Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - considering " + keyEvent);
    }

    final int keyCode = keyEvent.getKeyCode();
    int metaState = keyEvent.getMetaState();
    if (KeyEvent.isModifierKey(keyCode)) {
         // Try restoring meta stat in case the released key was a modifier.
         // I am sure one can come up with scenarios to break this, but it
         // seems to work well in practice.
         metaState |= mMetaState;
    }

    final Pair<Integer, Integer> key = Pair.create(keyCode, metaState);
    if (mKeySet.contains(key)) {
        if (mCanFire) {
            if (!keyEvent.isCanceled()) {
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - firing action");
                }
                action();
            } else {
                // This key up event was a part of key combinations and
                // should be ignored.
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - canceled, ignoring action");
                }
            }
            mCanFire = false;
        }
    }

    if (mCanFire) {
        if (DEBUG) {
            Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - disabling action");
        }
        mCanFire = false;
    }
}
 
Example 4
Source File: EmojiAltPhysicalKeyDetector.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
public void onKeyUp(@Nonnull final KeyEvent keyEvent) {
    if (DEBUG) {
        Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - considering " + keyEvent);
    }

    final int keyCode = keyEvent.getKeyCode();
    int metaState = keyEvent.getMetaState();
    if (KeyEvent.isModifierKey(keyCode)) {
         // Try restoring meta stat in case the released key was a modifier.
         // I am sure one can come up with scenarios to break this, but it
         // seems to work well in practice.
         metaState |= mMetaState;
    }

    final Pair<Integer, Integer> key = Pair.create(keyCode, metaState);
    if (mKeySet.contains(key)) {
        if (mCanFire) {
            if (!keyEvent.isCanceled()) {
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - firing action");
                }
                action();
            } else {
                // This key up event was a part of key combinations and
                // should be ignored.
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - canceled, ignoring action");
                }
            }
            mCanFire = false;
        }
    }

    if (mCanFire) {
        if (DEBUG) {
            Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - disabling action");
        }
        mCanFire = false;
    }
}
 
Example 5
Source File: EmojiAltPhysicalKeyDetector.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
public void onKeyUp(@Nonnull final KeyEvent keyEvent) {
    if (DEBUG) {
        Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - considering " + keyEvent);
    }

    final int keyCode = keyEvent.getKeyCode();
    int metaState = keyEvent.getMetaState();
    if (KeyEvent.isModifierKey(keyCode)) {
         // Try restoring meta stat in case the released key was a modifier.
         // I am sure one can come up with scenarios to break this, but it
         // seems to work well in practice.
         metaState |= mMetaState;
    }

    final Pair<Integer, Integer> key = Pair.create(keyCode, metaState);
    if (mKeySet.contains(key)) {
        if (mCanFire) {
            if (!keyEvent.isCanceled()) {
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - firing action");
                }
                action();
            } else {
                // This key up event was a part of key combinations and
                // should be ignored.
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - canceled, ignoring action");
                }
            }
            mCanFire = false;
        }
    }

    if (mCanFire) {
        if (DEBUG) {
            Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - disabling action");
        }
        mCanFire = false;
    }
}
 
Example 6
Source File: KeyComboManager.java    From talkback with Apache License 2.0 5 votes vote down vote up
private int matchKeyEventWith(KeyEvent event, int triggerModifier, long keyComboCode) {
  int keyCode = getConvertedKeyCode(event);
  int metaState = event.getModifiers() & KEY_EVENT_MODIFIER_MASK;

  int targetKeyCode = getKeyCode(keyComboCode);
  int targetMetaState = getModifier(keyComboCode) | triggerModifier;

  // Handle exact matches first.
  if (metaState == targetMetaState && keyCode == targetKeyCode) {
    return EXACT_MATCH;
  }

  if (targetMetaState != 0 && metaState == 0) {
    return NO_MATCH;
  }

  // Otherwise, all modifiers must be down.
  if (KeyEvent.isModifierKey(keyCode)
      && targetMetaState != 0
      && (targetMetaState & metaState) != 0) {
    // Partial match.
    return PARTIAL_MATCH;
  }

  // No match.
  return NO_MATCH;
}
 
Example 7
Source File: DefaultKeyComboModel.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEligibleKeyComboCode(long keyComboCode) {
  if (keyComboCode == KEY_COMBO_CODE_UNASSIGNED) {
    return true;
  }

  // Do not allow to set key combo which is consisted only with modifiers.
  int keyCode = KeyComboManager.getKeyCode(keyComboCode);
  if (KeyEvent.isModifierKey(keyCode) || keyCode == KeyEvent.KEYCODE_UNKNOWN) {
    return false;
  }

  // It's not allowed to use trigger modifier as part of key combo code.
  return (KeyComboManager.getModifier(keyComboCode) & getTriggerModifier()) == 0;
}
 
Example 8
Source File: KeyAssignmentUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Handles a key assignment key press. If a key is a valid assignment, the calling screen's keyset
 * is updated and view refreshed. Otherwise, the calling method is told to pass the event up to be
 * handled, or display a message saying the key is already assigned elsewhere.
 *
 * @param context The context of the activity the method call came from
 * @param event The key press event to be processed
 * @param keyCombos The present set of assigned keys
 * @param key The key to ignore while checking for assignment
 * @param keyListAdapter The ArrayAdapter that displays the current list of keys assigned to this
 *     preference
 * @return descriptive constant of how the event was handled
 */
public static int processKeyAssignment(
    Context context,
    KeyEvent event,
    Set<Long> keyCombos,
    String key,
    ArrayAdapter<CharSequence> keyListAdapter) {
  int keyCode = event.getKeyCode();
  /* If we're ignoring this key, don't handle it */
  if (isKeyCodeToIgnore(context, keyCode) || hasFlagToIgnore(event)) {
    return IGNORE_EVENT;
  }
  /* If this is a modifier key, ignore it */
  if (KeyEvent.isModifierKey(keyCode)) {
    return CONSUME_EVENT;
  }
  Long keyCombo = keyEventToExtendedKeyCode(event);
  if (keyCombos.contains(keyCombo)) {
    /* If this key was pressed already, remove it from the list */
    keyCombos.remove(keyCombo);
    updateKeyListAdapter(keyListAdapter, keyCombos, context);
  } else {
    if (otherActionAssociatedWithKey(keyCombo, context, key)) {
      /* If the key is assigned elsewhere, notify the calling method to show a message */
      return KEYCOMBO_ALREADY_ASSIGNED;
    } else {
      /* Add the key to the list to be mapped to the preference */
      keyCombos.add(keyCombo);
      updateKeyListAdapter(keyListAdapter, keyCombos, context);
    }
  }
  return CONSUME_EVENT;
}
 
Example 9
Source File: EmojiAltPhysicalKeyDetector.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
public void onKeyUp(@Nonnull final KeyEvent keyEvent) {
    if (DEBUG) {
        Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - considering " + keyEvent);
    }

    final int keyCode = keyEvent.getKeyCode();
    int metaState = keyEvent.getMetaState();
    if (KeyEvent.isModifierKey(keyCode)) {
         // Try restoring meta stat in case the released key was a modifier.
         // I am sure one can come up with scenarios to break this, but it
         // seems to work well in practice.
         metaState |= mMetaState;
    }

    final Pair<Integer, Integer> key = Pair.create(keyCode, metaState);
    if (mKeySet.contains(key)) {
        if (mCanFire) {
            if (!keyEvent.isCanceled()) {
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - firing action");
                }
                action();
            } else {
                // This key up event was a part of key combinations and
                // should be ignored.
                if (DEBUG) {
                    Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - canceled, ignoring action");
                }
            }
            mCanFire = false;
        }
    }

    if (mCanFire) {
        if (DEBUG) {
            Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - disabling action");
        }
        mCanFire = false;
    }
}
 
Example 10
Source File: DialerFilter.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean handled = false;

    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
        case KeyEvent.KEYCODE_DPAD_DOWN:
        case KeyEvent.KEYCODE_DPAD_LEFT:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        case KeyEvent.KEYCODE_ENTER:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            break;

        case KeyEvent.KEYCODE_DEL:
            switch (mMode) {
                case DIGITS_AND_LETTERS:
                    handled = mDigits.onKeyDown(keyCode, event);
                    handled &= mLetters.onKeyDown(keyCode, event);
                    break;

                case DIGITS_AND_LETTERS_NO_DIGITS:
                    handled = mLetters.onKeyDown(keyCode, event);
                    if (mLetters.getText().length() == mDigits.getText().length()) {
                        setMode(DIGITS_AND_LETTERS);
                    }
                    break;

                case DIGITS_AND_LETTERS_NO_LETTERS:
                    if (mDigits.getText().length() == mLetters.getText().length()) {
                        mLetters.onKeyDown(keyCode, event);
                        setMode(DIGITS_AND_LETTERS);
                    }
                    handled = mDigits.onKeyDown(keyCode, event);
                    break;

                case DIGITS_ONLY:
                    handled = mDigits.onKeyDown(keyCode, event);
                    break;

                case LETTERS_ONLY:
                    handled = mLetters.onKeyDown(keyCode, event);
                    break;
            }
            break;

        default:
            //mIsQwerty = msg.getKeyIsQwertyKeyboard();

            switch (mMode) {
                case DIGITS_AND_LETTERS:
                    handled = mLetters.onKeyDown(keyCode, event);

                    // pass this throw so the shift state is correct (for example,
                    // on a standard QWERTY keyboard, * and 8 are on the same key)
                    if (KeyEvent.isModifierKey(keyCode)) {
                        mDigits.onKeyDown(keyCode, event);
                        handled = true;
                        break;
                    }

                    // Only check to see if the digit is valid if the key is a printing key
                    // in the TextKeyListener. This prevents us from hiding the digits
                    // line when keys like UP and DOWN are hit.
                    // XXX note that KEYCODE_TAB is special-cased here for
                    // devices that share tab and 0 on a single key.
                    boolean isPrint = event.isPrintingKey();
                    if (isPrint || keyCode == KeyEvent.KEYCODE_SPACE
                            || keyCode == KeyEvent.KEYCODE_TAB) {
                        char c = event.getMatch(DialerKeyListener.CHARACTERS);
                        if (c != 0) {
                            handled &= mDigits.onKeyDown(keyCode, event);
                        } else {
                            setMode(DIGITS_AND_LETTERS_NO_DIGITS);
                        }
                    }
                    break;

                case DIGITS_AND_LETTERS_NO_LETTERS:
                case DIGITS_ONLY:
                    handled = mDigits.onKeyDown(keyCode, event);
                    break;

                case DIGITS_AND_LETTERS_NO_DIGITS:
                case LETTERS_ONLY:
                    handled = mLetters.onKeyDown(keyCode, event);
                    break;
            }
    }

    if (!handled) {
        return super.onKeyDown(keyCode, event);
    } else {
        return true;
    }
}
 
Example 11
Source File: TabHost.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
      * <p>Call setup() before adding tabs if loading TabHost using findViewById().
      * <i><b>However</i></b>: You do not need to call setup() after getTabHost()
      * in {@link android.app.TabActivity TabActivity}.
      * Example:</p>
<pre>mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
      */
    public void setup() {
        mTabWidget = findViewById(com.android.internal.R.id.tabs);
        if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
        }

        // KeyListener to attach to all tabs. Detects non-navigation keys
        // and relays them to the tab content.
        mTabKeyListener = new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (KeyEvent.isModifierKey(keyCode)) {
                    return false;
                }
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                    case KeyEvent.KEYCODE_DPAD_UP:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_TAB:
                    case KeyEvent.KEYCODE_SPACE:
                    case KeyEvent.KEYCODE_ENTER:
                        return false;

                }
                mTabContent.requestFocus(View.FOCUS_FORWARD);
                return mTabContent.dispatchKeyEvent(event);
            }

        };

        mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
            public void onTabSelectionChanged(int tabIndex, boolean clicked) {
                setCurrentTab(tabIndex);
                if (clicked) {
                    mTabContent.requestFocus(View.FOCUS_FORWARD);
                }
            }
        });

        mTabContent = findViewById(com.android.internal.R.id.tabcontent);
        if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is "
                            + "'android.R.id.tabcontent'");
        }
    }
 
Example 12
Source File: KeyComboManager.java    From talkback with Apache License 2.0 4 votes vote down vote up
/** Returns user friendly string representations of key combo code */
public String getKeyComboStringRepresentation(long keyComboCode) {
  if (keyComboCode == KeyComboModel.KEY_COMBO_CODE_UNASSIGNED) {
    return mContext.getString(R.string.keycombo_unassigned);
  }

  int triggerModifier = mKeyComboModel.getTriggerModifier();
  int modifier = getModifier(keyComboCode);
  int modifierWithoutTriggerModifier = modifier & ~triggerModifier;
  int keyCode = getKeyCode(keyComboCode);

  StringBuilder sb = new StringBuilder();

  // Append trigger modifier if key combo code contains it.
  if ((triggerModifier & modifier) != 0) {
    appendModifiers(triggerModifier, sb);
  }

  // Append modifier except trigger modifier.
  appendModifiers(modifierWithoutTriggerModifier, sb);

  // Append key code.
  if (keyCode > 0 && !KeyEvent.isModifierKey(keyCode)) {
    appendPlusSignIfNotEmpty(sb);

    switch (keyCode) {
      case KeyEvent.KEYCODE_DPAD_RIGHT:
        sb.append(mContext.getString(R.string.keycombo_key_arrow_right));
        break;
      case KeyEvent.KEYCODE_DPAD_LEFT:
        sb.append(mContext.getString(R.string.keycombo_key_arrow_left));
        break;
      case KeyEvent.KEYCODE_DPAD_UP:
        sb.append(mContext.getString(R.string.keycombo_key_arrow_up));
        break;
      case KeyEvent.KEYCODE_DPAD_DOWN:
        sb.append(mContext.getString(R.string.keycombo_key_arrow_down));
        break;
      default:
        String keyCodeString = KeyEvent.keyCodeToString(keyCode);
        if (keyCodeString != null) {
          String keyCodeNoPrefix;
          if (keyCodeString.startsWith(KEYCODE_PREFIX)) {
            keyCodeNoPrefix = keyCodeString.substring(KEYCODE_PREFIX.length());
          } else {
            keyCodeNoPrefix = keyCodeString;
          }
          sb.append(keyCodeNoPrefix.replace('_', ' '));
        }
        break;
    }
  }

  return sb.toString();
}