下面列出了android.view.KeyEvent#META_ALT_ON 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void loadTriggerModifierFromPreferences() {
SharedPreferences prefs = SharedPreferencesUtils.getSharedPreferences(mContext);
int defaultTriggerModifier =
mIsArc
? R.string.trigger_modifier_meta_entry_value
: R.string.trigger_modifier_alt_entry_value;
if (!prefs.contains(getPreferenceKeyForTriggerModifier())) {
// Store default value in preferences to show it in preferences UI.
prefs
.edit()
.putString(
getPreferenceKeyForTriggerModifier(), mContext.getString(defaultTriggerModifier))
.apply();
}
String triggerModifier =
prefs.getString(
getPreferenceKeyForTriggerModifier(), mContext.getString(defaultTriggerModifier));
if (triggerModifier.equals(mContext.getString(R.string.trigger_modifier_alt_entry_value))) {
mTriggerModifier = KeyEvent.META_ALT_ON;
} else if (triggerModifier.equals(
mContext.getString(R.string.trigger_modifier_meta_entry_value))) {
mTriggerModifier = KeyEvent.META_META_ON;
}
}
private static int getModifiers(int metaState) {
int modifiers = 0;
if ((metaState & KeyEvent.META_SHIFT_ON) != 0) {
modifiers |= WebInputEventModifier.SHIFT_KEY;
}
if ((metaState & KeyEvent.META_ALT_ON) != 0) {
modifiers |= WebInputEventModifier.ALT_KEY;
}
if ((metaState & KeyEvent.META_CTRL_ON) != 0) {
modifiers |= WebInputEventModifier.CONTROL_KEY;
}
if ((metaState & KeyEvent.META_CAPS_LOCK_ON) != 0) {
modifiers |= WebInputEventModifier.CAPS_LOCK_ON;
}
if ((metaState & KeyEvent.META_NUM_LOCK_ON) != 0) {
modifiers |= WebInputEventModifier.NUM_LOCK_ON;
}
return modifiers;
}
private static int getModifiers(int metaState) {
int modifiers = 0;
if ((metaState & KeyEvent.META_SHIFT_ON) != 0) {
modifiers |= sModifierShift;
}
if ((metaState & KeyEvent.META_ALT_ON) != 0) {
modifiers |= sModifierAlt;
}
if ((metaState & KeyEvent.META_CTRL_ON) != 0) {
modifiers |= sModifierCtrl;
}
if ((metaState & KeyEvent.META_CAPS_LOCK_ON) != 0) {
modifiers |= sModifierCapsLockOn;
}
if ((metaState & KeyEvent.META_NUM_LOCK_ON) != 0) {
modifiers |= sModifierNumLockOn;
}
return modifiers;
}
private static int getModifiers(int metaState) {
int modifiers = 0;
if ((metaState & KeyEvent.META_SHIFT_ON) != 0) {
modifiers |= sModifierShift;
}
if ((metaState & KeyEvent.META_ALT_ON) != 0) {
modifiers |= sModifierAlt;
}
if ((metaState & KeyEvent.META_CTRL_ON) != 0) {
modifiers |= sModifierCtrl;
}
if ((metaState & KeyEvent.META_CAPS_LOCK_ON) != 0) {
modifiers |= sModifierCapsLockOn;
}
if ((metaState & KeyEvent.META_NUM_LOCK_ON) != 0) {
modifiers |= sModifierNumLockOn;
}
return modifiers;
}
@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
// Get all items that can be associated directly or indirectly with the keyCode
ArrayList<MenuItemImpl> items = mTempShortcutItemList;
items.clear();
findItemsWithShortcutForKey(items, keyCode, event);
if (items.isEmpty()) {
return null;
}
final int metaState = event.getMetaState();
final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
// Get the chars associated with the keyCode (i.e using any chording combo)
event.getKeyData(possibleChars);
// If we have only one element, we can safely returns it
final int size = items.size();
if (size == 1) {
return items.get(0);
}
final boolean qwerty = isQwertyMode();
// If we found more than one item associated with the key,
// we have to return the exact match
for (int i = 0; i < size; i++) {
final MenuItemImpl item = items.get(i);
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
item.getNumericShortcut();
if ((shortcutChar == possibleChars.meta[0] &&
(metaState & KeyEvent.META_ALT_ON) == 0)
|| (shortcutChar == possibleChars.meta[2] &&
(metaState & KeyEvent.META_ALT_ON) != 0)
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) {
return item;
}
}
return null;
}
private int getMetaState(boolean shifted) {
int meta = 0;
if (shifted) meta |= KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_LEFT_ON;
if (mModCtrl) meta |= KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON;
if (mModAlt) meta |= KeyEvent.META_ALT_ON | KeyEvent.META_ALT_LEFT_ON;
if (mModMeta) meta |= KeyEvent.META_META_ON | KeyEvent.META_META_LEFT_ON;
return meta;
}
private void sendAltKey(InputConnection ic, boolean isDown, boolean chording) {
if (chording && delayChordingAltModifier()) return;
int key = sKeyboardSettings.chordingAltKey;
if (key == 0) key = KeyEvent.KEYCODE_ALT_LEFT;
int meta = KeyEvent.META_ALT_ON | KeyEvent.META_ALT_LEFT_ON;
if (isDown) {
sendKeyDown(ic, key, meta);
} else {
sendKeyUp(ic, key, meta);
}
}
/**
* 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 int normalizeMetaState(int metaState) {
if ((metaState & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_SHIFT_ON;
}
if ((metaState & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_ALT_ON;
}
return metaState & META_ALL_MASK;
}
@Override
public int normalizeMetaState(int metaState) {
if ((metaState & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_SHIFT_ON;
}
if ((metaState & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_ALT_ON;
}
return metaState & META_ALL_MASK;
}
@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
// Get all items that can be associated directly or indirectly with the keyCode
ArrayList<MenuItemImpl> items = mTempShortcutItemList;
items.clear();
findItemsWithShortcutForKey(items, keyCode, event);
if (items.isEmpty()) {
return null;
}
final int metaState = event.getMetaState();
final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
// Get the chars associated with the keyCode (i.e using any chording combo)
event.getKeyData(possibleChars);
// If we have only one element, we can safely returns it
final int size = items.size();
if (size == 1) {
return items.get(0);
}
final boolean qwerty = isQwertyMode();
// If we found more than one item associated with the key,
// we have to return the exact match
for (int i = 0; i < size; i++) {
final MenuItemImpl item = items.get(i);
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
item.getNumericShortcut();
if ((shortcutChar == possibleChars.meta[0] &&
(metaState & KeyEvent.META_ALT_ON) == 0)
|| (shortcutChar == possibleChars.meta[2] &&
(metaState & KeyEvent.META_ALT_ON) != 0)
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) {
return item;
}
}
return null;
}
@Override
public int normalizeMetaState(int metaState) {
if ((metaState & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_SHIFT_ON;
}
if ((metaState & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_ALT_ON;
}
return metaState & META_ALL_MASK;
}
@Override
public int normalizeMetaState(int metaState) {
if ((metaState & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_SHIFT_ON;
}
if ((metaState & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_ALT_ON;
}
return metaState & META_ALL_MASK;
}
@Override
public int normalizeMetaState(int metaState) {
if ((metaState & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_SHIFT_ON;
}
if ((metaState & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON)) != 0) {
metaState |= KeyEvent.META_ALT_ON;
}
return metaState & META_ALL_MASK;
}
@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
// Get all items that can be associated directly or indirectly with the keyCode
ArrayList<MenuItemImpl> items = mTempShortcutItemList;
items.clear();
findItemsWithShortcutForKey(items, keyCode, event);
if (items.isEmpty()) {
return null;
}
final int metaState = event.getMetaState();
final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
// Get the chars associated with the keyCode (i.e using any chording combo)
event.getKeyData(possibleChars);
// If we have only one element, we can safely returns it
final int size = items.size();
if (size == 1) {
return items.get(0);
}
final boolean qwerty = isQwertyMode();
// If we found more than one item associated with the key,
// we have to return the exact match
for (int i = 0; i < size; i++) {
final MenuItemImpl item = items.get(i);
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
item.getNumericShortcut();
if ((shortcutChar == possibleChars.meta[0] &&
(metaState & KeyEvent.META_ALT_ON) == 0)
|| (shortcutChar == possibleChars.meta[2] &&
(metaState & KeyEvent.META_ALT_ON) != 0)
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) {
return item;
}
}
return null;
}
@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
// Get all items that can be associated directly or indirectly with the keyCode
ArrayList<MenuItemImpl> items = mTempShortcutItemList;
items.clear();
findItemsWithShortcutForKey(items, keyCode, event);
if (items.isEmpty()) {
return null;
}
final int metaState = event.getMetaState();
final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
// Get the chars associated with the keyCode (i.e using any chording combo)
event.getKeyData(possibleChars);
// If we have only one element, we can safely returns it
final int size = items.size();
if (size == 1) {
return items.get(0);
}
final boolean qwerty = isQwertyMode();
// If we found more than one item associated with the key,
// we have to return the exact match
for (int i = 0; i < size; i++) {
final MenuItemImpl item = items.get(i);
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
item.getNumericShortcut();
if ((shortcutChar == possibleChars.meta[0] &&
(metaState & KeyEvent.META_ALT_ON) == 0)
|| (shortcutChar == possibleChars.meta[2] &&
(metaState & KeyEvent.META_ALT_ON) != 0)
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) {
return item;
}
}
return null;
}
@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
// Get all items that can be associated directly or indirectly with the keyCode
ArrayList<MenuItemImpl> items = mTempShortcutItemList;
items.clear();
findItemsWithShortcutForKey(items, keyCode, event);
if (items.isEmpty()) {
return null;
}
final int metaState = event.getMetaState();
final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
// Get the chars associated with the keyCode (i.e using any chording combo)
event.getKeyData(possibleChars);
// If we have only one element, we can safely returns it
final int size = items.size();
if (size == 1) {
return items.get(0);
}
final boolean qwerty = isQwertyMode();
// If we found more than one item associated with the key,
// we have to return the exact match
for (int i = 0; i < size; i++) {
final MenuItemImpl item = items.get(i);
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
item.getNumericShortcut();
if ((shortcutChar == possibleChars.meta[0] &&
(metaState & KeyEvent.META_ALT_ON) == 0)
|| (shortcutChar == possibleChars.meta[2] &&
(metaState & KeyEvent.META_ALT_ON) != 0)
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) {
return item;
}
}
return null;
}
/**
* 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) {
// Log.v("SpartacusRex","SOFT : onKeyDown "+ keyCode+" "+event.getMetaState());
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);
}
/**
* 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);
}
/**
* Create a string that describes the extended key code. This string can be shown to the user to
* indicate the current choice of key.
*
* @param extendedKeyCode The key code to describe
* @param context The current Context
* @return A description of the key code
*/
public static String describeExtendedKeyCode(long extendedKeyCode, Context context) {
if (extendedKeyCode == INVALID_EXTENDED_KEY_CODE) {
return context.getString(R.string.no_key_assigned);
}
if (extendedKeyCode == KEYCODE_SCREEN_SWITCH) {
return context.getString(R.string.name_of_screen_switch);
}
/* If meta keys are pressed, build a string to represent this combination of keys */
StringBuilder keystrokeDescriptionBuilder = new StringBuilder();
if ((extendedKeyCode & (((long) KeyEvent.META_CTRL_ON) << 32)) != 0) {
keystrokeDescriptionBuilder.append(
context.getString(R.string.key_combo_preference_control_plus));
}
if ((extendedKeyCode & (((long) KeyEvent.META_ALT_ON) << 32)) != 0) {
keystrokeDescriptionBuilder.append(context.getString(R.string.key_combo_preference_alt_plus));
}
if ((extendedKeyCode & (((long) KeyEvent.META_SHIFT_ON) << 32)) != 0) {
keystrokeDescriptionBuilder.append(
context.getString(R.string.key_combo_preference_shift_plus));
}
/* Try to obtain a localized representation of the key */
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, (int) extendedKeyCode);
char displayLabel = keyEvent.getDisplayLabel();
if (displayLabel != 0 && !Character.isWhitespace(displayLabel)) {
keystrokeDescriptionBuilder.append(displayLabel);
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
keystrokeDescriptionBuilder.append(context.getString(R.string.name_of_space_bar));
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
keystrokeDescriptionBuilder.append(context.getString(R.string.name_of_enter_key));
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_TAB) {
keystrokeDescriptionBuilder.append(context.getString(R.string.name_of_tab_key));
} else {
/* Fall back on non-localized descriptions */
keystrokeDescriptionBuilder.append(KeyEvent.keyCodeToString((int) extendedKeyCode));
}
return keystrokeDescriptionBuilder.toString();
}