Java Code Examples for java.awt.event.InputEvent#getModifiersExText()

The following examples show how to use java.awt.event.InputEvent#getModifiersExText() . 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: ActionUtils.java    From workcraft with MIT License 6 votes vote down vote up
public static String getKeystrokeString(KeyStroke keystroke) {
    String result = "";
    if (keystroke != null) {
        result = InputEvent.getModifiersExText(keystroke.getModifiers());

        switch (keystroke.getKeyEventType()) {
        case KeyEvent.KEY_TYPED:
            if (!result.isEmpty()) {
                result += "-";
            }
            result += keystroke.getKeyChar();
            break;
        case KeyEvent.KEY_PRESSED:
        case KeyEvent.KEY_RELEASED:
            if (!result.isEmpty()) {
                result += "-";
            }
            int keyCode = keystroke.getKeyCode();
            result += getKeyString(keyCode);
            break;
        default:
        }
    }
    return result;
}
 
Example 2
Source File: Utils.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
public static String keystrokeAsText(KeyStroke keyStroke) {
    String s = "";
    int modifiers = keyStroke.getModifiers();
    if (modifiers > 0) {
        s = InputEvent.getModifiersExText(modifiers);
        s += " ";
    }
    int keyCode = keyStroke.getKeyCode();
    if (keyCode != 0) {
        s += KeyEvent.getKeyText(keyCode);
    } else {
        s += keyStroke.getKeyChar();
    }

    return s;
}
 
Example 3
Source File: FreeColActionUI.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
/**
* Gets a string to represent the given {@code KeyStroke} to the user.
*
* @param keyStroke {@code java.awt.event.KeyStroke}
* @return String
*/
public static String getHumanKeyStrokeText(KeyStroke keyStroke) {
    if (keyStroke == null) {
        return " ";
    }

    String s = InputEvent.getModifiersExText(keyStroke.getModifiers());
    if (!s.isEmpty()) s += "+";
    return s + KeyEvent.getKeyText(keyStroke.getKeyCode());
}
 
Example 4
Source File: MenuShortcut.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an internationalized description of the MenuShortcut.
 * @return a string representation of this MenuShortcut.
 * @since 1.1
 */
public String toString() {
    int modifiers = 0;
    if (!GraphicsEnvironment.isHeadless()) {
        modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
    }
    if (usesShiftModifier()) {
        modifiers |= InputEvent.SHIFT_DOWN_MASK;
    }
    return InputEvent.getModifiersExText(modifiers) + "+" +
           KeyEvent.getKeyText(key);
}
 
Example 5
Source File: ExtendedModifiersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        return;
    }
    modifiersEx = e.getModifiersEx();
    exText = InputEvent.getModifiersExText(modifiersEx);
    keyPressedFlag = true;

    synchronized (lock) {
        lock.notifyAll();
    }
}
 
Example 6
Source File: KeyStrokeDisplay.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param ks The {@link KeyStroke} to use.
 * @return The text that represents the {@link KeyStroke}.
 */
public static String getKeyStrokeDisplay(KeyStroke ks) {
    StringBuilder buffer = new StringBuilder();
    if (ks != null) {
        int modifiers = ks.getModifiers();
        if (modifiers > 0) {
            String modifierText = InputEvent.getModifiersExText(modifiers);
            if (Platform.isMacintosh()) {
                buffer.append(modifierText.replaceAll("\\+", ""));
            } else {
                buffer.append(modifierText);
                String delimiter = UIManager.getString("MenuItem.acceleratorDelimiter");
                if (delimiter == null) {
                    delimiter = "+";
                }
                buffer.append(delimiter);
            }
        }
        int keyCode = ks.getKeyCode();
        if (keyCode == 0) {
            buffer.append(ks.getKeyChar());
        } else {
            buffer.append(getKeyText(keyCode));
        }
    }
    return buffer.toString();
}
 
Example 7
Source File: Debug.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
public static String keyEvent(KeyEvent e) {
    String keyText = KeyEvent.getKeyText(e.getKeyCode());
    int modifiers = e.getModifiersEx();
    if (modifiers != 0) {
        String modifiersText = InputEvent.getModifiersExText(modifiers);
        if (keyText.equals(modifiersText)) { // the key itself is the modifier
            return modifiersText;
        }
        return modifiersText + "+" + keyText;
    }
    return keyText;
}
 
Example 8
Source File: MenuUtilities.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Returns the layout info for the specified menu item.
 * 
 * @param menuItem
 *            Menu item.
 * @param acceleratorFont
 *            Font for the accelerator text.
 * @param checkIcon
 *            Check icon.
 * @param arrowIcon
 *            Arrow icon.
 * @param defaultTextIconGap
 *            Gap between the icon and the text.
 * @return Layout info for the specified menu item.
 */
