Example usage for java.awt.event KeyEvent isActionKey

List of usage examples for java.awt.event KeyEvent isActionKey

Introduction

In this page you can find the example usage for java.awt.event KeyEvent isActionKey.

Prototype

public boolean isActionKey() 

Source Link

Document

Returns whether the key in this event is an "action" key.

Usage

From source file:KeyTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Key Listener");
    Container contentPane = frame.getContentPane();

    KeyListener listener = new KeyListener() {
        public void keyPressed(KeyEvent e) {
            dumpInfo("Pressed", e);
        }/*from   w  w  w  .  j a v a  2s . co  m*/

        public void keyReleased(KeyEvent e) {
            dumpInfo("Released", e);
        }

        public void keyTyped(KeyEvent e) {
            dumpInfo("Typed", e);
        }

        private void dumpInfo(String s, KeyEvent e) {
            System.out.println(s);
            int code = e.getKeyCode();
            System.out.println("\tCode: " + KeyEvent.getKeyText(code));
            System.out.println("\tChar: " + e.getKeyChar());
            int mods = e.getModifiersEx();
            System.out.println("\tMods: " + KeyEvent.getModifiersExText(mods));
            System.out.println("\tLocation: " + location(e.getKeyLocation()));
            System.out.println("\tAction? " + e.isActionKey());
        }

        private String location(int location) {
            switch (location) {
            case KeyEvent.KEY_LOCATION_LEFT:
                return "Left";
            case KeyEvent.KEY_LOCATION_RIGHT:
                return "Right";
            case KeyEvent.KEY_LOCATION_NUMPAD:
                return "NumPad";
            case KeyEvent.KEY_LOCATION_STANDARD:
                return "Standard";
            case KeyEvent.KEY_LOCATION_UNKNOWN:
            default:
                return "Unknown";
            }
        }
    };

    JTextField text = new JTextField();
    text.addKeyListener(listener);
    contentPane.add(text, BorderLayout.NORTH);
    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame();

    aWindow.setBounds(50, 100, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField typingArea = new JTextField(20);
    typingArea.addKeyListener(new KeyListener() {
        /** Handle the key typed event from the text field. */
        public void keyTyped(KeyEvent e) {
            displayInfo(e, "KEY TYPED: ");
        }//from  w  w w.ja v a  2 s. c  o m

        /** Handle the key pressed event from the text field. */
        public void keyPressed(KeyEvent e) {
            displayInfo(e, "KEY PRESSED: ");
        }

        /** Handle the key released event from the text field. */
        public void keyReleased(KeyEvent e) {
            displayInfo(e, "KEY RELEASED: ");
        }

        protected void displayInfo(KeyEvent e, String s) {
            String keyString, modString, tmpString, actionString, locationString;
            int id = e.getID();
            if (id == KeyEvent.KEY_TYPED) {
                char c = e.getKeyChar();
                keyString = "key character = '" + c + "'";
            } else {
                int keyCode = e.getKeyCode();
                keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
            }

            int modifiers = e.getModifiersEx();
            modString = "modifiers = " + modifiers;
            tmpString = KeyEvent.getModifiersExText(modifiers);
            if (tmpString.length() > 0) {
                modString += " (" + tmpString + ")";
            } else {
                modString += " (no modifiers)";
            }

            actionString = "action key? ";
            if (e.isActionKey()) {
                actionString += "YES";
            } else {
                actionString += "NO";
            }

            locationString = "key location: ";
            int location = e.getKeyLocation();
            if (location == KeyEvent.KEY_LOCATION_STANDARD) {
                locationString += "standard";
            } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
                locationString += "left";
            } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
                locationString += "right";
            } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
                locationString += "numpad";
            } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
                locationString += "unknown";
            }

            System.out.println(keyString);
            System.out.println(modString);
            System.out.println(actionString);
            System.out.println(locationString);
        }

    });
    aWindow.add(typingArea);
    aWindow.setVisible(true);
}

From source file:Main.java

