java.awt.event

Interface KeyListener

All Superinterfaces:
EventListener
Known Implementing Classes:
AWTEventMulticaster, BasicComboBoxUI.KeyHandler, BasicComboPopup.InvocationKeyHandler, BasicTableUI.KeyHandler, BasicTreeUI.KeyHandler, JMeterTreeListener, KeyAdapter, MotifComboBoxUI.MotifComboPopup.InvocationKeyHandler, WindowsComboBoxUI.WindowsComboPopup.InvocationKeyHandler

public interface KeyListener
extends EventListener

The listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

The listener object created from that class is then registered with a component using the component's addKeyListener method. A keyboard event is generated when a key is pressed, released, or typed. The relevant method in the listener object is then invoked, and the KeyEvent is passed to it.

Since:
1.1
See Also:
KeyAdapter, KeyEvent, Tutorial: Writing a Key Listener, Reference: The Java Class Libraries (update file)

Method Summary

void
keyPressed(KeyEvent e)
Invoked when a key has been pressed.
void
keyReleased(KeyEvent e)
Invoked when a key has been released.
void
keyTyped(KeyEvent e)
Invoked when a key has been typed.

Method Details

keyPressed

public void keyPressed(KeyEvent e)
Invoked when a key has been pressed. See the class description for KeyEvent for a definition of a key pressed event.
Usages and Demos :

View More Examples of keyPressed(KeyEvent e)
   1: import java.awt.event.KeyEvent;
   2: import java.awt.event.KeyListener;
   3: import java.awt.event.MouseEvent;
   4:         ...
   5: public class SwingTerminal extends Component
   6:         implements VDUDisplay, KeyListener, MouseListener, MouseMotionListener, VDUBufferListener {
   7: 
   8:         ...
   9:     if (input != null)
  10:       input.keyPressed(e.getKeyCode(), e.getKeyChar(), getModifiers(e));
  11:   }
  12:         ...
  13: 
  14:   private KeyListener keyListener;

View Full Code Here
   1: import java.awt.event.KeyEvent;
   2: import java.awt.event.KeyListener;
   3: import java.awt.event.WindowEvent;
   4:         ...
   5:                 for (Iterator i = easyKeyListeners.iterator(); i.hasNext();) {
   6:                     KeyListener l = (KeyListener) i.next();
   7:                     l.keyTyped(e);
   8:         ...
   9:                     KeyListener l = (KeyListener) i.next();
  10:                     l.keyPressed(e);
  11:                 }

