下面列出了java.awt.event.KeyEvent#KEY_PRESSED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
boolean handleShortcut(KeyEvent e) {
// Is it a key event?
int id = e.getID();
if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) {
return false;
}
// Is the accelerator modifier key pressed?
int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
if ((e.getModifiers() & accelKey) == 0) {
return false;
}
// Pass MenuShortcut on to child menus.
int nmenus = getMenuCount();
for (int i = 0 ; i < nmenus ; i++) {
Menu m = getMenu(i);
if (m.handleShortcut(e)) {
return true;
}
}
return false;
}
boolean handleShortcut(KeyEvent e) {
// Is it a key event?
int id = e.getID();
if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) {
return false;
}
// Is the accelerator modifier key pressed?
int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
if ((e.getModifiers() & accelKey) == 0) {
return false;
}
// Pass MenuShortcut on to child menus.
int nmenus = getMenuCount();
for (int i = 0 ; i < nmenus ; i++) {
Menu m = getMenu(i);
if (m.handleShortcut(e)) {
return true;
}
}
return false;
}
/**
* Handles the keys which have the same functionality for all the types
* of labeling.
*/
private void dispatchKeyGeneral(KeyEvent e) {
int eventId = e.getID();
int key = e.getKeyCode();
// the mapping of the keys is based on the default assignment of keys for UGEE graphic tablet
if (eventId == KeyEvent.KEY_PRESSED) {
if (e.isControlDown()) {
handleCtrlKey(key);
} else if (e.isAltDown()) {
handleAltKey(key);
} else {
handleNormalKey(key);
}
}
}
@Override
protected void processKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
int keyCode = e.getKeyCode();
if (
keyCode == KeyEvent.VK_SHIFT ||
keyCode == KeyEvent.VK_ALT ||
keyCode == KeyEvent.VK_CONTROL ||
keyCode == KeyEvent.VK_ALT_GRAPH ||
keyCode == KeyEvent.VK_META
){
return;
}
setKeyStroke(KeyStrokeAdapter.getDefaultKeyStroke(e));
}
}
boolean handleShortcut(KeyEvent e) {
// Is it a key event?
int id = e.getID();
if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) {
return false;
}
// Is the accelerator modifier key pressed?
int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
if ((e.getModifiers() & accelKey) == 0) {
return false;
}
// Pass MenuShortcut on to child menus.
int nmenus = getMenuCount();
for (int i = 0 ; i < nmenus ; i++) {
Menu m = getMenu(i);
if (m.handleShortcut(e)) {
return true;
}
}
return false;
}
private void handleReconnectAccountCommand() {
JFrame jf = MainWindowManager.mainWindowManager().getMainWindow();
int modifiers = KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK;
KeyEvent pressed=new KeyEvent(jf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), modifiers, KeyEvent.VK_R, KeyEvent.CHAR_UNDEFINED);
KeyEvent typed=new KeyEvent(jf, KeyEvent.KEY_TYPED, System.currentTimeMillis(), modifiers, KeyEvent.VK_UNDEFINED, 'R' );
KeyEvent released=new KeyEvent(jf, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), modifiers, KeyEvent.VK_R, KeyEvent.CHAR_UNDEFINED );
jf.dispatchEvent(pressed);
jf.dispatchEvent(typed);
jf.dispatchEvent(released);
mChannel.writeAck("");
}
void handleJavaKeyEvent(KeyEvent e) {
int id = e.getID();
switch (id) {
case KeyEvent.KEY_PRESSED:
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
pressed=true;
armed=true;
repaint();
action(e.getWhen(),e.getModifiers());
}
break;
case KeyEvent.KEY_RELEASED:
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
pressed = false;
armed = false;
repaint();
}
break;
}
}
/**
* Utility method, calls one of <code>keyPressed()</code>,
* <code>keyReleased()</code>, or <code>keyTyped()</code>.
*/
public void processKeyEvent(KeyEvent evt) {
switch(evt.getID())
{
case KeyEvent.KEY_TYPED:
keyTyped(evt);
break;
case KeyEvent.KEY_PRESSED:
keyPressed(evt);
break;
case KeyEvent.KEY_RELEASED:
keyReleased(evt);
break;
}
}
public void processEvent(AWTEvent event) {
setIndexOfType(super.getIndexOfType());
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
switch (me.getID()) {
case MouseEvent.MOUSE_ENTERED:
mouseEntered(me);
break;
case MouseEvent.MOUSE_PRESSED:
mousePressed(me);
break;
case MouseEvent.MOUSE_RELEASED:
mouseReleased(me);
break;
case MouseEvent.MOUSE_CLICKED:
mouseClicked(me);
break;
case MouseEvent.MOUSE_EXITED:
mouseExited(me);
break;
}
} else if (event instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) event;
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
keyPressed(ke);
break;
case KeyEvent.KEY_RELEASED:
keyReleased(ke);
break;
case KeyEvent.KEY_TYPED:
keyTyped(ke);
break;
}
}
}
void handleJavaKeyEvent(KeyEvent e) {
int id = e.getID();
switch (id) {
case KeyEvent.KEY_PRESSED:
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
pressed=true;
armed=true;
repaint();
action(e.getWhen(),e.getModifiers());
}
break;
case KeyEvent.KEY_RELEASED:
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
pressed = false;
armed = false;
repaint();
}
break;
}
}
void handleJavaKeyEvent(KeyEvent e) {
int id = e.getID();
switch (id) {
case KeyEvent.KEY_PRESSED:
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
pressed=true;
armed=true;
repaint();
action(e.getWhen(),e.getModifiers());
}
break;
case KeyEvent.KEY_RELEASED:
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
pressed = false;
armed = false;
repaint();
}
break;
}
}
public boolean ioEventIsOfDesiredType(IOEvent io) {
if(selection == null) return true;
if(io instanceof MouseStateInputEvent) {
MouseStateInputEvent mouseEvent = (MouseStateInputEvent)io;
int id = mouseEvent.getType();
switch(id) {
case MouseEvent.MOUSE_PRESSED :
return selection == press;
case MouseEvent.MOUSE_DRAGGED :
return selection == drag;
case MouseEvent.MOUSE_CLICKED :
case MouseEvent.MOUSE_RELEASED :
return selection == release;
case MouseEvent.MOUSE_WHEEL :
return selection == scroll;
case MouseEvent.MOUSE_MOVED :
return selection == move;
default :
return false;
}
}
else if(io instanceof KeyStateInputEvent) {
KeyStateInputEvent keyEvent = (KeyStateInputEvent)io;
int type = keyEvent.getType();
switch(type) {
case KeyEvent.KEY_PRESSED :
return selection == keydown;
case KeyEvent.KEY_RELEASED :
return selection == keyup;
case KeyEvent.KEY_TYPED :
return selection == keyup;
default:
return false;
}
}
else if(selection == repaint) return io instanceof GetGraphicsOutputEvent;
else if(selection == print) return io instanceof TextualOutputEvent;
else return false;
}
@Override
public void eventDispatched( AWTEvent e ) {
if( !(e instanceof KeyEvent) )
return;
KeyEvent ke = ( KeyEvent ) e;
ke.consume();
if( e.getID() == KeyEvent.KEY_PRESSED ) {
switch( ke.getKeyCode() ) {
case KeyEvent.VK_LEFT:
do {
currentSide = currentSide.moveLeft();
} while( !checkDropLocation() );
refresh();
break;
case KeyEvent.VK_RIGHT:
do {
currentSide = currentSide.moveRight();
} while( !checkDropLocation() );
refresh();
break;
case KeyEvent.VK_UP:
do {
currentSide = currentSide.moveUp();
} while( !checkDropLocation() );
refresh();
break;
case KeyEvent.VK_DOWN:
do {
currentSide = currentSide.moveDown();
} while( !checkDropLocation() );
refresh();
break;
case KeyEvent.VK_ENTER:
stop( true );
break;
case KeyEvent.VK_ESCAPE:
abort();
break;
}
}
}
public boolean postProcessKeyEvent(KeyEvent ev) {
if(ev.isConsumed() && ev.getKeyCode() != KeyEvent.VK_ALT) {
// mnemonic combination, it's consumed, but we need
// set altKeyPressed to false, otherwise after selection
// component by mnemonic combination a menu will be open
altKeyPressed = false;
return false;
}
if (ev.getKeyCode() == KeyEvent.VK_ALT) {
root = SwingUtilities.getRootPane(ev.getComponent());
winAncestor = (root == null ? null :
SwingUtilities.getWindowAncestor(root));
if (ev.getID() == KeyEvent.KEY_PRESSED) {
if (!altKeyPressed) {
altPressed(ev);
}
altKeyPressed = true;
return true;
} else if (ev.getID() == KeyEvent.KEY_RELEASED) {
if (altKeyPressed) {
altReleased(ev);
} else {
MenuSelectionManager msm =
MenuSelectionManager.defaultManager();
MenuElement[] path = msm.getSelectedPath();
if (path.length <= 0) {
WindowsLookAndFeel.setMnemonicHidden(true);
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
}
}
altKeyPressed = false;
}
root = null;
winAncestor = null;
} else {
if (WindowsLookAndFeel.isMnemonicHidden() && ev.isAltDown()) {
WindowsLookAndFeel.setMnemonicHidden(false);
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
}
altKeyPressed = false;
}
return false;
}
public void testCancel6() throws Exception {
KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_DELETE, '\0');
performTest("package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }", 84 - 22 - 1, ke, "package test; public class Test { public void test() {int xxx= 0; int y = xxx; } }", false);
}
/** Overridden to allow standard keybinding processing of VK_TAB and
* abort any pending drag operation on the vertical grid. */
public void processKeyEvent(KeyEvent e) {
if (dragListener.isArmed()) {
dragListener.setArmed(false);
}
boolean suppressDefaultHandling = ((searchField != null) && searchField.isShowing()) &&
((e.getKeyCode() == KeyEvent.VK_UP) || (e.getKeyCode() == KeyEvent.VK_DOWN));
//Manually hook in the bindings for tab - does not seem to get called
//automatically
if (e.getKeyCode() != KeyEvent.VK_TAB) {
if (!suppressDefaultHandling) {
//Either the search field or the table should handle up/down, not both
super.processKeyEvent(e);
}
if (!e.isConsumed()) {
if ((e.getID() == KeyEvent.KEY_PRESSED) && !isEditing()) {
int modifiers = e.getModifiers();
int keyCode = e.getKeyCode();
if (((modifiers > 0) && (modifiers != KeyEvent.SHIFT_MASK)) || e.isActionKey()) {
return;
}
char c = e.getKeyChar();
if (!Character.isISOControl(c) && (keyCode != KeyEvent.VK_SHIFT) &&
(keyCode != KeyEvent.VK_ESCAPE)) {
searchArmed = true;
e.consume();
}
} else if (searchArmed && (e.getID() == KeyEvent.KEY_TYPED)) {
passToSearchField(e);
e.consume();
searchArmed = false;
} else {
searchArmed = false;
}
}
} else {
processKeyBinding(
KeyStroke.getKeyStroke(e.VK_TAB, e.getModifiersEx(), e.getID() == e.KEY_RELEASED), e,
JComponent.WHEN_FOCUSED, e.getID() == e.KEY_PRESSED
);
}
}
private void consumeTraversalKey(KeyEvent e) {
e.consume();
consumeNextKeyTyped = (e.getID() == KeyEvent.KEY_PRESSED) &&
!e.isActionKey();
}
private void consumeTraversalKey(KeyEvent e) {
e.consume();
consumeNextKeyTyped = (e.getID() == KeyEvent.KEY_PRESSED) &&
!e.isActionKey();
}
/**
* This method initiates a focus traversal operation if and only if the
* KeyEvent represents a focus traversal key for the specified
* focusedComponent. It is expected that focusedComponent is the current
* focus owner, although this need not be the case. If it is not,
* focus traversal will nevertheless proceed as if focusedComponent
* were the focus owner.
*
* @param focusedComponent the Component that is the basis for a focus
* traversal operation if the specified event represents a focus
* traversal key for the Component
* @param e the event that may represent a focus traversal key
*/
public void processKeyEvent(Component focusedComponent, KeyEvent e) {
// consume processed event if needed
if (consumeProcessedKeyEvent(e)) {
return;
}
// KEY_TYPED events cannot be focus traversal keys
if (e.getID() == KeyEvent.KEY_TYPED) {
return;
}
if (focusedComponent.getFocusTraversalKeysEnabled() &&
!e.isConsumed())
{
AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e),
oppStroke = AWTKeyStroke.getAWTKeyStroke(stroke.getKeyCode(),
stroke.getModifiers(),
!stroke.isOnKeyRelease());
Set<AWTKeyStroke> toTest;
boolean contains, containsOpp;
toTest = focusedComponent.getFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
contains = toTest.contains(stroke);
containsOpp = toTest.contains(oppStroke);
if (contains || containsOpp) {
consumeTraversalKey(e);
if (contains) {
focusNextComponent(focusedComponent);
}
return;
} else if (e.getID() == KeyEvent.KEY_PRESSED) {
// Fix for 6637607: consumeNextKeyTyped should be reset.
consumeNextKeyTyped = false;
}
toTest = focusedComponent.getFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
contains = toTest.contains(stroke);
containsOpp = toTest.contains(oppStroke);
if (contains || containsOpp) {
consumeTraversalKey(e);
if (contains) {
focusPreviousComponent(focusedComponent);
}
return;
}
toTest = focusedComponent.getFocusTraversalKeys(
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
contains = toTest.contains(stroke);
containsOpp = toTest.contains(oppStroke);
if (contains || containsOpp) {
consumeTraversalKey(e);
if (contains) {
upFocusCycle(focusedComponent);
}
return;
}
if (!((focusedComponent instanceof Container) &&
((Container)focusedComponent).isFocusCycleRoot())) {
return;
}
toTest = focusedComponent.getFocusTraversalKeys(
KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
contains = toTest.contains(stroke);
containsOpp = toTest.contains(oppStroke);
if (contains || containsOpp) {
consumeTraversalKey(e);
if (contains) {
downFocusCycle((Container)focusedComponent);
}
}
}
}
/**
* @see java.awt.im.InputContext#dispatchEvent
*/
@SuppressWarnings("fallthrough")
public void dispatchEvent(AWTEvent event) {
if (event instanceof InputMethodEvent) {
return;
}
// Ignore focus events that relate to the InputMethodWindow of this context.
// This is a workaround. Should be removed after 4452384 is fixed.
if (event instanceof FocusEvent) {
Component opposite = ((FocusEvent)event).getOppositeComponent();
if ((opposite != null) &&
(getComponentWindow(opposite) instanceof InputMethodWindow) &&
(opposite.getInputContext() == this)) {
return;
}
}
InputMethod inputMethod = getInputMethod();
int id = event.getID();
switch (id) {
case FocusEvent.FOCUS_GAINED:
focusGained((Component) event.getSource());
break;
case FocusEvent.FOCUS_LOST:
focusLost((Component) event.getSource(), ((FocusEvent) event).isTemporary());
break;
case KeyEvent.KEY_PRESSED:
if (checkInputMethodSelectionKey((KeyEvent)event)) {
// pop up the input method selection menu
InputMethodManager.getInstance().notifyChangeRequestByHotKey((Component)event.getSource());
break;
}
// fall through
default:
if ((inputMethod != null) && (event instanceof InputEvent)) {
inputMethod.dispatchEvent(event);
}
}
}