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

The following examples show how to use java.awt.event.KeyEvent#getKeyText() . 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: KeybindSet.java    From rscplus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns a human-friendly format of this KeybindSet's keybind (modifier + key), for use in
 * buttons and printing.
 *
 * @return A string representing the keybind.
 */
public String getFormattedKeybindText() {
  String modifierText = modifier.toString() + " + ";
  String keyText = KeyEvent.getKeyText(key);

  if (key == -1) keyText = "NONE";

  if (modifier == KeyModifier.NONE) modifierText = "";

  if ("Open Bracket".equals(keyText)) {
    keyText = "[";
  }
  if ("Close Bracket".equals(keyText)) {
    keyText = "]";
  }
  if ("Unknown keyCode: 0x0".equals(keyText)) {
    keyText = "???";
  }
  return modifierText + keyText;
}
 
Example 2
Source File: MenuItemLayoutHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private String getAccText(String acceleratorDelimiter) {
    String accText = "";
    KeyStroke accelerator = mi.getAccelerator();
    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            accText = KeyEvent.getKeyModifiersText(modifiers);
            accText += acceleratorDelimiter;
        }
        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            accText += KeyEvent.getKeyText(keyCode);
        } else {
            accText += accelerator.getKeyChar();
        }
    }
    return accText;
}
 
Example 3
Source File: MenuItemLayoutHelper.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private String getAccText(String acceleratorDelimiter) {
    String accText = "";
    KeyStroke accelerator = mi.getAccelerator();
    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            accText = KeyEvent.getKeyModifiersText(modifiers);
            accText += acceleratorDelimiter;
        }
        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            accText += KeyEvent.getKeyText(keyCode);
        } else {
            accText += accelerator.getKeyChar();
        }
    }
    return accText;
}
 
Example 4
Source File: MenuItemLayoutHelper.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private String getAccText(String acceleratorDelimiter) {
    String accText = "";
    KeyStroke accelerator = mi.getAccelerator();
    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            accText = KeyEvent.getKeyModifiersText(modifiers);
            accText += acceleratorDelimiter;
        }
        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            accText += KeyEvent.getKeyText(keyCode);
        } else {
            accText += accelerator.getKeyChar();
        }
    }
    return accText;
}
 
Example 5
Source File: MenuShortcut.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an internationalized description of the MenuShortcut.
 * @return a string representation of this MenuShortcut.
 * @since JDK1.1
 */
public String toString() {
    int modifiers = 0;
    if (!GraphicsEnvironment.isHeadless()) {
        modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    }
    if (usesShiftModifier()) {
        modifiers |= Event.SHIFT_MASK;
    }
    return KeyEvent.getKeyModifiersText(modifiers) + "+" +
           KeyEvent.getKeyText(key);
}
 
Example 6
Source File: SwingTools.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Converts {@link KeyStroke} to a human-readable string.
 *
 * @param keyStroke
 *            the keystroke
 * @return a human-readable string like 'Ctrl+E'
 */
public static String formatKeyStroke(KeyStroke keyStroke) {
	StringBuilder builder = new StringBuilder();
	String modifierString = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
	String keyString = KeyEvent.getKeyText(keyStroke.getKeyCode());

	if (modifierString != null && !modifierString.trim().isEmpty()) {
		builder.append(modifierString);
		builder.append("+");
	}

	builder.append(keyString);

	return builder.toString();
}
 
Example 7
Source File: ConsumedKeyTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final int key) throws Exception {
    passed = false;
    try {
        SwingUtilities.invokeAndWait(() -> {
            frame = new JFrame();
            JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
            JPanel panel = new JPanel();
            panel.add(combo);
            combo.requestFocusInWindow();
            frame.setBounds(100, 150, 300, 100);
            addAction(panel, key);
            frame.add(panel);
            frame.setVisible(true);
        });

        ExtendedRobot robot = new ExtendedRobot();
        robot.waitForIdle();
        robot.type(key);
        robot.waitForIdle();
        if (!passed) {
            throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
        }
    } finally {
        if (frame != null) {
            frame.dispose();
        }
    }

}
 
