Example usage for java.awt.event KeyEvent getKeyChar

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

Introduction

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

Prototype

public char getKeyChar() 

Source Link

Document

Returns the character associated with the key in this event.

Usage

From source file:net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.BaseKeyTextHandler.java

/**
 * Ensure, that a sign character is entered at the right position.
 * Valid characters for a sign is the <code>-</code>. If the corresponding {@link ColumnDisplayDefinition#isSigned()} is 
 * <code>false</code>, than a sign is not allowed for this column.
 * Otherwise, a sign character is allowed at the first position of the text if there is not already a sign char present.
 * <B>If the entered sign character is not valid, then the keyEvent will be consumed and a beep will be processed.</B>
 * <p>For example:</p>//from   ww w. j a v  a2 s  .  c o m
 * Allowed:
 * <li>42</li>
 * <li>-42</li>
 * Not allowed:
 * <li>-.42</li>
 * <li>--42</li>
 * <li>42-42</li>
 * <li>-42-42</li>
 * <li>42-</li>
 * @param keyEvent KeyEvent, which insert a character
 * @param textComponent Text component, where the character was typed.
 * @param colDef Display definition of the affected column. This will be used, for finding out, if the data type can be signed.
 * @param beepHelper Helper for beeping, if a sign char is would be inserted at a wrong position.
 */
protected void checkSignCharacter(KeyEvent keyEvent, JTextComponent textComponent,
        ColumnDisplayDefinition colDef, IToolkitBeepHelper beepHelper) {
    char c = keyEvent.getKeyChar();
    String text = textComponent.getText();

    if (isSignCharacter(c)) {
        boolean ok = true;
        if (colDef.isSigned() == false) {
            ok = false;
        } else if (!text.equals("<null>") && text.length() != 0) {
            int caretPosition = textComponent.getCaretPosition();
            if (caretPosition != 0 || isSignCharacter(text.charAt(0))) {
                ok = false;
            }
        }
        if (ok == false) {
            /*
             *  user entered '+' or '-' at a bad place,
             *  Maybe not at the first position, or there is not a numeric char at the beginning - maybe we have already a sign
             */
            beepHelper.beep(textComponent);
            keyEvent.consume();
        }
    }
}

From source file:ja.lingo.application.gui.main.describer.DescriberGui.java

public void keyTypedOnEditorPane(KeyEvent e) {
    if (KeyEvent.VK_ESCAPE == e.getKeyChar()) {
        model.requestFocusInNavigator();
    }//  w  w w. jav  a  2  s . co  m
}

From source file:PassiveTextField.java

public void processComponentKeyEvent(KeyEvent evt) {
    switch (evt.getID()) {
    case KeyEvent.KEY_PRESSED:
    case KeyEvent.KEY_RELEASED:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            return;
        }/*w  w w .j  a v  a  2 s . co m*/
        break;

    case KeyEvent.KEY_TYPED:
        if (evt.getKeyChar() == '\r') {
            return;
        }
        break;
    }

    super.processComponentKeyEvent(evt);
}

From source file:JToggleButtonMenuItem.java

public void processKeyEvent(KeyEvent keyEvent, MenuElement path[], MenuSelectionManager manager) {
    // If user presses space while menu item armed, select it
    if (getModel().isArmed()) {
        int keyChar = keyEvent.getKeyChar();
        if (keyChar == KeyEvent.VK_SPACE) {
            manager.clearSelectedPath();
            System.out.println("Selected: JToggleButtonMenuItem, by KeyEvent");
            doClick(0); // inherited from AbstractButton
        }//from w  w  w  . j  a va 2s.  c om
    }
}

From source file:GeneradorLenguaje.java

private void txt_nKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_nKeyTyped
    // TODO add your handling code here:
    char a = evt.getKeyChar();
    if (a < '0' || a > '9') {
        evt.consume();//from   w  w w. j av  a2  s . co m
    }
}

From source file:GeneradorLenguaje.java

private void txt_iKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_iKeyTyped
    // TODO add your handling code here:
    char a = evt.getKeyChar();
    if (a < '0' || a > '9') {
        evt.consume();/*w  ww  . j a v  a  2 s  .  c o m*/
    }
}

From source file:playground.sergioo.capacitiesChanger2012.gui.NetworkPanel.java

@Override
public void keyTyped(KeyEvent e) {
    switch (e.getKeyChar()) {
    case 'n':
        getActiveLayer().changeVisible();
        break;/*  w w  w  .  j ava  2 s.co  m*/
    case 's':
        ((SimpleSelectionNetworkPainter) getPrincipalLayer().getPainter()).changeVisibleSelectedElements();
        break;
    case 'o':
        ((NetworkPainter) getPrincipalLayer().getPainter()).getNetworkPainterManager().selectOppositeLink();
        window.refreshLabel(Labels.LINK);
        break;
    case 'v':
        viewAll();
        break;
    case '+':
        if (addressLocator.getNumResults() > 0) {
            posLocation++;
            if (posLocation == addressLocator.getNumResults())
                posLocation = 0;
            Coord c = addressLocator.getLocation(posLocation);
            centerCamera(new double[] { c.getX(), c.getY() });
        }
        break;
    case '-':
        if (addressLocator.getNumResults() > 0) {
            posLocation--;
            if (posLocation < 0)
                posLocation = addressLocator.getNumResults() - 1;
            Coord c = addressLocator.getLocation(posLocation);
            centerCamera(new double[] { c.getX(), c.getY() });
        }
        break;
    }
    repaint();
}

From source file:playground.sergioo.networkBusLaneAdder2012.gui.BusLaneAdderPanel.java

@Override
public void keyTyped(KeyEvent e) {
    switch (e.getKeyChar()) {
    case 'v':
        viewAll();//from   w w  w . ja va2 s  .c  om
        break;
    case '+':
        if (addressLocator.getNumResults() > 0) {
            posLocation++;
            if (posLocation == addressLocator.getNumResults())
                posLocation = 0;
            Coord c = addressLocator.getLocation(posLocation);
            centerCamera(new double[] { c.getX(), c.getY() });
        }
        break;
    case '-':
        if (addressLocator.getNumResults() > 0) {
            posLocation--;
            if (posLocation < 0)
                posLocation = addressLocator.getNumResults() - 1;
            Coord c = addressLocator.getLocation(posLocation);
            centerCamera(new double[] { c.getX(), c.getY() });
        }
        break;
    }
    repaint();
}

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   w w  w  .  ja va2 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) {

    // You should only rely on the key char if the event
    // is a key typed event.
    int id = e.getID();
    String keyString;/*from  w w w . j  a  va  2s.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);
    System.out.println(e.isAltGraphDown());

    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());
}