View Full Code Here
   1:   private boolean transientSwitch;
   2:   private KeyListener keyEventInterceptor;
   3:   private InputHandler inputHandler;
   4:         ...
   5: 
   6:   public final KeyListener getKeyEventInterceptor()
   7:   {
   8:         ...
   9: 
  10:   public void setKeyEventInterceptor(KeyListener listener)
  11:   {
  12:         ...
  13:         if (keyEventInterceptor != null)
  14:           keyEventInterceptor.keyPressed(evt);

View Full Code Here
   1:     InputHandler inputHandler = view.getInputHandler();
   2:     KeyListener keyEventInterceptor = view.getKeyEventInterceptor();
   3:     switch (evt.getID())
   4:         ...
   5:         if (keyEventInterceptor != null)
   6:           keyEventInterceptor.keyPressed(evt);
   7:         else
   8:         ...
   9:           inputHandler.keyPressed(evt);
  10:         break;

View Full Code Here

keyReleased

public void keyReleased(KeyEvent e)
Invoked when a key has been released. See the class description for KeyEvent for a definition of a key released event.
Usages and Demos :

View More Examples of keyReleased(KeyEvent e)
   1: import java.awt.event.KeyEvent;
   2: import java.awt.event.KeyListener;
   3: import java.awt.event.MouseEvent;
   4:         ...
   5: public class SwingTerminal extends Component
   6:         implements VDUDisplay, KeyListener, MouseListener, MouseMotionListener, VDUBufferListener {
   7: 
   8:         ...
   9: 
  10:   private KeyListener keyListener;
  11: 
  12:         ...
  13:         case KeyEvent.KEY_RELEASED:
  14:           keyListener.keyReleased(evt);

View Full Code Here
   1: import java.awt.event.KeyEvent;
   2: import java.awt.event.KeyListener;
   3: import java.awt.event.WindowEvent;
   4:         ...
   5:                 for (Iterator i = easyKeyListeners.iterator(); i.hasNext();) {
   6:                     KeyListener l = (KeyListener) i.next();
   7:                     l.keyTyped(e);
   8:         ...
   9:                     KeyListener l = (KeyListener) i.next();
  10:                     l.keyReleased(e);
  11:                 }

View Full Code Here
   1:   private boolean transientSwitch;
   2:   private KeyListener keyEventInterceptor;
   3:   private InputHandler inputHandler;
   4:         ...
   5: 
   6:   public final KeyListener getKeyEventInterceptor()
   7:   {
   8:         ...
   9: 
  10:   public void setKeyEventInterceptor(KeyListener listener)
  11:   {
  12:         ...
  13:         if (keyEventInterceptor != null)
  14:           keyEventInterceptor.keyReleased(evt);

View Full Code Here
   1:     InputHandler inputHandler = view.getInputHandler();
   2:     KeyListener keyEventInterceptor = view.getKeyEventInterceptor();
   3:     switch (evt.getID())
   4:         ...
   5:         if (keyEventInterceptor != null)
   6:           keyEventInterceptor.keyReleased(evt);
   7:         else
   8:         ...
   9:           inputHandler.keyReleased(evt);
  10:         break;

View Full Code Here

keyTyped

public void keyTyped(KeyEvent e)
Invoked when a key has been typed. See the class description for KeyEvent for a definition of a key typed event.
Usages and Demos :

View More Examples of keyTyped(KeyEvent e)
   1: import java.awt.event.KeyEvent;
   2: import java.awt.event.KeyListener;
   3: import java.awt.event.MouseEvent;
   4:         ...
   5: public class SwingTerminal extends Component
   6:         implements VDUDisplay, KeyListener, MouseListener, MouseMotionListener, VDUBufferListener {
   7: 
   8:         ...
   9:     if (input != null)
  10:       input.keyTyped(e.getKeyCode(), e.getKeyChar(), getModifiers(e));
  11:   }
  12:         ...
  13: 
  14:   private KeyListener keyListener;

View Full Code Here
   1: import java.awt.event.KeyEvent;
   2: import java.awt.event.KeyListener;
   3: import java.awt.event.WindowEvent;
   4:         ...
   5:                 for (Iterator i = easyKeyListeners.iterator(); i.hasNext();) {
   6:                     KeyListener l = (KeyListener) i.next();
   7:         ...
   8:                     l.keyTyped(e);
   9:                 }

View Full Code Here
   1:   private boolean transientSwitch;
   2:   private KeyListener keyEventInterceptor;
   3:   private InputHandler inputHandler;
   4:         ...
   5: 
   6:   public final KeyListener getKeyEventInterceptor()
   7:   {
   8:         ...
   9: 
  10:   public void setKeyEventInterceptor(KeyListener listener)
  11:   {
  12:         ...
  13:         if (keyEventInterceptor != null)
  14:           keyEventInterceptor.keyTyped(evt);

View Full Code Here
   1:     InputHandler inputHandler = view.getInputHandler();
   2:     KeyListener keyEventInterceptor = view.getKeyEventInterceptor();
   3:     switch (evt.getID())
   4:         ...
   5:         if (keyEventInterceptor != null)
   6:           keyEventInterceptor.keyTyped(evt);
   7:         else
   8:         ...
   9:           inputHandler.keyTyped(evt);
  10:         break;

View Full Code Here