Java Code Examples for javax.swing.KeyStroke#getKeyChar()

The following examples show how to use javax.swing.KeyStroke#getKeyChar() . 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: AbstractDockingTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Simulates a user initiated keystroke using the keybinding of the given action
 * 
 * @param destination the component for the action being executed
 * @param action The action to simulate pressing
 */
public static void triggerActionKey(Component destination, DockingActionIf action) {

	Objects.requireNonNull(destination);

	KeyStroke keyStroke = action.getKeyBinding();
	if (keyStroke == null) {
		throw new IllegalArgumentException("No KeyStroke assigned for the given action");
	}

	int modifiers = keyStroke.getModifiers();
	int keyCode = keyStroke.getKeyCode();
	char keyChar = keyStroke.getKeyChar();
	boolean isDefined = Character.isDefined(keyChar);
	if (!isDefined) {
		keyChar = KeyEvent.VK_UNDEFINED;
	}

	triggerKey(destination, modifiers, keyCode, keyChar);
}
 
Example 2
Source File: KeyEntryTextField.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void processEntry(KeyStroke ks) {
	ksName = null;

	currentKeyStroke = ks;

	// Clear entry if enter or backspace
	if (ks != null) {
		char keyChar = ks.getKeyChar();
		if (!Character.isWhitespace(keyChar) &&
			Character.getType(keyChar) != Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE) {
			ksName = KeyBindingUtils.parseKeyStroke(ks);
		}
	}
	listener.processEntry(ks);
}
 
Example 3
Source File: MindMapDocumentEditor.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void onNonConsumedKeyEvent(@Nonnull final MindMapPanel source, @Nonnull final KeyEvent e, @Nonnull final KeyEventType type) {
  if (type == KeyEventType.PRESSED) {
    if (e.getModifiers() == 0) {
      switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_RIGHT:
        case KeyEvent.VK_DOWN: {
          e.consume();
        }
        break;
      }
    }
  }

  boolean activated = false;
  final ShortcutSet findAtMindMap = getFindAtMindMapShortcutSet();
  if (findAtMindMap != null) {
    final KeyStroke eventStroke = KeyStroke.getKeyStrokeForEvent(e);
    for (final Shortcut c : findAtMindMap.getShortcuts()) {
      if (c instanceof KeyboardShortcut) {
        final KeyboardShortcut keyboardShortcut = (KeyboardShortcut) c;
        final KeyStroke firstKeyStroke = keyboardShortcut.getFirstKeyStroke();
        final KeyStroke secondKeyStroke = keyboardShortcut.getSecondKeyStroke();

        if (firstKeyStroke != null && firstKeyStroke.getModifiers() == eventStroke.getModifiers() && firstKeyStroke.getKeyCode() == eventStroke.getKeyCode() && firstKeyStroke.getKeyChar() == eventStroke.getKeyChar()) {
          activated = true;
          break;
        }
        if (secondKeyStroke != null && secondKeyStroke.getModifiers() == eventStroke.getModifiers() && secondKeyStroke.getKeyCode() == eventStroke.getKeyCode() && secondKeyStroke.getKeyChar() == eventStroke.getKeyChar()) {
          activated = true;
          break;
        }
      }
    }
  }

  if (activated) {
    e.consume();
    activateTextSearchPanel();
  }

  if (!e.isConsumed() && e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
    ApplicationManager.getApplication().invokeLater(new Runnable() {
      @Override
      public void run() {
        findTextPanel.deactivate();
      }
    });
  }
}
 
Example 4
Source File: SeaGlassMenuItemUI.java    From seaglass with Apache License 2.0 4 votes vote down vote up
static Dimension getPreferredMenuItemSize(SeaGlassContext context, SeaGlassContext accContext, boolean useCheckAndArrow, JComponent c,
    Icon checkIcon, Icon arrowIcon, int defaultTextIconGap, String acceleratorDelimiter) {
    JMenuItem b = (JMenuItem) c;
    Icon icon = (Icon) b.getIcon();
    String text = b.getText();
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";

    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += acceleratorDelimiter;
        }
        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            acceleratorText += KeyEvent.getKeyText(keyCode);
        } else {
            acceleratorText += accelerator.getKeyChar();
        }
    }

    Font font = context.getStyle().getFont(context);
    FontMetrics fm = b.getFontMetrics(font);
    FontMetrics fmAccel = b.getFontMetrics(accContext.getStyle().getFont(accContext));

    resetRects();

    layoutMenuItem(context, fm, accContext, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon, b.getVerticalAlignment(), b
        .getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect,
        acceleratorRect, checkIconRect, arrowIconRect, text == null ? 0 : defaultTextIconGap, defaultTextIconGap, useCheckAndArrow);
    // find the union of the icon and text rects
    r.setBounds(textRect);
    r = SwingUtilities.computeUnion(iconRect.x, iconRect.y, iconRect.width, iconRect.height, r);
    // To make the accelerator texts appear in a column,
    // find the widest MenuItem text and the widest accelerator text.

    // Get the parent, which stores the information.
    Container parent = b.getParent();

    if (parent instanceof JPopupMenu) {
        SeaGlassPopupMenuUI popupUI = (SeaGlassPopupMenuUI) SeaGlassLookAndFeel.getUIOfType(((JPopupMenu) parent).getUI(),
            SeaGlassPopupMenuUI.class);

        if (popupUI != null) {
            r.width = popupUI.adjustTextWidth(r.width);

            popupUI.adjustAcceleratorWidth(acceleratorRect.width);

            r.width += popupUI.getMaxAcceleratorWidth();
        }
    } else if (parent != null && !(b instanceof JMenu && ((JMenu) b).isTopLevelMenu())) {
        r.width += acceleratorRect.width;
    }

    if (useCheckAndArrow) {
        // Add in the checkIcon
        r.width += checkIconRect.width;
        r.width += defaultTextIconGap;

        // Add in the arrowIcon
        r.width += defaultTextIconGap;
        r.width += arrowIconRect.width;
    }

    r.width += 2 * defaultTextIconGap;

    Insets insets = b.getInsets();
    if (insets != null) {
        r.width += insets.left + insets.right;
        r.height += insets.top + insets.bottom;
    }

    // if the width is even, bump it up one. This is critical
    // for the focus dash line to draw properly
    if (r.width % 2 == 0) {
        r.width++;
    }

    // if the height is even, bump it up one. This is critical
    // for the text to center properly
    if (r.height % 2 == 0) {
        r.height++;
    }
    return r.getSize();
}
 
Example 5
Source File: AbstractDockingTest.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Fires a {@link KeyListener#keyPressed(KeyEvent)}, 
 * {@link KeyListener#keyTyped(KeyEvent)}
 * and {@link KeyListener#keyReleased(KeyEvent)} for the given key stroke
 * 
 * @param c the destination component
 * @param ks the key stroke
 */
public static void triggerKey(Component c, KeyStroke ks) {
	int modifiers = ks.getModifiers();
	char keyChar = ks.getKeyChar();
	int keyCode = ks.getKeyCode();
	triggerKey(c, modifiers, keyCode, keyChar);
}