Java Code Examples for java.awt.event.KeyEvent#isConsumed()

The following examples show how to use java.awt.event.KeyEvent#isConsumed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PdfPanel.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void keyTyped(KeyEvent event) {
    if (!event.isConsumed() && (event.getModifiersEx() & getToolkit().getMenuShortcutKeyMaskEx()) == 0) {
        char ch = event.getKeyChar();
        switch (ch) {
        case '-':
            zoomOut();
            break;
        case '=':
            zoomIn();
            break;
        case '1':
            actualSize();
            break;
        default:
            return;
        }
        event.consume();
    }
}
 
Example 2
Source File: KeyManager.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void processKeyTyped(KeyEvent keyEvent)
{
	if (keyEvent.isConsumed())
	{
		return;
	}

	for (KeyListener keyListener : keyListeners)
	{
		if (!shouldProcess(keyListener))
		{
			continue;
		}

		keyListener.keyTyped(keyEvent);
		if (keyEvent.isConsumed())
		{
			break;
		}
	}
}
 
Example 3
Source File: KeyStrokeAdapter.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @param event    the specified key event to process
 * @param extended {@code true} if extended key code should be used
 * @return a key stroke or {@code null} if it is not applicable
 * @see JComponent#processKeyBindings(KeyEvent, boolean)
 */
public static KeyStroke getKeyStroke(KeyEvent event, boolean extended) {
  if (event != null && !event.isConsumed()) {
    int id = event.getID();
    if (id == KeyEvent.KEY_TYPED) {
      return extended ? null : getKeyStroke(event.getKeyChar(), 0);
    }
    boolean released = id == KeyEvent.KEY_RELEASED;
    if (released || id == KeyEvent.KEY_PRESSED) {
      int code = event.getKeyCode();
      if (extended) {
        if (Registry.is("actionSystem.extendedKeyCode.disabled")) {
          return null;
        }
        code = getExtendedKeyCode(event);
        if (code == event.getKeyCode()) {
          return null;
        }
      }
      return getKeyStroke(code, event.getModifiers(), released);
    }
  }
  return null;
}
 
Example 4
Source File: Outline.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void keyTyped(KeyEvent event) {
    if (!event.isConsumed() && (event.getModifiersEx() & getToolkit().getMenuShortcutKeyMaskEx()) == 0) {
        char ch = event.getKeyChar();
        if (ch == '\n' || ch == '\r') {
            if (mModel.hasSelection()) {
                notifyActionListeners();
            }
            event.consume();
        } else if (ch == '\b' || ch == KeyEvent.VK_DELETE) {
            if (canDeleteSelection()) {
                deleteSelection();
            }
            event.consume();
        }
    }
}
 
Example 5
Source File: Treeline.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void keyPressed(final KeyEvent ke) {
	if (isTagging()) {
		super.keyPressed(ke);
		return;
	}
	final int tool = ProjectToolbar.getToolId();
	try {
		if (ProjectToolbar.PEN == tool) {
			final Object origin = ke.getSource();
			if (! (origin instanceof DisplayCanvas)) {
				ke.consume();
				return;
			}
			final DisplayCanvas dc = (DisplayCanvas)origin;
			final Layer layer = dc.getDisplay().getLayer();
			final Point p = dc.getCursorLoc(); // as offscreen coords

			switch (ke.getKeyCode()) {
				case KeyEvent.VK_O:
					if (askAdjustRadius(p.x, p.y, layer, dc.getMagnification())) {
						ke.consume();
					}
					break;
			}
		}
	} finally {
		if (!ke.isConsumed()) {
			super.keyPressed(ke);
		}
	}
}
 
Example 6
Source File: ComponentWithBrowseButton.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected final boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
  if (condition == WHEN_FOCUSED && myCurrentEvent != e) {
    try {
      myCurrentEvent = e;
      myComponent.dispatchEvent(e);
    }
    finally {
      myCurrentEvent = null;
    }
  }
  if (e.isConsumed()) return true;
  return super.processKeyBinding(ks, e, condition, pressed);
}
 
Example 7
Source File: QueueJumpingKeyListener.java    From SNT with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void keyReleased(final KeyEvent e) {
	for (final KeyListener kl : listeners) {
		if (e.isConsumed())
			break;
		kl.keyReleased(e);
	}
}
 
