Keyboard.Key
public
static
class
Keyboard.Key
extends Object
| java.lang.Object | |
| ↳ | android.inputmethodservice.Keyboard.Key |
Class for describing the position and characteristics of a single key in the keyboard.
Summary
XML attributes | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
android:codes |
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character; May be an integer value, such as " |
|||||||||
android:horizontalGap |
This is deprecated.
Copy this definition into your own application project.
May be a dimension value, which is a floating point number appended with a
unit such as " May be a fractional value, which is a floating point number appended with
either % or %p, such as " |
|||||||||
android:iconPreview |
This is deprecated.
Copy this definition into your own application project.
May be a reference to another resource, in the form
" |
|||||||||
android:isModifier |
This is deprecated.
Copy this definition into your own application project.
May be a boolean value, such as " |
|||||||||
android:isRepeatable |
This is deprecated.
Copy this definition into your own application project.
May be a boolean value, such as " |
|||||||||
android:isSticky |
This is deprecated.
Copy this definition into your own application project.
May be a boolean value, such as " |
|||||||||
android:keyEdgeFlags |
This is deprecated.
Copy this definition into your own application project.
Must be one or more (separated by '|') of the following constant values.
|
|||||||||
android:keyHeight |
This is deprecated.
Copy this definition into your own application project.
May be a dimension value, which is a floating point number appended with a
unit such as " May be a fractional value, which is a floating point number appended with
either % or %p, such as " |
|||||||||
android:keyIcon |
This is deprecated.
Copy this definition into your own application project.
May be a reference to another resource, in the form
" |
|||||||||
android:keyLabel |
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character; |
|||||||||
android:keyOutputText |
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character; |
|||||||||
android:keyWidth |
This is deprecated.
Copy this definition into your own application project.
May be a dimension value, which is a floating point number appended with a
unit such as " May be a fractional value, which is a floating point number appended with
either % or %p, such as " |
|||||||||
android:popupCharacters |
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character; |
|||||||||
android:popupKeyboard |
This is deprecated.
Copy this definition into your own application project.
May be a reference to another resource, in the form
" |
|||||||||
Fields | |
|---|---|
public
int[] |
codes
All the key codes (unicode or custom code) that this key could generate, zero'th being the most important. |
public
int |
edgeFlags
Flags that specify the anchoring to edges of the keyboard for detecting touch events that are just out of the boundary of the key. |
public
int |
gap
The horizontal gap before this key |
public
int |
height
Height of the key, not including the gap |
public
Drawable |
icon
Icon to display instead of a label. |
public
Drawable |
iconPreview
Preview version of the icon, for the preview popup |
public
CharSequence |
label
Label to display |
public
boolean |
modifier
Whether this is a modifier key, such as Shift or Alt |
public
boolean |
on
If this is a sticky key, is it on? |
public
CharSequence |
popupCharacters
Popup characters |
public
int |
popupResId
If this key pops up a mini keyboard, this is the resource id for the XML layout for that keyboard. |
public
boolean |
pressed
The current pressed state of this key |
public
boolean |
repeatable
Whether this key repeats itself when held down |
public
boolean |
sticky
Whether this key is sticky, i.e., a toggle key |
public
CharSequence |
text
Text to output when pressed. |
public
int |
width
Width of the key, not including the gap |
public
int |
x
X coordinate of the key in the keyboard layout |
public
int |
y
Y coordinate of the key in the keyboard layout |
Public constructors | |
|---|---|
Key(Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser)
Create a key with the given top-left coordinate and extract its attributes from the XML parser. |
|
Key(Keyboard.Row parent)
Create an empty key with no attributes. |
|
Public methods | |
|---|---|
int[]
|
getCurrentDrawableState()
Returns the drawable state for the key, based on the current state and type of the key. |
boolean
|
isInside(int x, int y)
Detects if a point falls inside this key. |
void
|
onPressed()
Informs the key that it has been pressed, in case it needs to change its appearance or state. |
void
|
onReleased(boolean inside)
Changes the pressed state of the key. |
int
|
squaredDistanceFrom(int x, int y)
Returns the square of the distance between the center of the key and the given point. |
Inherited methods | |
|---|---|
XML attributes
android:codes
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
May be an integer value, such as "100".
The unicode value or comma-separated values that this key outputs.
android:horizontalGap
This is deprecated.
Copy this definition into your own application project.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
May be a fractional value, which is a floating point number appended with
either % or %p, such as "14.5%".
The % suffix always means a percentage of the base size;
the optional %p suffix provides a size relative to some parent container.
Default horizontal gap between keys.
android:iconPreview
This is deprecated.
Copy this definition into your own application project.
May be a reference to another resource, in the form
"@[+][package:]type/name" or a theme
attribute in the form
"?[package:]type/name".
The icon to show in the popup preview.
android:isModifier
This is deprecated.
Copy this definition into your own application project.
May be a boolean value, such as "true" or
"false".
Whether this is a modifier key such as Alt or Shift.
android:isRepeatable
This is deprecated.
Copy this definition into your own application project.
May be a boolean value, such as "true" or
"false".
Whether long-pressing on this key will make it repeat.
android:isSticky
This is deprecated.
Copy this definition into your own application project.
May be a boolean value, such as "true" or
"false".
Whether this is a toggle key.
android:keyEdgeFlags
This is deprecated.
Copy this definition into your own application project.
Must be one or more (separated by '|') of the following constant values.
| Constant | Value | Description |
|---|---|---|
| left | 1 | Key is anchored to the left of the keyboard.Copy this definition into your own application project. |
| right | 2 | Key is anchored to the right of the keyboard.Copy this definition into your own application project. |
Key edge flags.
android:keyHeight
This is deprecated.
Copy this definition into your own application project.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
May be a fractional value, which is a floating point number appended with
either % or %p, such as "14.5%".
The % suffix always means a percentage of the base size;
the optional %p suffix provides a size relative to some parent container.
Default height of a key, in pixels or percentage of display width.
android:keyIcon
This is deprecated.
Copy this definition into your own application project.
May be a reference to another resource, in the form
"@[+][package:]type/name" or a theme
attribute in the form
"?[package:]type/name".
The icon to display on the key instead of the label.
android:keyLabel
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
The label to display on the key.
android:keyOutputText
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
The string of characters to output when this key is pressed.
android:keyWidth
This is deprecated.
Copy this definition into your own application project.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
May be a fractional value, which is a floating point number appended with
either % or %p, such as "14.5%".
The % suffix always means a percentage of the base size;
the optional %p suffix provides a size relative to some parent container.
Default width of a key, in pixels or percentage of display width.
android:popupCharacters
This is deprecated.
Copy this definition into your own application project.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
The characters to display in the popup keyboard.
android:popupKeyboard
This is deprecated.
Copy this definition into your own application project.
May be a reference to another resource, in the form
"@[+][package:]type/name" or a theme
attribute in the form
"?[package:]type/name".
The XML keyboard layout of any popup keyboard.
Fields
codes
public int[] codes
All the key codes (unicode or custom code) that this key could generate, zero'th being the most important.
edgeFlags
public int edgeFlags
Flags that specify the anchoring to edges of the keyboard for detecting touch events
that are just out of the boundary of the key. This is a bit mask of
Keyboard.EDGE_LEFT, Keyboard.EDGE_RIGHT, Keyboard.EDGE_TOP and
Keyboard.EDGE_BOTTOM.
icon
public Drawable icon
Icon to display instead of a label. Icon takes precedence over a label
iconPreview
public Drawable iconPreview
Preview version of the icon, for the preview popup
modifier
public boolean modifier
Whether this is a modifier key, such as Shift or Alt
popupResId
public int popupResId
If this key pops up a mini keyboard, this is the resource id for the XML layout for that keyboard.
repeatable
public boolean repeatable
Whether this key repeats itself when held down
text
public CharSequence text
Text to output when pressed. This can be multiple characters, like ".com"
Public constructors
Key
public Key (Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser)
Create a key with the given top-left coordinate and extract its attributes from the XML parser.
| Parameters | |
|---|---|
res |
Resources: resources associated with the caller's context |
parent |
Keyboard.Row: the row that this key belongs to. The row must already be attached to
a Keyboard. |
x |
int: the x coordinate of the top-left |
y |
int: the y coordinate of the top-left |
parser |
XmlResourceParser: the XML parser containing the attributes for this key |
Key
public Key (Keyboard.Row parent)
Create an empty key with no attributes.
| Parameters | |
|---|---|
parent |
Keyboard.Row |
Public methods
getCurrentDrawableState
public int[] getCurrentDrawableState ()
Returns the drawable state for the key, based on the current state and type of the key.
| Returns | |
|---|---|
int[] |
the drawable state of the key. |
See also:
isInside
public boolean isInside (int x,
int y)Detects if a point falls inside this key.
| Parameters | |
|---|---|
x |
int: the x-coordinate of the point |
y |
int: the y-coordinate of the point |
| Returns | |
|---|---|
boolean |
whether or not the point falls inside the key. If the key is attached to an edge, it will assume that all points between the key and the edge are considered to be inside the key. |
onPressed
public void onPressed ()
Informs the key that it has been pressed, in case it needs to change its appearance or state.
See also:
onReleased
public void onReleased (boolean inside)
Changes the pressed state of the key.
Toggled state of the key will be flipped when all the following conditions are fulfilled:
- This is a sticky key, that is,
stickyistrue. - The parameter
insideistrue. Build.VERSION.SDK_INTis greater thanBuild.VERSION_CODES.LOLLIPOP_MR1.
| Parameters | |
|---|---|
inside |
boolean: whether the finger was released inside the key. Works only on Android M and
later. See the method document for details. |
See also:
squaredDistanceFrom
public int squaredDistanceFrom (int x,
int y)Returns the square of the distance between the center of the key and the given point.
| Parameters | |
|---|---|
x |
int: the x-coordinate of the point |
y |
int: the y-coordinate of the point |
| Returns | |
|---|---|
int |
the square of the distance of the point from the center of the key |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026-02-13 UTC.