The class which implements KeyListener
interface becomes our custom key event listener. This listener can not directly listen the key events. It can only listen the key events through intermediate objects such as JFrame
. So
Make one Key listener class as
class MyListener implements KeyListener{
// override all the methods of KeyListener interface.
}
Now our class MyKeyListener
is ready to listen the key events. But it can not directly do so.
Create any object like JFrame
object through which MyListener
can listen the key events. for that you need to add MyListener
object to the JFrame
object.
JFrame f=new JFrame();
f.addKeyListener(new MyKeyListener);