An event which indicates that a keystroke occurred in a component.
This low-level event is generated by a component object (such as a text
field) when a key is pressed, released, or typed.
The event is passed to every KeyListener
or KeyAdapter
object which registered to receive such
events using the component's addKeyListener
method.
(KeyAdapter
objects implement the
KeyListener
interface.) Each such listener object
gets this KeyEvent
when the event occurs.
"Key typed" events are higher-level and generally do not depend on
the platform or keyboard layout. They are generated when a Unicode character
is entered, and are the preferred way to find out about character input.
In the simplest case, a key typed event is produced by a single key press
(e.g., 'a'). Often, however, characters are produced by series of key
presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to
key typed events may be many-to-one or many-to-many. Key releases are not
usually necessary to generate a key typed event, but there are some cases
where the key typed event is not generated until a key is released (e.g.,
entering ASCII sequences via the Alt-Numpad method in Windows).
No key typed events are generated for keys that don't generate Unicode
characters (e.g., action keys, modifier keys, etc.).
The getKeyChar method always returns a valid Unicode character or
CHAR_UNDEFINED. Character input is reported by KEY_TYPED events:
KEY_PRESSED and KEY_RELEASED events are not necessarily associated
with character input. Therefore, the result of the getKeyChar method
is guaranteed to be meaningful only for KEY_TYPED events.
For key pressed and key released events, the getKeyCode method returns
the event's keyCode. For key typed events, the getKeyCode method
always returns VK_UNDEFINED.
"Key pressed" and "key released" events are lower-level and depend
on the platform and keyboard layout. They are generated whenever a key is
pressed or released, and are the only way to find out about keys that don't
generate character input (e.g., action keys, modifier keys, etc.). The key
being pressed or released is indicated by the getKeyCode method, which returns
a virtual key code.
Virtual key codes are used to report which keyboard key has
been pressed, rather than a character generated by the combination
of one or more keystrokes (such as "A", which comes from shift and "a").
For example, pressing the Shift key will cause a KEY_PRESSED event
with a VK_SHIFT keyCode, while pressing the 'a' key will result in
a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event
will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
value of 'A' is generated.
Notes:
- Key combinations which do not result in Unicode characters, such as action
keys like F1 and the HELP key, do not generate KEY_TYPED events.
- Not all keyboards or systems are capable of generating all
virtual key codes. No attempt is made in Java to generate these keys
artificially.
- Virtual key codes do not identify a physical key: they depend on the
platform and keyboard layout. For example, the key that generates VK_Q
when using a U.S. keyboard layout will generate VK_A when using a French
keyboard layout.
- Not all characters have a keycode associated with them. For example,
there is no keycode for the question mark because there is no keyboard
for which it appears on the primary layer.
- In order to support the platform-independent handling of action keys,
the Java platform uses a few additional virtual key constants for functions
that would otherwise have to be recognized by interpreting virtual key codes
and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
is returned instead of VK_CONVERT with the ALT modifier.
WARNING: Aside from those keys that are defined by the Java language
(VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
constants. Sun reserves the right to change these values as needed
to accomodate a wider range of keyboards in the future.
Constructs a
KeyEvent
object.
Note that passing in an invalid id
results in
unspecified behavior. This method throws an
IllegalArgumentException
if source
is null
.
The event mask for selecting action events.
The event mask for selecting adjustment events.
The Alt key extended modifier constant.
The AltGraph key extended modifier constant.
The AltGraph key modifier constant.
The Alt key modifier constant.
It is recommended that ALT_DOWN_MASK be used instead.
The Mouse Button1 extended modifier constant.
The Mouse Button1 modifier constant.
It is recommended that BUTTON1_DOWN_MASK be used instead.
The Mouse Button2 extended modifier constant.
The Mouse Button2 modifier constant.
It is recommended that BUTTON2_DOWN_MASK be used instead.
Note that BUTTON2_MASK has the same value as ALT_MASK.
The Mouse Button3 extended modifier constant.
The Mouse Button3 modifier constant.
It is recommended that BUTTON3_DOWN_MASK be used instead.
Note that BUTTON3_MASK has the same value as META_MASK.
KEY_PRESSED and KEY_RELEASED events which do not map to a
valid Unicode character use this for the keyChar value.
The event mask for selecting component events.
The first number in the range of ids used for component events.
This event indicates that the component was rendered invisible.
The last number in the range of ids used for component events.
This event indicates that the component's position changed.
This event indicates that the component's size changed.
This event indicates that the component was made visible.
The event mask for selecting container events.
The Control key extended modifier constant.
The Control key modifier constant.
It is recommended that CTRL_DOWN_MASK be used instead.
The event mask for selecting focus events.
The event mask for selecting hierarchy bounds events.
The event mask for selecting hierarchy events.
The event mask for selecting input method events.
The event mask for selecting invocation events.
The event mask for selecting item events.
The event mask for selecting key events.
The first number in the range of ids used for key events.
The last number in the range of ids used for key events.
A constant indicating that the key pressed or released is in
the left key location (there is more than one possible location
for this key). Example: the left shift key.
A constant indicating that the key event originated on the
numeric keypad or with a virtual key corresponding to the
numeric keypad.
A constant indicating that the key pressed or released is in
the right key location (there is more than one possible location
for this key). Example: the right shift key.
A constant indicating that the key pressed or released
is not distinguished as the left or right version of a key,
and did not originate on the numeric keypad (or did not
originate with a virtual key corresponding to the numeric
keypad).
A constant indicating that the keyLocation is indeterminate
or not relevant.
KEY_TYPED
events do not have a keyLocation; this value
is used instead.
The "key pressed" event. This event is generated when a key
is pushed down.
The "key released" event. This event is generated when a key
is let up.
The "key typed" event. This event is generated when a character is
entered. In the simplest case, it is produced by a single key press.
Often, however, characters are produced by series of key presses, and
the mapping from key pressed events to key typed events may be
many-to-one or many-to-many.
The Meta key extended modifier constant.
The Meta key modifier constant.
It is recommended that META_DOWN_MASK be used instead.
The event mask for selecting mouse events.
The event mask for selecting mouse motion events.
The event mask for selecting mouse wheel events.
The event mask for selecting paint events.
The maximum value for reserved AWT event IDs. Programs defining
their own event IDs should use IDs greater than this value.
The Shift key extended modifier constant.
The Shift key modifier constant.
It is recommended that SHIFT_DOWN_MASK be used instead.
The event mask for selecting text events.
VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
Constant for the Accept or Commit function key.
Constant for the All Candidates function key.
Constant for the Alphanumeric function key.
Constant for the AltGraph function key.
Constant for the "@" key.
Constant for the back slash key, "\"
Constant for the Begin key.
Constant for the "^" key.
Constant for the close bracket key, "]"
Constant for the Code Input function key.
Constant for the ":" key.
Constant for the comma key, ","
Constant for the Compose function key.
Constant for the Microsoft Windows Context Menu key.
Constant for the Convert function key.
Constant for the "$" key.
Constant for the non-numpad down arrow key.
Constant for the equals key, "="
Constant for the Euro currency sign key.
Constant for the "!" key.
Constant for the F1 function key.
Constant for the F10 function key.
Constant for the F11 function key.
Constant for the F12 function key.
Constant for the F13 function key.
Constant for the F14 function key.
Constant for the F15 function key.
Constant for the F16 function key.
Constant for the F17 function key.
Constant for the F18 function key.
Constant for the F19 function key.
Constant for the F2 function key.
Constant for the F20 function key.
Constant for the F21 function key.
Constant for the F22 function key.
Constant for the F23 function key.
Constant for the F24 function key.
Constant for the F3 function key.
Constant for the F4 function key.
Constant for the F5 function key.
Constant for the F6 function key.
Constant for the F7 function key.
Constant for the F8 function key.
Constant for the F9 function key.
Constant for the Full-Width Characters function key.
Constant for the Half-Width Characters function key.
Constant for the Hiragana function key.
Constant for the input method on/off key.
Constant for the inverted exclamation mark key.
Constant for the Japanese-Hiragana function key.
This key switches to a Japanese input method and selects its Hiragana input mode.
Constant for the Japanese-Katakana function key.
This key switches to a Japanese input method and selects its Katakana input mode.
Constant for the Japanese-Roman function key.
This key switches to a Japanese input method and selects its Roman-Direct input mode.
Constant for the locking Kana function key.
This key locks the keyboard into a Kana layout.
Constant for the Katakana function key.
Constant for the numeric keypad down arrow key.
Constant for the numeric keypad left arrow key.
Constant for the numeric keypad right arrow key.
Constant for the numeric keypad up arrow key.
Constant for the non-numpad left arrow key.
Constant for the "(" key.
Constant for the minus key, "-"
Constant for the Don't Convert function key.
Constant for the "#" key.
Constant for the open bracket key, "["
Constant for the period key, "."
Constant for the "+" key.
Constant for the Previous Candidate function key.
Constant for the non-numpad right arrow key.
Constant for the ")" key.
Constant for the Roman Characters function key.
Constant for the semicolon key, ";"
This constant is obsolete, and is included only for backwards
compatibility.
Constant for the Numpad Separator key.
Constant for the forward slash key, "/"
This value is used to indicate that the keyCode is unknown.
KEY_TYPED events do not have a keyCode value; this value
is used instead.
Constant for the "_" key.
Constant for the non-numpad up arrow key.
Constant for the Microsoft Windows "Windows" key.
It is used for both the left and right version of the key.
The event mask for selecting window events.
The event mask for selecting window focus events.
The event mask for selecting window state events.
Consumes this event so that it will not be processed
in the default manner by the source which originated it.
Indicates whether some other object is "equal to" this one.
The equals
method implements an equivalence relation
on non-null object references:
- It is reflexive: for any non-null reference value
x
, x.equals(x)
should return
true
.
- It is symmetric: for any non-null reference values
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
- It is transitive: for any non-null reference values
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
- It is consistent: for any non-null reference values
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
- For any non-null reference value
x
,
x.equals(null)
should return false
.
The equals method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode method, which states
that equal objects must have equal hash codes.
Returns the runtime class of an object. That Class
object is the object that is locked by static synchronized
methods of the represented class.
Returns the originator of the event.
Returns the character associated with the key in this event.
For example, the
KEY_TYPED
event for shift + "a"
returns the value for "A".
KEY_PRESSED
and KEY_RELEASED
events
are not intended for reporting of character input. Therefore,
the values returned by this method are guaranteed to be
meaningful only for KEY_TYPED
events.
Returns the integer keyCode associated with the key in this event.
Returns the location of the key that originated this key event.
Some keys occur more than once on a keyboard, e.g. the left and
right shift keys. Additionally, some keys occur on the numeric
keypad. This provides a way of distinguishing such keys.
Returns a
String
describing the modifier key(s),
such as "Shift", or "Ctrl+Shift". These strings can be
localized by changing the
awt.properties
file.
Note that InputEvent.ALT_MASK
and
InputEvent.BUTTON2_MASK
have the same value,
so the string "Alt" is returned for both modifiers. Likewise,
InputEvent.META_MASK
and
InputEvent.BUTTON3_MASK
have the same value,
so the string "Meta" is returned for both modifiers.
Returns a String describing the keyCode, such as "HOME", "F1" or "A".
These strings can be localized by changing the awt.properties file.
Returns the modifier mask for this event.
Returns the extended modifier mask for this event.
Extended modifiers represent the state of all modal keys,
such as ALT, CTRL, META, and the mouse buttons just after
the event occurred
For example, if the user presses button 1 followed by
button 2, and then releases them in the same order,
the following sequence of events is generated:
MOUSE_PRESSED
: BUTTON1_DOWN_MASK
MOUSE_PRESSED
: BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK
MOUSE_RELEASED
: BUTTON2_DOWN_MASK
MOUSE_CLICKED
: BUTTON2_DOWN_MASK
MOUSE_RELEASED
:
MOUSE_CLICKED
:
It is not recommended to compare the return value of this method
using ==
because new modifiers can be added in the future.
For example, the appropriate way to check that SHIFT and BUTTON1 are
down, but CTRL is up is demonstrated by the following code:
int onmask = SHIFT_DOWN_MASK | BUTTON1_DOWN_MASK;
int offmask = CTRL_DOWN_MASK;
if (event.getModifiersEx() & (onmask | offmask) == onmask) {
...
}
The above code will work even if new modifiers are added.
Returns a String describing the extended modifier keys and
mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".
These strings can be localized by changing the
awt.properties file.
The object on which the Event initially occurred.
Returns the timestamp of when this event occurred.
Returns a hash code value for the object. This method is
supported for the benefit of hashtables such as those provided by
java.util.Hashtable
.
The general contract of hashCode
is:
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the hashCode method
must consistently return the same integer, provided no information
used in equals comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
- If two objects are equal according to the equals(Object)
method, then calling the
hashCode
method on each of
the two objects must produce the same integer result.
- It is not required that if two objects are unequal
according to the
method, then calling the hashCode method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by
class Object does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
Returns whether the key in this event is an "action" key.
Typically an action key does not fire a unicode character and is
not a modifier key.
Returns whether or not the Alt modifier is down on this event.
Returns whether or not the AltGraph modifier is down on this event.
Returns whether or not this event has been consumed.
Returns whether or not the Control modifier is down on this event.
Returns whether or not the Meta modifier is down on this event.
Returns whether or not the Shift modifier is down on this event.
Wakes up a single thread that is waiting on this object's
monitor. If any threads are waiting on this object, one of them
is chosen to be awakened. The choice is arbitrary and occurs at
the discretion of the implementation. A thread waits on an object's
monitor by calling one of the
wait
methods.
The awakened thread will not be able to proceed until the current
thread relinquishes the lock on this object. The awakened thread will
compete in the usual manner with any other threads that might be
actively competing to synchronize on this object; for example, the
awakened thread enjoys no reliable privilege or disadvantage in being
the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. A thread becomes the owner of the
object's monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a
synchronized
statement
that synchronizes on the object.
- For objects of type
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
Wakes up all threads that are waiting on this object's monitor. A
thread waits on an object's monitor by calling one of the
wait
methods.
The awakened threads will not be able to proceed until the current
thread relinquishes the lock on this object. The awakened threads
will compete in the usual manner with any other threads that might
be actively competing to synchronize on this object; for example,
the awakened threads enjoy no reliable privilege or disadvantage in
being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
Returns a parameter string identifying this event.
This method is useful for event logging and for debugging.
Set the keyChar value to indicate a logical character.
Set the keyCode value to indicate a physical key.
Set the modifiers to indicate additional keys that were held down
(e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
NOTE: use of this method is not recommended, because many AWT
implementations do not recognize modifier changes. This is
especially true for KEY_TYPED
events where the shift
modifier is changed.
Retargets an event to a new source. This method is typically used to
retarget an event to a lightweight child Component of the original
heavyweight source.
This method is intended to be used only by event targeting subsystems,
such as client-defined KeyboardFocusManagers. It is not for general
client use.
Returns a String representation of this object.
Causes current thread to wait until another thread invokes the
method or the
method for this object.
In other words, this method behaves exactly as if it simply
performs the call
wait(0).
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are
possible, and this method should always be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
}
This method should only be called by a thread that is the owner
of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
Causes current thread to wait until either another thread invokes the
method or the
method for this object, or a
specified amount of time has elapsed.
The current thread must own this object's monitor.
This method causes the current thread (call it T) to
place itself in the wait set for this object and then to relinquish
any and all synchronization claims on this object. Thread T
becomes disabled for thread scheduling purposes and lies dormant
until one of four things happens:
- Some other thread invokes the notify method for this
object and thread T happens to be arbitrarily chosen as
the thread to be awakened.
- Some other thread invokes the notifyAll method for this
object.
- Some other thread interrupts
thread T.
- The specified amount of real time has elapsed, more or less. If
timeout is zero, however, then real time is not taken into
consideration and the thread simply waits until notified.
The thread
T is then removed from the wait set for this
object and re-enabled for thread scheduling. It then competes in the
usual manner with other threads for the right to synchronize on the
object; once it has gained control of the object, all its
synchronization claims on the object are restored to the status quo
ante - that is, to the situation as of the time that the
wait
method was invoked. Thread
T then returns from the
invocation of the
wait method. Thus, on return from the
wait method, the synchronization state of the object and of
thread
T is exactly as it was when the
wait method
was invoked.
A thread can also wake up without being notified, interrupted, or
timing out, a so-called spurious wakeup. While this will rarely
occur in practice, applications must guard against it by testing for
the condition that should have caused the thread to be awakened, and
continuing to wait if the condition is not satisfied. In other words,
waits should always occur in loops, like this one:
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout);
... // Perform action appropriate to condition
}
(For more information on this topic, see Section 3.2.3 in Doug Lea's
"Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
2000), or Item 50 in Joshua Bloch's "Effective Java Programming
Language Guide" (Addison-Wesley, 2001).
If the current thread is
interrupted
by another thread
while it is waiting, then an InterruptedException is thrown.
This exception is not thrown until the lock status of this object has
been restored as described above.
Note that the wait method, as it places the current thread
into the wait set for this object, unlocks only this object; any
other objects on which the current thread may be synchronized remain
locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
Causes current thread to wait until another thread invokes the
method or the
method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the
method
of one argument. In particular,
wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until either of the
following two conditions has occurred:
- Another thread notifies threads waiting on this object's monitor
to wake up either through a call to the
notify
method
or the notifyAll
method.
- The timeout period, specified by
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the
monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are
possible, and this method should always be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout, nanos);
... // Perform action appropriate to condition
}
This method should only be called by a thread that is the owner
of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.