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:com.dbschools.quickquiz.client.giver.MainWindow.java

private void addListeners() {
    takerTableDisplay.addTableKeyListener(new KeyAdapter() {
        @Override/*from  w w  w  .  j  a v  a 2s.  c o m*/
        public void keyPressed(KeyEvent e) {
            char keyChar = e.getKeyChar();
            if (Character.isDigit(keyChar) && e.isControlDown()) {
                awardPoints(Integer.parseInt(Character.toString(keyChar)));
            }
        }
    });
    final ListSelectionModel takerTableSelectionModel = takerTableDisplay.getSelectionModel();
    takerTableSelectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            btnAwardPoints.setEnabled(!takerTableSelectionModel.isSelectionEmpty());
        }
    });
    countdownMeter.addCountdownFinishListener(new CountdownFinishListener() {
        public void countdownFinished() {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    btnSendQuestion.setEnabled(true);
                }
            });
        }
    });
}

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  .jav  a2 s . c  o  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:schoolsystem.FeeModule.java

private void txt_fee_moduleKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_fee_moduleKeyTyped
    if (evt.getKeyChar() >= '0' && evt.getKeyChar() <= '9') {

    } else {/*from   w w w  . java 2  s.c o  m*/
        if (evt.getKeyChar() == KeyEvent.VK_SLASH) {

        } else {
            evt.consume();
            Toolkit.getDefaultToolkit().beep();
        }

    } // TODO add your handling code here:
}

From source file:schoolsystem.FeeModule.java

private void txt_fee_module_receiptKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_fee_module_receiptKeyTyped
    if (evt.getKeyChar() < '0' || evt.getKeyChar() > '9') {
        Toolkit.getDefaultToolkit().beep();
        evt.consume();//ww  w.  j av  a 2  s .  c om
    }
}

From source file:schoolsystem.FeeModule.java

private void txt_fee_module_cheque_noKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_fee_module_cheque_noKeyTyped
    if (evt.getKeyChar() < '0' || evt.getKeyChar() > '9') {
        Toolkit.getDefaultToolkit().beep();
        evt.consume();/*from  w w  w  .  j  ava  2  s . c o  m*/
    } // TODO add your handling code here:
}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

