Java Utililty Methods Swing Key Event

List of utility methods to do Swing Key Event

Description

The list of methods to do Swing Key Event are organized into topic(s).

Method

booleancheckRowDownEvent(KeyEvent evt)
check Row Down Event
int keyCode = evt.getKeyCode();
if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_TAB || keyCode == KeyEvent.VK_ENTER
        || keyCode == KeyEvent.VK_KP_DOWN || keyCode == KeyEvent.VK_RIGHT) {
    return true;
} else {
    return false;
booleanesFecha(KeyEvent e)
es Fecha
char c = e.getKeyChar();
if (!Character.isDigit(c) && c != KeyEvent.VK_MINUS) {
    return true;
return false;
voidinsertKeyListener(Component component, KeyListener l, int index)
Inserts the key listener at the particular index in the listeners' chain.
KeyListener[] listeners = component.getKeyListeners();
for (KeyListener listener : listeners) {
    component.removeKeyListener(listener);
for (int i = 0; i < listeners.length; i++) {
    KeyListener listener = listeners[i];
    if (index == i) {
        component.addKeyListener(l);
...
StringkeyEventGetKeyText(int keycode)
key Event Get Key Text
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";
...
intkeyEventModifiersToKeyStrokeModifiers(int modifiers)
key Event Modifiers To Key Stroke Modifiers
int shift = InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK;
int ctrl = InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK;
int alt = InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK;
switch (modifiers) {
case 1:
    return shift;
case 2:
    return ctrl;
...
intkeyStrokeModMac(String keyStrokeStr)
key Stroke Mod Mac
keyStrokeStr = keyStrokeStr.toLowerCase();
int mod = 0;
if (keyStrokeStr.contains("ctrl") || keyStrokeStr.contains("control")) {
    mod = mod | InputEvent.CTRL_MASK;
if (keyStrokeStr.contains("alt")) {
    mod = mod | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
if (keyStrokeStr.contains("meta")) {
    mod = mod | InputEvent.ALT_MASK;
return mod;
voidpress(int key)
press
Robot robot = null;
try {
    robot = new Robot();
} catch (AWTException e) {
    e.printStackTrace();
robot.setAutoDelay(80);
robot.keyPress(key);
...
voidpressKey(final int key)
Press key.
try {
    final Robot robot = new Robot();
    robot.keyPress(key);
} catch (final AWTException e) {
voidrecipientHintSelected(String hintString, TextField toList, boolean shiftKeyPressed)
recipient Hint Selected
if (shiftKeyPressed) {
    String currentList = toList.getText();
    if (currentList == null || currentList.length() == 0)
        toList.setText(hintString);
    else
        toList.setText(currentList + ", " + hintString);
} else
    toList.setText(hintString);
...
voidrejectNonNumericKeys(KeyEvent evt)
reject Non Numeric Keys
char c = evt.getKeyChar();
if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)
        || (c == KeyEvent.VK_ENTER))) {
    Toolkit.getDefaultToolkit().beep();
    evt.consume();