public abstract class IlvAccelerator extends Object implements Serializable
IlvAccelerator
is the abstract base class for classes that
define for a manager specific actions triggered by a specific keyboard event.
An accelerator defines the action to be performed when a predefined keyboard
event occurs, such as the pressing of a specific key sequence. Accelerators
are attached to a manager by calling to
IlvManager.addAccelerator(IlvAccelerator)
.
The ilog.views.accelerator
package provides ready made accelerators
that handle zoom, rotation, scrolling, duplication and deletion actions.
Subclass IlvAccelerator
to make your own custom accelerators.
new IlvZoomInAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_UNDEFINED, '+', 0);This accelerator works for all '+' keys and is independent from the keyboard layout. As converse, the following code allocates an accelerator that reacts on pressing the '+' key on the number pad:
new IlvZoomOutAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_ADD, KeyEvent.CHAR_UNDEFINED, 0);
Notice that besides the number pad, most keyboard layouts have also other '+' keys. Different keyboard layouts may produce different key codes for the '+' key. A US keyboard may produce the same key code for '+' and '=' when using the key that is shared for both characters, while a German keyboard may produce the same key code for '+' and '*' because it has a key that is shared for those characters.
The following code example shows how to do the following:
IlvAccelerator
subclass// Create the manager: manager = new IlvManager(); // Create the view associated to the manager: mgrview = new IlvManagerView(manager); mgrview.setBackground(Color.white); IlvGraphic obj; obj = new IlvRectangle(new IlvRect(60, 30, 50, 50), false, true); manager.addObject(obj, false); obj = new IlvRectangle(new IlvRect(140, 50, 50, 50), false, true); manager.addObject(obj, false); // Install zoom accelerators. // Press CTRL and + to zoom in, CTRL and - to zoom out. manager.addAccelerator( new IlvZoomOutAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_UNDEFINED, '-', KeyEvent.CTRL_MASK)); manager.addAccelerator( new IlvZoomInAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_UNDEFINED, '+', KeyEvent.CTRL_MASK)); // Install the select interactor on the view. // This is used to select graphic objects with the mouse that can then be // deleted using the custom delete interactor. IlvSelectInteractor inter = new IlvSelectInteractor(); inter.setOpaqueMove(true); mgrview.setInteractor(inter); // Add the delete selection accelerator. // Press DEL to delete the selected objects. manager.addAccelerator( new IlvDeleteSelectionAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_DELETE, KeyEvent.CHAR_UNDEFINED, 0)); // Add your own custom accelerator. // Press BACKSPACE to print the number of objects in the manager // manager.addAccelerator( new IlvAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_BACK_SPACE, 0) { protected boolean handleEvent(IlvManagerView v) { int n = v.getManager().getCardinal(); System.out.println("Number of objects: " + n); return true; } });
For information about how to treat events in a manager, see Handling Events. The following code examples show how to handle user events:
Constructor and Description |
---|
IlvAccelerator(int type,
int keyCode,
char keyChar,
int modifiers,
boolean consumeEvents)
Creates a new accelerator.
|
IlvAccelerator(int type,
int keyCode,
int modifiers)
Creates a new accelerator.
|
IlvAccelerator(int type,
int keyCode,
int modifiers,
boolean consumeEvents)
Creates a new accelerator.
|
IlvAccelerator(KeyStroke keystroke,
boolean consumeEvents)
Creates a new accelerator associated with the specified key stroke.
|
Modifier and Type | Method and Description |
---|---|
static char |
CtrlChar(char c)
Returns the key code corresponding to the Ctrl+char event.
|
int |
getEventType()
Returns the type of the AWT event as provided
in the constructor.
|
int |
getKeyChar()
Returns the character of the key event as provided
in the constructor.
|
int |
getKeyCode()
Returns the code of the key event as provided
in the constructor.
|
int |
getModifier()
Returns the modifier of the event as provided
in the constructor.
|
protected abstract boolean |
handleEvent(IlvManagerView view)
Handles the accelerator.
|
boolean |
isConsumeEvents()
Returns whether the accelerator consumes events.
|
void |
setConsumeEvents(boolean enable)
Sets whether the accelerator consumes events.
|
public IlvAccelerator(KeyStroke keystroke, boolean consumeEvents)
keystroke
- The key stroke associated with the accelerator.consumeEvents
- If true
, the accelerator handles and consumes the
event, except when the consume flag of the event is already set
to true
. If false
, the accelerator
ignores the event's consume flag, and handles all events without
changing the consume flag.public IlvAccelerator(int type, int keyCode, char keyChar, int modifiers, boolean consumeEvents)
KeyEvent.CHAR_UNDEFINED
as key character.
To indicate that only the key character should be checked, pass
KeyEvent.VK_UNDEFINED
as key code.
If you are creating accelerators that have the same key code or key character
and have intersecting sets of modifiers, then use IlvAccelerator(KeyStroke, boolean)
instead. An example is if you want to define an accelerator that responds to a
key character while the CTRL modifier is pressed and a different accelerator that
will respond to the same key character while both the CTRL and ALT modifiers are
pressed. Accelerators created from key strokes match more accurately against
modifier combinations.
type
- The type of the AWT event.keyCode
- The code associated with the key,
or KeyEvent.VK_UNDEFINED
.keyChar
- The character associated with the key,
or KeyEvent.CHAR_UNDEFINED
.modifiers
- The modifiers of the key.consumeEvents
- If true
, the accelerator handles and
consumes the event, except when the consume flag of the event
is already set to true
. If false
, the
accelerator ignores the event's consume flag, and handles all
events without changing the consume flag.IlvAccelerator(KeyStroke, boolean)
public IlvAccelerator(int type, int keyCode, int modifiers, boolean consumeEvents)
If you are creating accelerators that have the same key code
and have intersecting sets of modifiers, then use IlvAccelerator(KeyStroke, boolean)
instead. An example is if you want to define an accelerator that responds to a
key code while the CTRL modifier is pressed and a different accelerator that
will respond to the same key code while both the CTRL and ALT modifiers are
pressed. Accelerators created from key strokes match more accurately against
modifier combinations.
type
- The type of the AWT event.keyCode
- The code associated with the key.modifiers
- The modifiers of the key.consumeEvents
- If true
, the accelerator handles and
consumes the event, except when the consume flag of the event
is already set to true
. If false
, the
accelerator ignores the event's consume flag, and handles all
events without changing the consume flag.IlvAccelerator(KeyStroke, boolean)
public IlvAccelerator(int type, int keyCode, int modifiers)
If you are creating accelerators that have the same key code
and have intersecting sets of modifiers, then use IlvAccelerator(KeyStroke, boolean)
instead. An example is if you want to define an accelerator that responds to a
key code while the CTRL modifier is pressed and a different accelerator that
will respond to the same key code while both the CTRL and ALT modifiers are
pressed. Accelerators created from key strokes match more accurately against
modifier combinations.
type
- The type of the AWT event.keyCode
- The code associated with the key.modifiers
- The modifiers of the key.IlvAccelerator(KeyStroke, boolean)
public int getEventType()
public int getKeyCode()
KeyEvent.VK_UNDEFINED
if the key code does
not matter.public int getKeyChar()
KeyEvent.CHAR_UNDEFINED
if the key character does
not matter.public int getModifier()
public void setConsumeEvents(boolean enable)
enable
- If true
, the accelerator handles and consumes
the event, except when the consume flag of the event is already set to
true
. If false
, the accelerator ignores the
event's consume flag, and handles all events without changing the
consume flag.public boolean isConsumeEvents()
public static char CtrlChar(char c)
IlvAccelerator.CtrlChar('a')
will return the
key code for Ctrl+a.protected abstract boolean handleEvent(IlvManagerView view)
view
- the view of the manager.© Copyright Rogue Wave Software, Inc. 1997, 2018. All Rights Reserved.