Example 8
Source File: MindMapPanel.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
protected void fireNotificationNonConsumedKeyEvent(@Nonnull final KeyEvent keyEvent, @Nonnull final KeyEventType type) {
  for (final MindMapListener l : MindMapPanel.this.mindMapListeners) {
    if (keyEvent.isConsumed()) {
      break;
    }
    l.onNonConsumedKeyEvent(MindMapPanel.this, keyEvent, type);
  }
}
 
Example 9
Source File: DefaultKeyboardFocusManager.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method will be called by <code>dispatchKeyEvent</code>. It will
 * handle any unconsumed KeyEvents that map to an AWT
 * <code>MenuShortcut</code> by consuming the event and activating the
 * shortcut.
 *
 * @param e the KeyEvent to post-process
 * @return <code>true</code>
 * @see #dispatchKeyEvent
 * @see MenuShortcut
 */
public boolean postProcessKeyEvent(KeyEvent e) {
    if (!e.isConsumed()) {
        Component target = e.getComponent();
        Container p = (Container)
            (target instanceof Container ? target : target.getParent());
        if (p != null) {
            p.postProcessKeyEvent(e);
        }
    }
    return true;
}
 
Example 10
Source File: MnemonicsSearch.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void processKeyEvent(@Nonnull KeyEvent e) {
  if (e.isConsumed()) return;
  if (e.getID() != KeyEvent.KEY_TYPED) return;
  if (!StringUtil.isEmptyOrSpaces(myPopup.getSpeedSearch().getFilter())) return;

  if (Character.isLetterOrDigit(e.getKeyChar())) {
    final String s = Character.toString(e.getKeyChar());
    final T toSelect = myChar2ValueMap.get(s);
    if (toSelect != null) {
      select(toSelect);
      e.consume();
    }
  }
}
 
Example 11
Source File: Search.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private boolean redirectKeyEventToFloater(KeyEvent event) {
    if (mFloater != null && !event.isConsumed() && (event.getModifiersEx() & getToolkit().getMenuShortcutKeyMaskEx()) == 0) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) {
            mFloater.handleKeyPressed(event);
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: WindowsRootPaneUI.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed()) {
        // do not manage consumed event
        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 {
        altKeyPressed = false;
    }
    return false;
}
 
Example 13
Source File: CompletionLayout.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void processKeyEvent(KeyEvent evt) {
    for (CompletionLayoutPopup popup : visiblePopups) {
        popup.processKeyEvent(evt);
        if (evt.isConsumed())
            return;
    }
}
 
Example 14
Source File: WindowsRootPaneUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed()) {
        // do not manage consumed event
        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 {
        altKeyPressed = false;
    }
    return false;
}
 
Example 15
Source File: WindowsRootPaneUI.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed()) {
        // do not manage consumed event
        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 {
        altKeyPressed = false;
    }
    return false;
}
 
Example 16
Source File: MenuBar.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected @Override boolean processKeyBinding(KeyStroke ks,
                                KeyEvent e,
                                int condition,
                                boolean pressed) {
    if (Utilities.isMac()) {
        int mods = e.getModifiers();
        boolean isCtrl = (mods & KeyEvent.CTRL_MASK) != 0;
        boolean isAlt = (mods & KeyEvent.ALT_MASK) != 0;
        if (isAlt && (e instanceof MarkedKeyEvent)) {
            mods = mods & ~ KeyEvent.CTRL_MASK;
            mods = mods & ~ KeyEvent.CTRL_DOWN_MASK;
            mods |= KeyEvent.ALT_MASK;
            mods |= KeyEvent.ALT_DOWN_MASK;
            
            KeyEvent newEvent = new MarkedKeyEvent (
                (Component) e.getSource(), e.getID(), 
                e.getWhen(), mods, e.getKeyCode(), e.getKeyChar(), 
                e.getKeyLocation());
            
            KeyStroke newStroke = null;
            if( null != ks ) {
                newStroke = e.getID() == KeyEvent.KEY_TYPED ?
                    KeyStroke.getKeyStroke (ks.getKeyChar(), mods) :
                    KeyStroke.getKeyStroke (ks.getKeyCode(), mods,
                    !ks.isOnKeyRelease());
            }
            
            boolean result = super.processKeyBinding (newStroke, 
                newEvent, condition, pressed);
            
            if (newEvent.isConsumed()) {
                e.consume();
            }
            return result;
        } else if (!isAlt) {
            return super.processKeyBinding (ks, e, condition, pressed);
        } else {
            return false;
        }
    } else {
        return super.processKeyBinding (ks, e, condition, pressed);
    }                     
}
 