private void displayInfo(KeyEvent e, String keyStatus) {
    int id = e.getID();
    String keyString;// www  .  ja  v  a2  s  .  c  o  m

    int modifiersEx = e.getModifiersEx();
    String modString = "extended modifiers = " + modifiersEx;
    String tmpString = KeyEvent.getModifiersExText(modifiersEx);
    if (tmpString.length() > 0) {
        modString += " (" + tmpString + ")";
    } else {
        modString += " (no extended modifiers)";
    }
    String actionString = "action key? ";
    if (e.isActionKey()) {
        actionString += "YES";
    } else {
        actionString += "NO";
    }
    System.out.println(keyStatus + "\n" + modString + "\n" + actionString + "\n");
}

From source file:net.sf.jabref.gui.maintable.MainTableSelectionListener.java

/**
 * Receive key event on the main table. If the key is a letter or a digit,
 * we should select the first entry in the table which starts with the given
 * letter in the column by which the table is sorted.
 * @param e The KeyEvent// w  w  w.  ja  va 2  s. co m
 */
@Override
public void keyTyped(KeyEvent e) {
    if ((!e.isActionKey()) && Character.isLetterOrDigit(e.getKeyChar()) && (e.getModifiers() == 0)) {
        long time = System.currentTimeMillis();
        final long QUICK_JUMP_TIMEOUT = 2000;
        if ((time - lastPressedTime) > QUICK_JUMP_TIMEOUT) {
            lastPressedCount = 0; // Reset last pressed character
        }
        // Update timestamp:
        lastPressedTime = time;
        // Add the new char to the search array:
        int c = e.getKeyChar();
        if (lastPressedCount < lastPressed.length) {
            lastPressed[lastPressedCount] = c;
            lastPressedCount++;
        }

        int sortingColumn = table.getSortingColumn(0);
        if (sortingColumn == -1) {
            return; // No sorting? TODO: look up by author, etc.?
        }
        // TODO: the following lookup should be done by a faster algorithm,
        // such as binary search. But the table may not be sorted properly,
        // due to marked entries, search etc., which rules out the binary search.

        for (int i = 0; i < table.getRowCount(); i++) {
            Object o = table.getValueAt(i, sortingColumn);
            if (o == null) {
                continue;
            }
            String s = o.toString().toLowerCase();
            if (s.length() >= lastPressedCount) {
                for (int j = 0; j < lastPressedCount; j++) {
                    if (s.charAt(j) != lastPressed[j]) {
                        break; // Escape the loop immediately when we find a mismatch
                    } else if (j == (lastPressedCount - 1)) {
                        // We found a match:
                        table.setRowSelectionInterval(i, i);
                        table.ensureVisible(i);
                        return;
                    }
                }
            }
        }

    } else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
        lastPressedCount = 0;
    }
}

From source file:net.sf.jabref.gui.MainTableSelectionListener.java

/**
 * Receive key event on the main table. If the key is a letter or a digit,
 * we should select the first entry in the table which starts with the given
 * letter in the column by which the table is sorted.
 * @param e The KeyEvent//from w  w  w  . j a v  a2  s .c om
 */
@Override
public void keyTyped(KeyEvent e) {
    if ((!e.isActionKey()) && Character.isLetterOrDigit(e.getKeyChar())
    //&& !e.isControlDown() && !e.isAltDown() && !e.isMetaDown()) {
            && (e.getModifiers() == 0)) {
        long time = System.currentTimeMillis();
        long QUICK_JUMP_TIMEOUT = 2000;
        if ((time - lastPressedTime) > QUICK_JUMP_TIMEOUT) {
            lastPressedCount = 0; // Reset last pressed character
        }
        // Update timestamp:
        lastPressedTime = time;
        // Add the new char to the search array:
        int c = e.getKeyChar();
        if (lastPressedCount < lastPressed.length) {
            lastPressed[lastPressedCount] = c;
            lastPressedCount++;
        }

        int sortingColumn = table.getSortingColumn(0);
        if (sortingColumn == -1) {
            return; // No sorting? TODO: look up by author, etc.?
        }
        // TODO: the following lookup should be done by a faster algorithm,
        // such as binary search. But the table may not be sorted properly,
        // due to marked entries, search etc., which rules out the binary search.
        int startRow = 0;
        /*if ((c == lastPressed) && (lastQuickJumpRow >= 0)) {
        if (lastQuickJumpRow < table.getRowCount()-1)
            startRow = lastQuickJumpRow+1;
        }*/

        boolean done = false;
        while (!done) {
            for (int i = startRow; i < table.getRowCount(); i++) {
                Object o = table.getValueAt(i, sortingColumn);
                if (o == null) {
                    continue;
                }
                String s = o.toString().toLowerCase();
                if (s.length() >= lastPressedCount) {
                    for (int j = 0; j < lastPressedCount; j++) {
                        if (s.charAt(j) != lastPressed[j]) {
                            break; // Escape the loop immediately when we find a mismatch
                        } else if (j == (lastPressedCount - 1)) {
                            // We found a match:
                            table.setRowSelectionInterval(i, i);
                            table.ensureVisible(i);
                            return;
                        }
                    }
                    //if ((s.length() >= 1) && (s.charAt(0) == c)) {
                    //}
                }
            }
            // Finished, no result. If we didn't start at the beginning of
            // the table, try that. Otherwise, exit the while loop.
            if (startRow > 0) {
                startRow = 0;
            } else {
                done = true;
            }

        }

    } else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
        lastPressedCount = 0;

    }
}