@Override
public void keyTyped(KeyEvent e) {
    LOGGER.debug("key typed event caught " + e.getKeyCode());
    char ch = e.getKeyChar();
    if (ch == '\n') {
        // this case is handled at keyPressed(e)
        return;//  ww  w .j  a va  2s  . c o  m
    }

    // don't do auto completion inside words
    if (!atEndOfWord((JTextComponent) e.getSource())) {
        return;
    }

    if ((e.getModifiers() | InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) {
        // plain key or SHIFT + key is pressed, no handling of CTRL+key,  META+key, ...
        if (Character.isLetter(ch) || Character.isDigit(ch)
                || (Character.isWhitespace(ch) && completer.isSingleUnitField())) {
            JTextComponent comp = (JTextComponent) e.getSource();

            if (toSetIn == null) {
                LOGGER.debug("toSetIn is null");
            } else {
                LOGGER.debug("toSetIn: >" + toSetIn + '<');
            }

            // The case-insensitive system is a bit tricky here
            // If keyword is "TODO" and user types "tO", then this is treated as "continue" as the "O" matches the "O"
            // If keyword is "TODO" and user types "To", then this is treated as "discont" as the "o" does NOT match the "O".

            if ((toSetIn != null) && (toSetIn.length() > 1) && (ch == toSetIn.charAt(1))) {
                // User continues on the word that was suggested.
                LOGGER.debug("cont");

                toSetIn = toSetIn.substring(1);
                if (!toSetIn.isEmpty()) {
                    int cp = comp.getCaretPosition();
                    //comp.setCaretPosition(cp+1-toSetIn.);
                    comp.select((cp + 1) - toSetIn.length(), cp);
                    lastBeginning = lastBeginning + ch;

                    e.consume();
                    lastCaretPosition = comp.getCaretPosition();

                    lastCompletions = findCompletions(lastBeginning);
                    lastShownCompletion = 0;
                    for (int i = 0; i < lastCompletions.size(); i++) {
                        String lastCompletion = lastCompletions.get(i);
                        if (lastCompletion.endsWith(toSetIn)) {
                            lastShownCompletion = i;
                            break;
                        }

                    }
                    if (toSetIn.length() < 2) {
                        // User typed the last character of the autocompleted word
                        // We have to replace the automcompletion word by the typed word.
                        // This helps if the user presses "space" after the completion
                        // "space" indicates that the user does NOT want the autocompletion,
                        // but the typed word
                        String text = comp.getText();
                        comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length())
                                + lastBeginning + text.substring(lastCaretPosition));
                        // there is no selected text, therefore we are not updating the selection
                        toSetIn = null;
                    }
                    return;
                }
            }

            if ((toSetIn != null) && ((toSetIn.length() <= 1) || (ch != toSetIn.charAt(1)))) {
                // User discontinues the word that was suggested.
                lastBeginning = lastBeginning + ch;

                LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<');

                List<String> completed = findCompletions(lastBeginning);
                if ((completed != null) && (!completed.isEmpty())) {
                    lastShownCompletion = 0;
                    lastCompletions = completed;
                    String sno = completed.get(0);
                    // toSetIn = string used for autocompletion last time
                    // this string has to be removed
                    // lastCaretPosition is the position of the caret after toSetIn.
                    int lastLen = toSetIn.length() - 1;
                    toSetIn = sno.substring(lastBeginning.length() - 1);
                    String text = comp.getText();
                    //we do not use toSetIn as we want to obey the casing of "sno"
                    comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1)
                            + sno + text.substring(lastCaretPosition));
                    int startSelect = (lastCaretPosition + 1) - lastLen;
                    int endSelect = (lastCaretPosition + toSetIn.length()) - lastLen;
                    comp.select(startSelect, endSelect);

                    lastCaretPosition = comp.getCaretPosition();
                    e.consume();
                    return;
                } else {
                    setUnmodifiedTypedLetters(comp, true, false);
                    e.consume();
                    toSetIn = null;
                    return;
                }
            }

            LOGGER.debug("case else");

            comp.replaceSelection("");

            StringBuffer currentword = getCurrentWord(comp);

            // only "real characters" end up here
            assert (!Character.isISOControl(ch));
            currentword.append(ch);
            startCompletion(currentword, e);
            return;
        } else {
            if (Character.isWhitespace(ch)) {
                assert (!completer.isSingleUnitField());
                LOGGER.debug("whitespace && !singleUnitField");
                // start a new search if end-of-field is reached

                // replace displayed letters with typed letters
                setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, true);
                resetAutoCompletion();
                return;
            }

            LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED");
            // replace displayed letters with typed letters
            setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, !Character.isISOControl(ch));
            resetAutoCompletion();
            return;
        }
    }
    resetAutoCompletion();
}

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