Example 8
Source File: RendererSwing.java    From nullpomino with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String getKeyNameByButtonID(GameEngine engine, int btnID) {
	int[] keymap = engine.isInGame ? GameKeySwing.gamekey[engine.playerID].keymap : GameKeySwing.gamekey[engine.playerID].keymapNav;

	if((btnID >= 0) && (btnID < keymap.length)) {
		int keycode = keymap[btnID];
		return KeyEvent.getKeyText(keycode);
	}

	return "";
}
 
Example 9
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 10
Source File: CreateWorkDialog.java    From workcraft with MIT License 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list,
        Object value, int index, boolean isSelected, boolean cellHasFocus) {

    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (!isSelected && (index % 2 != 0)) {
        Color color = getBackground();
        setBackground(ColorUtils.colorise(color, Color.LIGHT_GRAY));
        setOpaque(true);
    }
    String shortcut = KeyEvent.getKeyText(getKeyCode(index));
    String text = getText().replace(SHORTCUT_PLACEHOLDER, shortcut);
    setText(text);
    return this;
}
 
Example 11
Source File: ConsumedKeyTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final int key) throws Exception {
    passed = false;
    try {
        SwingUtilities.invokeAndWait(() -> {
            frame = new JFrame();
            JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
            JPanel panel = new JPanel();
            panel.add(combo);
            combo.requestFocusInWindow();
            frame.setBounds(100, 150, 300, 100);
            addAction(panel, key);
            frame.add(panel);
            frame.setVisible(true);
        });

        Robot robot = new Robot();
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyPress(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyRelease(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        if (!passed) {
            throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
        }
    } finally {
        if (frame != null) {
            frame.dispose();
        }
    }

}
 
Example 12
Source File: KeyShortcut.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public String toString() {
  final String modifierText = KeyEvent.getKeyModifiersText(this.modifiers);
  final String keyText = KeyEvent.getKeyText(this.keyCode);

  final StringBuilder builder = new StringBuilder(modifierText);

  if (builder.length() > 0 && !keyText.isEmpty()) {
    builder.append('+');
  }
  builder.append(keyText);

  return builder.toString();
}
 
Example 13
Source File: ConsumedKeyTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final int key) throws Exception {
    passed = false;
    try {
        SwingUtilities.invokeAndWait(() -> {
            frame = new JFrame();
            JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
            JPanel panel = new JPanel();
            panel.add(combo);
            combo.requestFocusInWindow();
            frame.setBounds(100, 150, 300, 100);
            addAction(panel, key);
            frame.add(panel);
            frame.setVisible(true);
        });

        Robot robot = new Robot();
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyPress(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyRelease(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        if (!passed) {
            throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
        }
    } finally {
        if (frame != null) {
            frame.dispose();
        }
    }

}
 
Example 14
Source File: MenuShortcut.java    From jdk-1.7-annotated 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 JDK1.1
 */
public String toString() {
    int modifiers = 0;
    if (!GraphicsEnvironment.isHeadless()) {
        modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    }
    if (usesShiftModifier()) {
        modifiers |= Event.SHIFT_MASK;
    }
    return KeyEvent.getKeyModifiersText(modifiers) + "+" +
           KeyEvent.getKeyText(key);
}
 
Example 15
Source File: KeyBindParser.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write the current keybindings to the default XML file.
 */
public static void writeKeyBindings(){
    try {
        Writer output = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new MegaMekFile(Configuration.configDir(), 
                        DEFAULT_BINDINGS_FILE).getFile())));
        output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        output.write("<KeyBindings " +
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                " xsi:noNamespaceSchemaLocation=\"keyBindingSchema.xsl\">\n");
        
        for (KeyCommandBind kcb : KeyCommandBind.values()){
            output.write("    <KeyBind>\n");
            output.write("         <command>"+kcb.cmd+"</command> ");
            String keyTxt = "";
            if (kcb.modifiers != 0) {
                keyTxt = KeyEvent.getKeyModifiersText(kcb.modifiers);
                keyTxt += "-";
            }
            keyTxt += KeyEvent.getKeyText(kcb.key);
            output.write("<!-- " + keyTxt + " -->\n");
            output.write("        <keyCode>"+kcb.key+"</keyCode>\n");
            output.write("        <modifier>"+kcb.modifiers+"</modifier>\n");
            output.write("        <isRepeatable>"+kcb.isRepeatable
                    +"</isRepeatable>\n");
            output.write("    </KeyBind>\n");
            output.write("\n");
        }
        
        output.write("</KeyBindings>");
        output.close();
    } catch (IOException e) {
        System.err.println("Error writing keybindings file!");
        e.printStackTrace(System.err);
    }

    
}
 