From source file:Main.java

private void displayInfo(KeyEvent e, String keyStatus) {
    int id = e.getID();
    String keyString;/*w w  w .ja v a2 s  .  c  o m*/
    if (id == KeyEvent.KEY_TYPED) {
        char c = e.getKeyChar();
        keyString = "key character = '" + c + "'";
    } else {
        int keyCode = e.getKeyCode();
        keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
    }

    int modifiersEx = e.getModifiersEx();
    String modString = "extended modifiers = " + modifiersEx;
    String tmpString = KeyEvent.getModifiersExText(modifiersEx);
    if (tmpString.length() > 0) {
        modString += " (" + tmpString + ")";
    } else {
        modString += " (no extended modifiers)";
    }

    String actionString = "action key? ";
    if (e.isActionKey()) {
        actionString += "YES";
    } else {
        actionString += "NO";
    }

    String locationString = "key location: ";
    int location = e.getKeyLocation();
    if (location == KeyEvent.KEY_LOCATION_STANDARD) {
        locationString += "standard";
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
        locationString += "left";
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
        locationString += "right";
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
        locationString += "numpad";
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
        locationString += "unknown";
    }

    displayArea.append(keyStatus + newline + "    " + keyString + newline + "    " + modString + newline
            + "    " + actionString + newline + "    " + locationString + newline);
    displayArea.setCaretPosition(displayArea.getDocument().getLength());
}

From source file:Main.java

private void displayInfo(KeyEvent e, String keyStatus) {
    int id = e.getID();
    String keyString;//from w w  w.j ava2s .  co  m
    if (id == KeyEvent.KEY_PRESSED) {
        char c = e.getKeyChar();
        keyString = "key character = '" + c + "'";
    } else {
        int keyCode = e.getKeyCode();
        keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
    }

    int modifiersEx = e.getModifiersEx();
    String modString = "extended modifiers = " + modifiersEx;
    String tmpString = KeyEvent.getModifiersExText(modifiersEx);
    if (tmpString.length() > 0) {
        modString += " (" + tmpString + ")";
    } else {
        modString += " (no extended modifiers)";
    }

    String actionString = "action key? ";
    if (e.isActionKey()) {
        actionString += "YES";
    } else {
        actionString += "NO";
    }

    String locationString = "key location: ";
    int location = e.getKeyLocation();
    if (location == KeyEvent.KEY_LOCATION_STANDARD) {
        locationString += "standard";
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
        locationString += "left";
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
        locationString += "right";
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
        locationString += "numpad";
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
        locationString += "unknown";
    }

    displayArea.append(keyStatus + newline + "    " + keyString + newline + "    " + modString + newline
            + "    " + actionString + newline + "    " + locationString + newline);
    displayArea.setCaretPosition(displayArea.getDocument().getLength());
}

From source file:Main.java

