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:Main.java

public void keyPressed(KeyEvent evt) {
    if (evt.getKeyChar() == 'a') {
        System.out.println("Check for key characters: " + evt.getKeyChar());
    }/*from  ww  w.j  a v a  2 s  . c o  m*/
    if (evt.getKeyCode() == KeyEvent.VK_HOME) {
        System.out.println("Check for key codes: " + evt.getKeyCode());
    }
}

From source file:Main.java

public void keyTyped(KeyEvent evt) {
    if (evt.getKeyChar() == 'a') {
        System.out.println("Check for key characters: " + evt.getKeyChar());
    }//  w  ww . j a  v  a2  s  . c  o  m
    if (evt.getKeyCode() == KeyEvent.VK_HOME) {
        System.out.println("Check for key codes: " + evt.getKeyCode());
    }
}

From source file:Main.java

public void keyReleased(KeyEvent evt) {
    if (evt.getKeyChar() == 'a') {
        System.out.println("Check for key characters: " + evt.getKeyChar());
    }/*from   w ww  .  ja va 2  s.  c  o m*/
    if (evt.getKeyCode() == KeyEvent.VK_HOME) {
        System.out.println("Check for key codes: " + evt.getKeyCode());
    }
}

From source file:MyKeyListener.java

public void keyTyped(KeyEvent evt) {
    JTextComponent c = (JTextComponent) evt.getSource();
    char ch = evt.getKeyChar();

    if (Character.isLowerCase(ch) == false) {
        return;//from  www . ja v  a 2s.  c om
    }
    try {
        c.getDocument().insertString(c.getCaretPosition(), "" + Character.toUpperCase(ch), null);
        evt.consume();
    } catch (BadLocationException e) {
    }
}

From source file:Main.java

public void setEditor(ComboBoxEditor anEditor) {
    super.setEditor(anEditor);
    if (anEditor.getEditorComponent() instanceof JTextField) {
        tf = (JTextField) anEditor.getEditorComponent();
        tf.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ev) {
                char key = ev.getKeyChar();
                if (!(Character.isLetterOrDigit(key) || Character.isSpaceChar(key))) {
                    return;
                }//  ww w  .ja v a 2s. com
                String s = tf.getText();
                caretPos = tf.getCaretPosition();
                try {
                    String text = tf.getText(0, caretPos);
                    int n = getItemCount();
                    for (int i = 0; i < n; i++) {
                        int ind = ((String) getItemAt(i)).indexOf(text);
                        if (ind == 0) {
                            setSelectedIndex(i);
                            return;
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

From source file:Main.java

public Main() {
    DefaultTableModel model = new DefaultTableModel(0, 5) {
        @Override/*  w  ww.  j ava  2 s  . c  om*/
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    JTable table = new JTable(model);
    for (int i = 0; i < 20; i++) {
        model.addRow(new String[] { i + ".1", i + ".2", i + ".3", i + ".4", i + ".5", });
    }
    add(table, BorderLayout.CENTER);
    table.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            System.out.println("pressed");
            char key = e.getKeyChar();
            int selectedColumn = table.getSelectedColumn();
            for (int i = 0; i < model.getRowCount(); i++) {
                String value = (String) model.getValueAt(i, selectedColumn);
                model.setValueAt(value + key, i, selectedColumn);
            }
        }
    });
}

From source file:pl.otros.logview.gui.actions.table.MarkRowBySpaceKeyListener.java

@Override
public void keyReleased(KeyEvent e) {
    char keyChar = e.getKeyChar();
    if (keyChar == ' ') {
        markUnmarkRow();//  w w  w  .  j a v a  2  s  .  c o m
    }
}

From source file:org.nekorp.workflow.desktop.view.resource.imp.MonedaTextField.java

public MonedaTextField() {
    super();/*from  www.  j av  a 2 s .co m*/
    this.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            char car = e.getKeyChar();
            if (car != '.') {
                if ((car < '0' || car > '9')) {
                    e.consume();
                }
            } else {
                if (StringUtils.contains(getText(), '.')) {
                    e.consume();
                }
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
}

From source file:Main.java

public void keyPressed(KeyEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();

    int curIx = cb.getSelectedIndex();

    char ch = evt.getKeyChar();

    JComboBox.KeySelectionManager ksm = cb.getKeySelectionManager();
    if (ksm != null) {
        int ix = ksm.selectionForKey(ch, cb.getModel());
        boolean noMatch = ix < 0;
        boolean uniqueItem = ix == curIx;

        if (noMatch || !uniqueItem) {
            cb.showPopup();/* w  w  w  . j  a v a  2  s.c om*/
        }
    }
}

From source file:Main.java

public void keyPressed(KeyEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();

    int curIx = cb.getSelectedIndex();

    char ch = evt.getKeyChar();

    JComboBox.KeySelectionManager ksm = cb.getKeySelectionManager();
    if (ksm != null) {
        // Determine if another item has the same prefix
        int ix = ksm.selectionForKey(ch, cb.getModel());
        boolean noMatch = ix < 0;
        boolean uniqueItem = ix == curIx;

        // Display menu if no matching items or the if the selection is not unique
        if (noMatch || !uniqueItem) {
            cb.showPopup();/*from ww  w . j a  v  a 2  s.  co m*/
        }
    }
}