Example 17
Source File: ProjectTree.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void keyPressed(final KeyEvent ke) {
	super.keyPressed(ke);
	if (ke.isConsumed()) return;
	if (!ke.getSource().equals(ProjectTree.this) || !project.isInputEnabled()) {
		return;
	}
	// get the first selected node only
	TreePath path = getSelectionPath();
	if (null == path) return;
	DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
	if (null == node) return;
	final ProjectThing pt = (ProjectThing)node.getUserObject();
	if (null == pt) return;
	//
	final int flags = ke.getModifiers();
	switch (ke.getKeyCode()) {
		case KeyEvent.VK_PAGE_UP:
			move(node, -1);
			ke.consume(); // in any case
			break;
		case KeyEvent.VK_PAGE_DOWN:
			move(node, 1);
			ke.consume(); // in any case
			break;
		case KeyEvent.VK_F2:
			rename(pt);
			ke.consume();
			break;
		case KeyEvent.VK_H:
			if (0 == (flags ^ Event.ALT_MASK)) {
				pt.setVisible(true);
				ke.consume();
			} else if (0 == flags) {
				pt.setVisible(false);
				ke.consume();
			}
			break;
		case KeyEvent.VK_A:
			if (0 == flags || (0 == (flags ^ Event.SHIFT_MASK))) {
				selectInDisplay(pt, 0 == (flags ^ Event.SHIFT_MASK));
				ke.consume();
			}
			break;
		case KeyEvent.VK_3:
			if (0 == flags) {
				ini.trakem2.display.Display3D.showAndResetView(pt);
				ke.consume();
				break;
			}
			// else, flow:
		case KeyEvent.VK_1:
		case KeyEvent.VK_2:
		case KeyEvent.VK_4:
		case KeyEvent.VK_5:
		case KeyEvent.VK_6:
		case KeyEvent.VK_7:
		case KeyEvent.VK_8:
		case KeyEvent.VK_9:
			// run a plugin, if any
			if (pt.getObject() instanceof Displayable && null != Utils.launchTPlugIn(ke, "Project Tree", project, (Displayable)pt.getObject())) {
				ke.consume();
			}
			break;
	}
	ke.consume();
}
 
Example 18
Source File: DefaultKeyboardFocusManager.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Called by <code>dispatchEvent</code> if no other
 * KeyEventDispatcher in the dispatcher chain dispatched the KeyEvent, or
 * if no other KeyEventDispatchers are registered. If the event has not
 * been consumed, its target is enabled, and the focus owner is not null,
 * this method dispatches the event to its target. This method will also
 * subsequently dispatch the event to all registered
 * KeyEventPostProcessors. After all this operations are finished,
 * the event is passed to peers for processing.
 * <p>
 * In all cases, this method returns <code>true</code>, since
 * DefaultKeyboardFocusManager is designed so that neither
 * <code>dispatchEvent</code>, nor the AWT event dispatcher, should take
 * further action on the event in any situation.
 *
 * @param e the KeyEvent to be dispatched
 * @return <code>true</code>
 * @see Component#dispatchEvent
 */
public boolean dispatchKeyEvent(KeyEvent e) {
    Component focusOwner = (((AWTEvent)e).isPosted) ? getFocusOwner() : e.getComponent();

    if (focusOwner != null && focusOwner.isShowing() && focusOwner.canBeFocusOwner()) {
        if (!e.isConsumed()) {
            Component comp = e.getComponent();
            if (comp != null && comp.isEnabled()) {
                redispatchEvent(comp, e);
            }
        }
    }
    boolean stopPostProcessing = false;
    java.util.List<KeyEventPostProcessor> processors = getKeyEventPostProcessors();
    if (processors != null) {
        for (java.util.Iterator<KeyEventPostProcessor> iter = processors.iterator();
             !stopPostProcessing && iter.hasNext(); )
        {
            stopPostProcessing = iter.next().
                        postProcessKeyEvent(e);
        }
    }
    if (!stopPostProcessing) {
        postProcessKeyEvent(e);
    }

    // Allow the peer to process KeyEvent
    Component source = e.getComponent();
    ComponentPeer peer = source.getPeer();

    if (peer == null || peer instanceof LightweightPeer) {
        // if focus owner is lightweight then its native container
        // processes event
        Container target = source.getNativeContainer();
        if (target != null) {
            peer = target.getPeer();
        }
    }
    if (peer != null) {
        peer.handleEvent(e);
    }

    return true;
}
 
Example 19
Source File: DefaultKeyboardFocusManager.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 20
Source File: BaseTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** 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
        );
    }
}