private void displayInfo(KeyEvent e, String keyStatus) {
    int id = e.getID();
    String keyString;/*  w  w w.jav  a2  s  .c o  m*/
    if (id == KeyEvent.KEY_RELEASED) {
        char c = e.getKeyChar();
        keyString = "key character = '" + c + "'";
    } else {
        int keyCode = e.getKeyCode();
        keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
    }

    int modifiersEx = e.getModifiersEx();
    String modString = "extended modifiers = " + modifiersEx;
    String tmpString = KeyEvent.getModifiersExText(modifiersEx);
    if (tmpString.length() > 0) {
        modString += " (" + tmpString + ")";
    } else {
        modString += " (no extended modifiers)";
    }

    String actionString = "action key? ";
    if (e.isActionKey()) {
        actionString += "YES";
    } else {
        actionString += "NO";
    }

    String locationString = "key location: ";
    int location = e.getKeyLocation();
    if (location == KeyEvent.KEY_LOCATION_STANDARD) {
        locationString += "standard";
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
        locationString += "left";
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
        locationString += "right";
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
        locationString += "numpad";
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
        locationString += "unknown";
    }

    displayArea.append(keyStatus + newline + "    " + keyString + newline + "    " + modString + newline
            + "    " + actionString + newline + "    " + locationString + newline);
    displayArea.setCaretPosition(displayArea.getDocument().getLength());
}

From source file:Main.java

private void displayInfo(KeyEvent e, String keyStatus) {

    // You should only rely on the key char if the event
    // is a key typed event.
    int id = e.getID();
    String keyString;/*from  ww  w.  j a va2s. c o  m*/
    if (id == KeyEvent.KEY_TYPED) {
        char c = e.getKeyChar();
        keyString = "key character = '" + c + "'";
    } else {
        int keyCode = e.getKeyCode();
        keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
    }

    int modifiersEx = e.getModifiersEx();
    String modString = "extended modifiers = " + modifiersEx;
    String tmpString = KeyEvent.getModifiersExText(modifiersEx);
    if (tmpString.length() > 0) {
        modString += " (" + tmpString + ")";
    } else {
        modString += " (no extended modifiers)";
    }

    String actionString = "action key? ";
    if (e.isActionKey()) {
        actionString += "YES";
    } else {
        actionString += "NO";
    }

    String locationString = "key location: ";
    int location = e.getKeyLocation();
    if (location == KeyEvent.KEY_LOCATION_STANDARD) {
        locationString += "standard";
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
        locationString += "left";
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
        locationString += "right";
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
        locationString += "numpad";
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
        locationString += "unknown";
    }

    displayArea.append(keyStatus + newline + "    " + keyString + newline + "    " + modString + newline
            + "    " + actionString + newline + "    " + locationString + newline);
    displayArea.setCaretPosition(displayArea.getDocument().getLength());
}

From source file:KeyEventDemo.java

protected void displayInfo(KeyEvent e, String s) {
    String keyString, modString, tmpString, actionString, locationString;

    //You should only rely on the key char if the event
    //is a key typed event.
    int id = e.getID();
    if (id == KeyEvent.KEY_TYPED) {
        char c = e.getKeyChar();
        keyString = "key character = '" + c + "'";
    } else {//ww w .  j  a va2 s .co  m
        int keyCode = e.getKeyCode();
        keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
    }

    int modifiers = e.getModifiersEx();
    modString = "modifiers = " + modifiers;
    tmpString = KeyEvent.getModifiersExText(modifiers);
    if (tmpString.length() > 0) {
        modString += " (" + tmpString + ")";
    } else {
        modString += " (no modifiers)";
    }

    actionString = "action key? ";
    if (e.isActionKey()) {
        actionString += "YES";
    } else {
        actionString += "NO";
    }

    locationString = "key location: ";
    int location = e.getKeyLocation();
    if (location == KeyEvent.KEY_LOCATION_STANDARD) {
        locationString += "standard";
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
        locationString += "left";
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
        locationString += "right";
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
        locationString += "numpad";
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
        locationString += "unknown";
    }

    displayArea.append(s + newline + "    " + keyString + newline + "    " + modString + newline + "    "
            + actionString + newline + "    " + locationString + newline);
    displayArea.setCaretPosition(displayArea.getDocument().getLength());
}