下面列出了android.view.KeyEvent#META_SHIFT_ON 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
if (adapter != null && currentTouchLayer != null) {
int metaState = event.getMetaState();
boolean isShiftPressed = (metaState & KeyEvent.META_SHIFT_ON) != 0;
if (!currentTouchIsSecondaryButton
|| !(adapter.getCurrentLayer() instanceof SelectionGroup)
|| ((SelectionGroup) adapter.getCurrentLayer()).getLayers().size() < 2) {
Layer selection = LayerUtils.selectionFrom(
adapter.getCurrentLayer(), currentTouchLayer, isShiftPressed);
adapter.setCurrentLayer(selection);
}
if (currentTouchIsSecondaryButton) {
currentTouchView.showContextMenu(event.getX(), event.getY());
}
}
return true;
}
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 int getMetaState() {
int metaState = 0;
if (isShiftPressed) {
metaState |= KeyEvent.META_SHIFT_ON;
}
if (isAltPressed) {
metaState |= KeyEvent.META_ALT_ON;
}
if (isCtrlPressed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
metaState |= KeyEvent.META_CTRL_ON;
}
return metaState;
}
@SuppressWarnings("deprecation")
void findItemsWithShortcutForKey(List<MenuItemImpl> items, int keyCode, KeyEvent event) {
final boolean qwerty = isQwertyMode();
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)
final boolean isKeyCodeMapped = event.getKeyData(possibleChars);
// The delete key is not mapped to '\b' so we treat it specially
if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) {
return;
}
// Look for an item whose shortcut is this key.
final int N = mItems.size();
for (int i = 0; i < N; i++) {
MenuItemImpl item = mItems.get(i);
if (item.hasSubMenu()) {
((MenuBuilder)item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event);
}
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut();
if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) &&
(shortcutChar != 0) &&
(shortcutChar == possibleChars.meta[0]
|| shortcutChar == possibleChars.meta[2]
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) &&
item.isEnabled()) {
items.add(item);
}
}
}
@SuppressWarnings("deprecation")
void findItemsWithShortcutForKey(List<MenuItemImpl> items, int keyCode, KeyEvent event) {
final boolean qwerty = isQwertyMode();
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)
final boolean isKeyCodeMapped = event.getKeyData(possibleChars);
// The delete key is not mapped to '\b' so we treat it specially
if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) {
return;
}
// Look for an item whose shortcut is this key.
final int N = mItems.size();
for (int i = 0; i < N; i++) {
MenuItemImpl item = mItems.get(i);
if (item.hasSubMenu()) {
((MenuBuilder)item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event);
}
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut();
if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) &&
(shortcutChar != 0) &&
(shortcutChar == possibleChars.meta[0]
|| shortcutChar == possibleChars.meta[2]
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) &&
item.isEnabled()) {
items.add(item);
}
}
}
@Override
public boolean onGenericMotionEvent(TextView widget, Spannable text, MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL: {
final float vscroll;
final float hscroll;
if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
vscroll = 0;
hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
} else {
vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
}
boolean handled = false;
if (hscroll < 0) {
handled |= scrollLeft(widget, text, (int)Math.ceil(-hscroll));
} else if (hscroll > 0) {
handled |= scrollRight(widget, text, (int)Math.ceil(hscroll));
}
if (vscroll < 0) {
handled |= scrollUp(widget, text, (int)Math.ceil(-vscroll));
} else if (vscroll > 0) {
handled |= scrollDown(widget, text, (int)Math.ceil(vscroll));
}
return handled;
}
}
}
return false;
}
@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 boolean onGenericMotionEvent(TextView widget, Spannable text, MotionEvent event) {
if ((ApiWrapper.getSourceOfEvent(event) & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL: {
final float vscroll;
final float hscroll;
if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
vscroll = 0;
hscroll = ApiWrapper.getAxisValue(event,MotionEvent.AXIS_VSCROLL);
} else {
vscroll = -ApiWrapper.getAxisValue(event,MotionEvent.AXIS_VSCROLL);
hscroll = ApiWrapper.getAxisValue(event,MotionEvent.AXIS_HSCROLL);
}
boolean handled = false;
if (hscroll < 0) {
handled |= scrollLeft(widget, text, (int)Math.ceil(-hscroll));
} else if (hscroll > 0) {
handled |= scrollRight(widget, text, (int)Math.ceil(hscroll));
}
if (vscroll < 0) {
handled |= scrollUp(widget, text, (int)Math.ceil(-vscroll));
if ( handled ){
widget.moveCursorToVisibleOffset();
}
} else if (vscroll > 0) {
handled |= scrollDown(widget, text, (int)Math.ceil(vscroll));
if ( handled ){
widget.moveCursorToVisibleOffset();
}
}
return handled;
}
}
}
return false;
}
@SuppressWarnings("deprecation")
void findItemsWithShortcutForKey(List<MenuItemImpl> items, int keyCode, KeyEvent event) {
final boolean qwerty = isQwertyMode();
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)
final boolean isKeyCodeMapped = event.getKeyData(possibleChars);
// The delete key is not mapped to '\b' so we treat it specially
if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) {
return;
}
// Look for an item whose shortcut is this key.
final int N = mItems.size();
for (int i = 0; i < N; i++) {
MenuItemImpl item = mItems.get(i);
if (item.hasSubMenu()) {
((MenuBuilder)item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event);
}
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut();
if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) &&
(shortcutChar != 0) &&
(shortcutChar == possibleChars.meta[0]
|| shortcutChar == possibleChars.meta[2]
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) &&
item.isEnabled()) {
items.add(item);
}
}
}
@Test
public void testMoveThumbFocus_tab_correctThumbHasFocus() {
slider.requestFocus();
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
KeyEventBuilder shiftTab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB, KeyEvent.META_SHIFT_ON);
sendKeyEventThereAndBack(tab, shiftTab);
}
@Test
public void testFocusFirstThumb_shiftTab_unhandled() {
slider.requestFocus();
slider.setFocusedThumbIndex(0);
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
tab.meta = KeyEvent.META_SHIFT_ON;
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
assertThat(handledDown).isFalse();
assertThat(handledUp).isFalse();
}
@SuppressWarnings("deprecation")
void findItemsWithShortcutForKey(List<MenuItemImpl> items, int keyCode, KeyEvent event) {
final boolean qwerty = isQwertyMode();
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)
final boolean isKeyCodeMapped = event.getKeyData(possibleChars);
// The delete key is not mapped to '\b' so we treat it specially
if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) {
return;
}
// Look for an item whose shortcut is this key.
final int N = mItems.size();
for (int i = 0; i < N; i++) {
MenuItemImpl item = mItems.get(i);
if (item.hasSubMenu()) {
((MenuBuilder)item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event);
}
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut();
if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) &&
(shortcutChar != 0) &&
(shortcutChar == possibleChars.meta[0]
|| shortcutChar == possibleChars.meta[2]
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) &&
item.isEnabled()) {
items.add(item);
}
}
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL: {
// Handle mouse (or ext. device) by shifting the page depending on the scroll
final float vscroll;
final float hscroll;
if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
vscroll = 0;
hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
} else {
vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
}
if (hscroll != 0 || vscroll != 0) {
boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
: (hscroll > 0 || vscroll > 0);
if (isForwardScroll) {
scrollRight();
} else {
scrollLeft();
}
return true;
}
}
}
}
return super.onGenericMotionEvent(event);
}
@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;
}
/**
* 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;
}
@SuppressWarnings("deprecation")
void findItemsWithShortcutForKey(List<MenuItemImpl> items, int keyCode, KeyEvent event) {
final boolean qwerty = isQwertyMode();
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)
final boolean isKeyCodeMapped = event.getKeyData(possibleChars);
// The delete key is not mapped to '\b' so we treat it specially
if (!isKeyCodeMapped && (keyCode != KeyEvent.KEYCODE_DEL)) {
return;
}
// Look for an item whose shortcut is this key.
final int N = mItems.size();
for (int i = 0; i < N; i++) {
MenuItemImpl item = mItems.get(i);
if (item.hasSubMenu()) {
((MenuBuilder)item.getSubMenu()).findItemsWithShortcutForKey(items, keyCode, event);
}
final char shortcutChar = qwerty ? item.getAlphabeticShortcut() : item.getNumericShortcut();
if (((metaState & (KeyEvent.META_SHIFT_ON | KeyEvent.META_SYM_ON)) == 0) &&
(shortcutChar != 0) &&
(shortcutChar == possibleChars.meta[0]
|| shortcutChar == possibleChars.meta[2]
|| (qwerty && shortcutChar == '\b' &&
keyCode == KeyEvent.KEYCODE_DEL)) &&
item.isEnabled()) {
items.add(item);
}
}
}
@Override
public boolean onGenericMotionEvent(TextView widget, Spannable text, MotionEvent event) {
if ((ApiWrapper.getSourceOfEvent(event) & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL: {
final float vscroll;
final float hscroll;
if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
vscroll = 0;
hscroll = ApiWrapper.getAxisValue(event,MotionEvent.AXIS_VSCROLL);
} else {
vscroll = -ApiWrapper.getAxisValue(event,MotionEvent.AXIS_VSCROLL);
hscroll = ApiWrapper.getAxisValue(event,MotionEvent.AXIS_HSCROLL);
}
boolean handled = false;
if (hscroll < 0) {
handled |= scrollLeft(widget, text, (int)Math.ceil(-hscroll));
} else if (hscroll > 0) {
handled |= scrollRight(widget, text, (int)Math.ceil(hscroll));
}
if (vscroll < 0) {
handled |= scrollUp(widget, text, (int)Math.ceil(-vscroll));
if ( handled ){
widget.moveCursorToVisibleOffset();
}
} else if (vscroll > 0) {
handled |= scrollDown(widget, text, (int)Math.ceil(vscroll));
if ( handled ){
widget.moveCursorToVisibleOffset();
}
}
return handled;
}
}
}
return false;
}
/**
* This method should be called when overriding from
* {@link android.app.Activity#onProvideKeyboardShortcuts(List, android.view.Menu, int)}
* in an activity. It will return a list of the possible shortcuts. If
* someone adds a shortcut they also need to add an explanation in the
* appropriate group in this method so the user can see it when this method
* is called.
*
* @param context We need an activity so we can call the strings from our
* resource.
* @return a list of shortcuts organized into groups.
*/
@TargetApi(Build.VERSION_CODES.N)
public static List<KeyboardShortcutGroup> createShortcutGroup(Context context) {
final int ctrlShift = KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON;
List<KeyboardShortcutGroup> shortcutGroups = new ArrayList<>();
KeyboardShortcutGroup tabShortcutGroup = new KeyboardShortcutGroup(
context.getString(R.string.keyboard_shortcut_tab_group_header));
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_open_new_tab,
KeyEvent.KEYCODE_N, KeyEvent.META_CTRL_ON);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_reopen_new_tab,
KeyEvent.KEYCODE_T, ctrlShift);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_new_incognito_tab,
KeyEvent.KEYCODE_N, ctrlShift);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_next_tab,
KeyEvent.KEYCODE_TAB, KeyEvent.META_CTRL_ON);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_prev_tab,
KeyEvent.KEYCODE_TAB, ctrlShift);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_close_tab,
KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON);
shortcutGroups.add(tabShortcutGroup);
KeyboardShortcutGroup chromeFeatureShortcutGroup = new KeyboardShortcutGroup(
context.getString(R.string.keyboard_shortcut_chrome_feature_group_header));
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_open_menu,
KeyEvent.KEYCODE_E, KeyEvent.META_ALT_ON);
addShortcut(context, chromeFeatureShortcutGroup,
R.string.keyboard_shortcut_bookmark_manager, KeyEvent.KEYCODE_B, ctrlShift);
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_history_manager,
KeyEvent.KEYCODE_H, KeyEvent.META_CTRL_ON);
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_find_bar,
KeyEvent.KEYCODE_F, KeyEvent.META_CTRL_ON);
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_address_bar,
KeyEvent.KEYCODE_L, KeyEvent.META_CTRL_ON);
shortcutGroups.add(chromeFeatureShortcutGroup);
KeyboardShortcutGroup webpageShortcutGroup = new KeyboardShortcutGroup(
context.getString(R.string.keyboard_shortcut_webpage_group_header));
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_print_page,
KeyEvent.KEYCODE_P, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_reload_page,
KeyEvent.KEYCODE_R, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_reload_no_cache,
KeyEvent.KEYCODE_R, ctrlShift);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_bookmark_page,
KeyEvent.KEYCODE_D, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_zoom_in,
KeyEvent.KEYCODE_EQUALS, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_zoom_out,
KeyEvent.KEYCODE_MINUS, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_reset_zoom,
KeyEvent.KEYCODE_0, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_help_center,
KeyEvent.KEYCODE_SLASH, ctrlShift);
shortcutGroups.add(webpageShortcutGroup);
return shortcutGroups;
}