A
KeyEvent
indicates that a key was pressed, released, or typed a character.
Most of the keys on a keyboard generate events when they are
pressed or released, but only keys or combinations of keys
(e.g., Shift a) that correspond to Unicode characters generate
events that indicate a character was typed.
The fields in a
KeyEvent
are:
| id |
An
Object
that must be an
int
or
String,
that identifies the type of this event.
A value that is a
String
must be the name of an event handler that can process this event.
A value that is an
int
must be a number that the
yoix.event.HandlerID
dictionary associates with an event handler that can process this event.
In practice,
id
is only used when you build an event that gets handed to
postEvent,
and in that case the value assigned to
id
is almost always a
String.
| | keychar |
An
int
that is the Unicode character represented by this event, or
-1
if there is no corresponding Unicode character.
| | keycode |
An
int
that represents the virtual key code for the key that was pressed or released.
Virtual key codes are defined in the
KeyCode
dictionary that can be found in
yoix.awt
or
yoix.swing.
| | keystring |
A
String
representation of the Unicode character stored in
keychar
or the empty string (i.e.,
"")
if
keychar
is
-1,
which means it does not represent a Unicode character.
| | modifiers |
An
int
that is a bitmask that always records the state of the keyboard modifiers
when the event occurred.
modifiers
also tries to record the state of the mouse buttons at the time of the event.
Unfortunately, mouse button and keyboard modifier flags overlap
(e.g.,
BUTTON2_MASK
and
ALT_MASK
match), so
modifiers
can not record the state of all mouse buttons, however reliable information about
them is available in
modifiersdown.
modifiers
and
modifiersdown
use disjoint sets of bits as flags, so make sure you test
modifiers
using some combination of
BUTTON1_MASK,
BUTTON2_MASK,
BUTTON3_MASK,
SHIFT_MASK,
CTRL_MASK,
ALT_MASK,
META_MASK,
and
ALT_GRAPH_MASK,
which are defined in
yoix.awt
and
yoix.swing.
Also available are
BUTTON_MASK
and
KEY_MASK,
which are convenient combinations that can be used to test for
any mouse button or any keyboard modifier.
| | modifiersdown |
An
int
that is a bitmask that represents the state of all keyboard modifiers
and mouse buttons when the event occurred.
modifiersdown
and
modifiers
use disjoint sets of bits as flags, so make sure you test
modifiersdown
using some combination of
BUTTON1_DOWN_MASK,
BUTTON2_DOWN_MASK,
BUTTON3_DOWN_MASK,
SHIFT_DOWN_MASK,
CTRL_DOWN_MASK,
ALT_DOWN_MASK,
META_DOWN_MASK,
and
ALT_GRAPH_DOWN_MASK,
which are defined in
yoix.awt
and
yoix.swing.
Also available are
BUTTON_DOWN_MASK
and
KEY_DOWN_MASK,
which are convenient combinations that can be used to test for
any mouse button or any keyboard modifier.
| | when |
A
double
that is a timestamp that represents when the event occurred.
|
Most components can receive
KeyEvents,
but they only arrive if the component has defined
one or more of the event handlers listed below.
| |
| Event Handlers: |
keyPressed,
keyReleased,
keyTyped
| | |
| Example: |
The program,
import yoix.*.*;
VM.exitmodel = 0; // so main thread does not exit
JFrame frame = {
keyPressed(Event e) {
printf("keyPressed: %O\n", e);
}
keyTyped(Event e) {
printf("keyTyped: %O\n", e);
if (e.keychar == 'q')
exit(0);
}
};
frame.visible = TRUE;
KeyEvent event = {
String id = "keyPressed";
int keychar = 'X';
int keycode = KeyCode.VK_X;
int when = 12345;
};
printf("Posting %s\n", event.id);
postEvent(event, frame);
prints
Posting keyPressed
keyPressed: Event[6:0]
id=401
keychar=88
keycode=88
keystring=^"X"
>modifiers=0
when=12345.0
on standard output and then dumps all real
keyPressed
and
keyTyped
events until you type a
q.
| | |
| See Also: |
ActionEvent,
AdjustmentEvent,
CaretEvent,
ChangeEvent,
ComponentEvent,
DragGestureEvent,
DragSourceEvent,
DropTargetEvent,
Event,
FocusEvent,
HyperlinkEvent,
InvocationEvent,
invokeLater,
isDispatchThread,
ItemEvent,
ListSelectionEvent,
MouseEvent,
MouseWheelEvent,
PaintEvent,
postEvent,
TextEvent,
TreeSelectionEvent,
WindowEvent
|
|