public abstract class MetaKeyKeyListener extends Object
Key listeners that care about meta state should inherit from this class; you should not instantiate this class directly in a client.
This class provides two mechanisms for tracking meta state that can be used together or independently.
handleKeyDown(long, int, KeyEvent)
and
getMetaState(long)
operate on a meta key state bit mask.onKeyDown(View, Editable, int, KeyEvent)
and
getMetaState(CharSequence, int)
operate on meta key state flags stored
as spans in an Editable
text buffer. The spans only describe the current
meta key state of the text editor; they do not carry any positional information.
The behavior of this class varies according to the keyboard capabilities
described by the KeyCharacterMap
of the keyboard device such as
the key modifier behavior
.
MetaKeyKeyListener
implements chorded and toggled key modifiers.
When key modifiers are toggled into a latched or locked state, the state
of the modifier is stored in the Editable
text buffer or in a
meta state integer managed by the client. These latched or locked modifiers
should be considered to be held in addition to those that the
keyboard already reported as being pressed in KeyEvent.getMetaState()
.
In other words, the MetaKeyKeyListener
augments the meta state
provided by the keyboard; it does not replace it. This distinction is important
to ensure that meta keys not handled by MetaKeyKeyListener
such as
KeyEvent.KEYCODE_CAPS_LOCK
or KeyEvent.KEYCODE_NUM_LOCK
are
taken into consideration.
To ensure correct meta key behavior, the following pattern should be used when mapping key codes to characters:
private char getUnicodeChar(TextKeyListener listener, KeyEvent event, Editable textBuffer) {
// Use the combined meta states from the event and the key listener.
int metaState = event.getMetaState() | listener.getMetaState(textBuffer);
return event.getUnicodeChar(metaState);
}
Modifier and Type | Field and Description |
---|---|
static int |
META_ALT_LOCKED
Flag that indicates that the ALT key is locked.
|
static int |
META_ALT_ON
Flag that indicates that the ALT key is on.
|
static int |
META_CAP_LOCKED
Flag that indicates that the SHIFT key is locked in CAPS mode.
|
static int |
META_SELECTING |
static int |
META_SHIFT_ON
Flag that indicates that the SHIFT key is on.
|
static int |
META_SYM_LOCKED
Flag that indicates that the SYM key is locked.
|
static int |
META_SYM_ON
Flag that indicates that the SYM key is on.
|
Constructor and Description |
---|
MetaKeyKeyListener() |
Modifier and Type | Method and Description |
---|---|
static long |
adjustMetaAfterKeypress(long state)
Call this method after you handle a keypress so that the meta
state will be reset to unshifted (if it is not still down)
or primed to be reset to unshifted (once it is released).
|
static void |
adjustMetaAfterKeypress(Spannable content)
Call this method after you handle a keypress so that the meta
state will be reset to unshifted (if it is not still down)
or primed to be reset to unshifted (once it is released).
|
static void |
clearMetaKeyState(Editable content,
int states) |
long |
clearMetaKeyState(long state,
int which)
Clears the state of the specified meta key if it is locked.
|
void |
clearMetaKeyState(View view,
Editable content,
int states) |
static int |
getMetaState(CharSequence text)
Gets the state of the meta keys.
|
static int |
getMetaState(CharSequence text,
int meta)
Gets the state of a particular meta key.
|
static int |
getMetaState(CharSequence text,
int meta,
KeyEvent event)
Gets the state of a particular meta key to use with a particular key event.
|
static int |
getMetaState(CharSequence text,
KeyEvent event)
Gets the state of the meta keys for a specific key event.
|
static int |
getMetaState(long state)
Gets the state of the meta keys.
|
static int |
getMetaState(long state,
int meta)
Gets the state of a particular meta key.
|
static long |
handleKeyDown(long state,
int keyCode,
KeyEvent event)
Handles presses of the meta keys.
|
static long |
handleKeyUp(long state,
int keyCode,
KeyEvent event)
Handles release of the meta keys.
|
static boolean |
isMetaTracker(CharSequence text,
Object what)
Returns true if this object is one that this class would use to
keep track of any meta state in the specified text.
|
static boolean |
isSelectingMetaTracker(CharSequence text,
Object what)
Returns true if this object is one that this class would use to
keep track of the selecting meta state in the specified text.
|
boolean |
onKeyDown(View view,
Editable content,
int keyCode,
KeyEvent event)
Handles presses of the meta keys.
|
boolean |
onKeyUp(View view,
Editable content,
int keyCode,
KeyEvent event)
Handles release of the meta keys.
|
static long |
resetLockedMeta(long state)
Call this if you are a method that ignores the locked meta state
(arrow keys, for example) and you handle a key.
|
protected static void |
resetLockedMeta(Spannable content)
Call this if you are a method that ignores the locked meta state
(arrow keys, for example) and you handle a key.
|
static void |
resetMetaState(Spannable text)
Resets all meta state to inactive.
|
static void |
startSelecting(View view,
Spannable content)
Start selecting text.
|
static void |
stopSelecting(View view,
Spannable content)
Stop selecting text.
|
public static final int META_SHIFT_ON
KeyEvent.META_SHIFT_ON
.public static final int META_ALT_ON
KeyEvent.META_ALT_ON
.public static final int META_SYM_ON
KeyEvent.META_SYM_ON
.public static final int META_CAP_LOCKED
public static final int META_ALT_LOCKED
public static final int META_SYM_LOCKED
public static final int META_SELECTING
public static void resetMetaState(Spannable text)
public static final int getMetaState(CharSequence text)
text
- the buffer in which the meta key would have been pressed.public static final int getMetaState(CharSequence text, KeyEvent event)
text
- the buffer in which the meta key would have been pressed.event
- the event for which to evaluate the meta state.public static final int getMetaState(CharSequence text, int meta)
meta
- META_SHIFT_ON, META_ALT_ON, META_SYM_ONtext
- the buffer in which the meta key would have been pressed.public static final int getMetaState(CharSequence text, int meta, KeyEvent event)
meta
- META_SHIFT_ON, META_ALT_ON, META_SYM_ONtext
- the buffer in which the meta key would have been pressed.event
- the event for which to evaluate the meta state.public static void adjustMetaAfterKeypress(Spannable content)
public static boolean isMetaTracker(CharSequence text, Object what)
public static boolean isSelectingMetaTracker(CharSequence text, Object what)
protected static void resetLockedMeta(Spannable content)
public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event)
public static void startSelecting(View view, Spannable content)
public static void stopSelecting(View view, Spannable content)
Selection.setSelection(android.text.Spannable, int, int)
too.public boolean onKeyUp(View view, Editable content, int keyCode, KeyEvent event)
public static void clearMetaKeyState(Editable content, int states)
public static long resetLockedMeta(long state)
public static final int getMetaState(long state)
state
- the current meta state bits.public static final int getMetaState(long state, int meta)
state
- the current state bits.meta
- META_SHIFT_ON, META_ALT_ON, or META_SYM_ONpublic static long adjustMetaAfterKeypress(long state)
public static long handleKeyDown(long state, int keyCode, KeyEvent event)
public static long handleKeyUp(long state, int keyCode, KeyEvent event)
public long clearMetaKeyState(long state, int which)
state
- the meta key statewhich
- meta keys to clear, may be a combination of META_SHIFT_ON
,
META_ALT_ON
or META_SYM_ON
.