@Override
public void keyTyped(KeyEvent e) {
    LOGGER.debug("key typed event caught " + e.getKeyCode());
    char ch = e.getKeyChar();
    if (ch == '\n') {
        // this case is handled at keyPressed(e)
        return;/*from   w ww.  ja  va2 s . co  m*/
    }

    if ((e.getModifiers() | InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) {
        // plain key or SHIFT + key is pressed, no handling of CTRL+key,  META+key, ...
        if (Character.isLetter(ch) || Character.isDigit(ch)
                || (Character.isWhitespace(ch) && completer.isSingleUnitField())) {
            JTextComponent comp = (JTextComponent) e.getSource();

            if (toSetIn == null) {
                LOGGER.debug("toSetIn is null");
            } else {
                LOGGER.debug("toSetIn: >" + toSetIn + '<');
            }

            // The case-insensitive system is a bit tricky here
            // If keyword is "TODO" and user types "tO", then this is treated as "continue" as the "O" matches the "O"
            // If keyword is "TODO" and user types "To", then this is treated as "discont" as the "o" does NOT match the "O".

            if ((toSetIn != null) && (toSetIn.length() > 1) && (ch == toSetIn.charAt(1))) {
                // User continues on the word that was suggested.
                LOGGER.debug("cont");

                toSetIn = toSetIn.substring(1);
                if (!toSetIn.isEmpty()) {
                    int cp = comp.getCaretPosition();
                    //comp.setCaretPosition(cp+1-toSetIn.);
                    //System.out.println(cp-toSetIn.length()+" - "+cp);
                    comp.select((cp + 1) - toSetIn.length(), cp);
                    lastBeginning = lastBeginning + ch;

                    e.consume();
                    lastCaretPosition = comp.getCaretPosition();

                    //System.out.println("Added char: '"+toSetIn+"'");
                    //System.out.println("LastBeginning: '"+lastBeginning+"'");

                    lastCompletions = findCompletions(lastBeginning, comp);
                    lastShownCompletion = 0;
                    for (int i = 0; i < lastCompletions.length; i++) {
                        String lastCompletion = lastCompletions[i];
                        //System.out.println("Completion["+i+"] = "+lastCompletion);
                        if (lastCompletion.endsWith(toSetIn)) {
                            lastShownCompletion = i;
                            break;
                        }

                    }
                    //System.out.println("Index now: "+lastShownCompletion);
                    if (toSetIn.length() < 2) {
                        // User typed the last character of the autocompleted word
                        // We have to replace the automcompletion word by the typed word.
                        // This helps if the user presses "space" after the completion
                        // "space" indicates that the user does NOT want the autocompletion,
                        // but the typed word
                        String text = comp.getText();
                        comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length())
                                + lastBeginning + text.substring(lastCaretPosition));
                        // there is no selected text, therefore we are not updating the selection
                        toSetIn = null;
                    }
                    return;
                }
            }

            if ((toSetIn != null) && ((toSetIn.length() <= 1) || (ch != toSetIn.charAt(1)))) {
                // User discontinues the word that was suggested.
                lastBeginning = lastBeginning + ch;

                LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<');

                String[] completed = findCompletions(lastBeginning, comp);
                if ((completed != null) && (completed.length > 0)) {
                    lastShownCompletion = 0;
                    lastCompletions = completed;
                    String sno = completed[0];
                    // toSetIn = string used for autocompletion last time
                    // this string has to be removed
                    // lastCaretPosition is the position of the caret after toSetIn.
                    int lastLen = toSetIn.length() - 1;
                    toSetIn = sno.substring(lastBeginning.length() - 1);
                    String text = comp.getText();
                    //Util.pr(""+lastLen);
                    //we do not use toSetIn as we want to obey the casing of "sno"
                    comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1)
                            + sno + text.substring(lastCaretPosition));
                    int startSelect = (lastCaretPosition + 1) - lastLen;
                    int endSelect = (lastCaretPosition + toSetIn.length()) - lastLen;
                    comp.select(startSelect, endSelect);

                    lastCaretPosition = comp.getCaretPosition();
                    e.consume();
                    return;
                } else {
                    setUnmodifiedTypedLetters(comp, true, false);
                    e.consume();
                    toSetIn = null;
                    return;
                }
            }

            LOGGER.debug("case else");

            comp.replaceSelection("");

            StringBuffer currentword = getCurrentWord(comp);
            if (currentword == null) {
                currentword = new StringBuffer();
            }

            // only "real characters" end up here
            assert (!Character.isISOControl(ch));
            currentword.append(ch);
            startCompletion(currentword, e);
            return;
        } else {
            if (Character.isWhitespace(ch)) {
                assert (!completer.isSingleUnitField());
                LOGGER.debug("whitespace && !singleUnitField");
                // start a new search if end-of-field is reached

                // replace displayed letters with typed letters
                setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, true);
                resetAutoCompletion();
                return;
            }

            LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED");
            // replace displayed letters with typed letters 
            setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, !Character.isISOControl(ch));
            resetAutoCompletion();
            return;
        }
    }
    resetAutoCompletion();
}

From source file:schoolsystem.FeeModule.java

private void txt_fee_module_amt_paidKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_fee_module_amt_paidKeyTyped
    // TODO add your handling code here:

    if (evt.getKeyChar() >= '0' && evt.getKeyChar() <= '9') {

    } else {/*from  ww  w .j  a v  a2s . c  om*/
        evt.consume();
        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:client.InterfaceJeu.java

private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    if (evt.isControlDown() && evt.getKeyChar() == 'C') {
        txtFieldChatEcr.grabFocus();//from   w w  w .ja va  2 s . c o  m
        txtFieldChatEcr.requestFocus();
        System.out.println("keypress");
    }
}

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  a 2s .  co  m
 */
@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;

    }
}