Example usage for java.awt.event KeyEvent getKeyLocation

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

Introduction

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

Prototype

public int getKeyLocation() 

Source Link

Document

Returns the location of the key that originated this key event.

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.jav a  2s .c o  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: ");
        }/*ww  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:com.github.fritaly.dualcommander.TabbedPane.java

@Override
public void keyReleased(KeyEvent e) {
    if (e.getSource() == getSelectedComponent()) {
        // Propagate the event to our listeners
        processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(),
                e.getKeyChar(), e.getKeyLocation()));
    }/*from   w  w w.j a v a  2  s. co m*/
}

From source file:com.github.fritaly.dualcommander.TabbedPane.java

@Override
public void keyTyped(KeyEvent e) {
    if (e.getSource() == getSelectedComponent()) {
        // Propagate the event to our listeners
        processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(),
                e.getKeyChar(), e.getKeyLocation()));
    }/*www  .ja  va 2  s  .c  om*/
}

From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java

@Override
public void keyReleased(KeyEvent e) {
    if (e.getSource() != table) {
        return;//from w  ww.ja v a 2s. c  o m
    }

    // Propagate event to our listeners
    processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(),
            e.getKeyLocation()));
}

From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java

@Override
public void keyTyped(KeyEvent e) {
    if (e.getSource() != table) {
        return;/* ww w. ja  va2s.com*/
    }

    // Propagate event to our listeners
    processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(),
            e.getKeyLocation()));
}

From source file:Main.java

private void displayInfo(KeyEvent e, String keyStatus) {
    int id = e.getID();
    String keyString;//from www.  j  av a 2 s .c om
    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 av a  2s  .  c o  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 ww .j  av 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 .  ja  v  a  2  s.  com
    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());
}