soad el-hayek
soad el-hayek

Reputation: 63

How to check KeyListener input?

In java.awt.event.KeyListener, how do I check if the user enters "+" or "- "?

Upvotes: 0

Views: 502

Answers (1)

Steve McLeod
Steve McLeod

Reputation: 52448

Here's a code example of how to do this:

    TextField field = new TextField();
    field.addKeyListener(new KeyListener() {
        public void keyTyped(KeyEvent e) {
            final char aChar = e.getKeyChar();
            if (aChar == '+') {
                System.out.println("+ was typed");
            }
            if (aChar == '-') {
                System.out.println("- was typed");
            }
        }

        public void keyPressed(KeyEvent e) {
        }

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

Upvotes: 1

Related Questions