event

KeyListener example

In this example we are going to see how you can use a KeyListener in Java, in order to monitor key events that take place in your applications. This is particularly useful when you want to add some key functionality to your app and it’s very important you want to monitor that has a very intensive keyboard activity.

Basically, to use a KeyListener in Java you have to:

  • Create a new KeyListener instance.
  • Override the methods that correspond to the key events you want to monitor e.gkeyPressedkeyReleasedkeyTyped.
  • Create a JTextField component
  • Use it’s addKeyListener method to add to it the KeyListener you’ve created.
  • Create a MouseAdapter instance.
  • Override mousePressed method to monitor the mouse button activities.

Let’s take a closer look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class KeyTest {

    public static void main(String args[]) {

  JFrame jfr = new JFrame("Key Text Sample");

  KeyTextComponent keyTtx = new KeyTextComponent();

  final JTextField jtf = new JTextField();

  ActionListener actListener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent actionEvent) {

    String keyText = actionEvent.getActionCommand();

    jtf.setText(keyText);

}

  };

  keyTtx.addActionListener(actListener);

  Container cp = jfr.getContentPane();

  cp.add(keyTtx, BorderLayout.CENTER);

  cp.add(jtf, BorderLayout.SOUTH);

  jfr.setSize(200, 200);

  jfr.setVisible(true);
    }
}
class KeyTextComponent extends Canvas {

    private ActionListener listnerList = null;

    public KeyTextComponent() {

  setBackground(Color.BLACK);

  KeyListener keyListener = new KeyAdapter() {

@Override

public void keyPressed(KeyEvent keyEvent) {

    if (listnerList != null) {

  int key = keyEvent.getKeyCode();

  String keystr = KeyEvent.getKeyText(key);

  ActionEvent aEvent = new ActionEvent(this,

    ActionEvent.ACTION_PERFORMED, keystr);

  listnerList.actionPerformed(aEvent);

    }

}

  };

  MouseListener mListner = new MouseAdapter() {

@Override

public void mousePressed(MouseEvent mEvent) {

    requestFocus();

}

  };

  addKeyListener(keyListener);

  addMouseListener(mListner);
    }

    public void addActionListener(ActionListener actionListn) {

  listnerList = AWTEventMulticaster.add(listnerList,

    actionListn);
    }

    public void removeActionListener(ActionListener actionListn) {

  listnerList = AWTEventMulticaster.remove(listnerList,

    actionListn);
    }

    @Override
    public boolean isFocusTraversable() {

  return true;
    }
}

 
This was an example on how to work with the KeyListener component.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button