public static MenuLayoutInfo getMenuLayoutInfo(boolean forPainting, JMenuItem menuItem,
        Font acceleratorFont, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) {

    Insets i = menuItem.getInsets();

    Rectangle iconRect = new Rectangle(0, 0, 0, 0);
    Rectangle textRect = new Rectangle(0, 0, 0, 0);
    Rectangle acceleratorRect = new Rectangle(0, 0, 0, 0);
    Rectangle checkIconRect = new Rectangle(0, 0, 0, 0);
    Rectangle arrowIconRect = new Rectangle(0, 0, 0, 0);
    Rectangle viewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    if (forPainting) {
        // fix for issue 379 - setting the available
        // bounds only during the painting.
        int menuWidth = menuItem.getWidth();
        int menuHeight = menuItem.getHeight();
        if ((menuWidth > 0) && (menuHeight > 0))
            viewRect.setBounds(0, 0, menuWidth, menuHeight);

        viewRect.x += i.left;
        viewRect.y += i.top;
        viewRect.width -= (i.right + viewRect.x);
        viewRect.height -= (i.bottom + viewRect.y);
    }

    FontMetrics fm = SubstanceMetricsUtilities.getFontMetrics(menuItem.getFont());
    FontMetrics fmAccel = SubstanceMetricsUtilities.getFontMetrics(acceleratorFont);

    // get Accelerator text
    KeyStroke accelerator = menuItem.getAccelerator();
    String acceleratorText = "";
    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            acceleratorText = InputEvent.getModifiersExText(modifiers);
            acceleratorText += UIManager.getString("MenuItem.acceleratorDelimiter");
        }

        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            acceleratorText += KeyEvent.getKeyText(keyCode);
        } else {
            acceleratorText += accelerator.getKeyChar();
        }
    }

    // layout the text and icon
    String text = layoutMenuItem(menuItem, fm, menuItem.getText(), fmAccel, acceleratorText,
            menuItem.getIcon(), checkIcon, arrowIcon, menuItem.getVerticalAlignment(),
            menuItem.getHorizontalAlignment(), menuItem.getVerticalTextPosition(),
            menuItem.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect,
            checkIconRect, arrowIconRect, menuItem.getText() == null ? 0 : defaultTextIconGap,
            defaultTextIconGap);

    MenuLayoutInfo mlInfo = new MenuLayoutInfo();
    mlInfo.checkIconRect = checkIconRect;
    mlInfo.iconRect = iconRect;
    mlInfo.textRect = textRect;
    mlInfo.viewRect = viewRect;
    mlInfo.acceleratorRect = acceleratorRect;
    mlInfo.arrowIconRect = arrowIconRect;
    mlInfo.text = text;

    return mlInfo;
}
 
Example 9
Source File: ShortcutsController.java    From latexdraw with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void initialize(final URL location, final ResourceBundle resources) {
	final String ctrl = InputEvent.getModifiersExText(InputEvent.CTRL_DOWN_MASK);
	final String shift = InputEvent.getModifiersExText(InputEvent.SHIFT_DOWN_MASK);
	final String leftClick = lang.getString("leftClick");
	final String catEdit = lang.getString("edit");
	final String catNav = lang.getString("navigation");
	final String catTran = lang.getString("transformation");
	final String catDraw = lang.getString("drawing");
	final String catFile = lang.getString("file");

	for(int i = 0, size = table.getColumns().size(); i < size; i++) {
		final int colIndex = i;
		final TableColumn<ObservableList<String>, String> col = (TableColumn<ObservableList<String>, String>) table.getColumns().get(i);
		col.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().get(colIndex)));
	}

	table.getColumns().forEach(col -> col.prefWidthProperty().bind(table.widthProperty().divide(3)));

	table.getItems().addAll(FXCollections.observableArrayList(ctrl + "+C", lang.getString("copy"), catEdit), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+V", lang.getString("paste"), catEdit), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+X", lang.getString("cut"), catEdit), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+Z", lang.getString("close"), catEdit),  //NON-NLS
		FXCollections.observableArrayList(ctrl + "+Y", lang.getString("redo"), catEdit),  //NON-NLS
		FXCollections.observableArrayList(ctrl + "+N", lang.getString("newDrawing"), catFile), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+O", lang.getString("openDrawing"), catFile), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+S", lang.getString("saveDrawing"), catFile), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+W", lang.getString("quit"), catFile),  //NON-NLS
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_ADD), lang.getString("zoomIn"), catNav),
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_SUBTRACT), lang.getString("zoomOut"), catNav),
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_DELETE), lang.getString("drawAxes"), catDraw),
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_RIGHT), lang.getString("moveScrollbarRight"), catNav),
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_LEFT), lang.getString("moveScrollbarLeft"), catNav),
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_UP), lang.getString("moveScrollbarTop"), catNav),
		FXCollections.observableArrayList(KeyEvent.getKeyText(KeyEvent.VK_DOWN), lang.getString("moveScrollbarBottom"), catNav),
		FXCollections.observableArrayList(ctrl + "+U", lang.getString("updateShapes"), catTran), //NON-NLS
		FXCollections.observableArrayList(ctrl + "+A", lang.getString("selectAll"), catDraw), //NON-NLS
		FXCollections.observableArrayList(ctrl + '+' + leftClick, lang.getString("addClickedShape"), catDraw),
		FXCollections.observableArrayList(shift + '+' + leftClick, lang.getString("removeClickedShape"), catDraw),
		FXCollections.observableArrayList(ctrl + '+' + lang.getString("mouseWheel"),
			lang.getString("zoom"), catDraw)
	);
}