Example 16
Source File: ConsumedKeyTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final int key) throws Exception {
    passed = false;
    try {
        SwingUtilities.invokeAndWait(() -> {
            frame = new JFrame();
            JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
            JPanel panel = new JPanel();
            panel.add(combo);
            combo.requestFocusInWindow();
            frame.setBounds(100, 150, 300, 100);
            addAction(panel, key);
            frame.add(panel);
            frame.setVisible(true);
        });

        Robot robot = new Robot();
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyPress(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyRelease(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        if (!passed) {
            throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
        }
    } finally {
        if (frame != null) {
            frame.dispose();
        }
    }

}
 
Example 17
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 18
Source File: OSUtils.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
public static String keyEventGetKeyText(int keycode) {
    if (keycode == KeyEvent.VK_TAB) {
        return "Tab";
    }
    if (keycode == KeyEvent.VK_CONTROL) {
        return "Ctrl";
    }
    if (keycode == KeyEvent.VK_ALT) {
        return "Alt";
    }
    if (keycode == KeyEvent.VK_SHIFT) {
        return "Shift";
    }
    if (keycode == KeyEvent.VK_META) {
        return "Meta";
    }
    if (keycode == KeyEvent.VK_SPACE) {
        return "Space";
    }
    if (keycode == KeyEvent.VK_BACK_SPACE) {
        return "Backspace";
    }
    if (keycode == KeyEvent.VK_HOME) {
        return "Home";
    }
    if (keycode == KeyEvent.VK_END) {
        return "End";
    }
    if (keycode == KeyEvent.VK_DELETE) {
        return "Delete";
    }
    if (keycode == KeyEvent.VK_PAGE_UP) {
        return "Pageup";
    }
    if (keycode == KeyEvent.VK_PAGE_DOWN) {
        return "Pagedown";
    }
    if (keycode == KeyEvent.VK_UP) {
        return "Up";
    }
    if (keycode == KeyEvent.VK_DOWN) {
        return "Down";
    }
    if (keycode == KeyEvent.VK_LEFT) {
        return "Left";
    }
    if (keycode == KeyEvent.VK_RIGHT) {
        return "Right";
    }
    if (keycode == KeyEvent.VK_ENTER) {
        return "Enter";
    }
    return KeyEvent.getKeyText(keycode);
}
 
Example 19
Source File: Action.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * returns String representation of shortcut
 *
 * @return String representation of shortcut
 */
@Override
public String toString() {
    String s = KeyEvent.getKeyModifiersText(getKeyModifiers());
    return s + (s.length() > 0 ? "+" : "") + KeyEvent.getKeyText(getKeyCode());
}
 
Example 20
Source File: WebHotkeyLabel.java    From weblaf with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs hotkey label for the specified key code and modifiers.
 *
 * @param id        {@link StyleId}
 * @param keyCode   single key code
 * @param modifiers hotkey modifiers
 */
public WebHotkeyLabel ( final StyleId id, final int keyCode, final int modifiers )
{
    super ( id, KeyEvent.getKeyModifiersText ( modifiers ) + "+" + KeyEvent.getKeyText ( keyCode ) );
}