Copyright (C) 2001, Geert Vernaeve. All Rights Reserved.
package test; import java.awt.*; import java.awt.event.*;
Conclusion: Lightweight components don't receive key events. Under JDK1.2, heavyweight components receive key events if the mouse is over them Under Kaffe, nobody receives key events! (except the Frame? Check!) ** SOLUTION: use requestFocus() ! Simple but you have to know it! :-)))
public class AwtKeyTest extends Panel implements KeyListener { public AwtKeyTest() { setLayout(new FlowLayout()); KeyTestView ktv; add(ktv = new KeyTestView()); ktv.setBackground(Color.black); add(ktv = new KeyTestView()); ktv.setBackground(Color.white); addKeyListener(this); // enableEvents(AWTEvent.xxx_EVENT_MASK); } public static void main(String [] args) { Frame frame = new Frame("test"); frame.add("Center",new AwtKeyTest()); frame.pack(); frame.show(); } public void keyPressed(KeyEvent evt) {} public void keyReleased(KeyEvent evt) {} public void keyTyped(KeyEvent evt) { System.out.println("container: typed "+evt.getKeyChar()); } } class KeyTestView extends Component implements KeyListener,MouseListener { public KeyTestView() { addKeyListener(this); addMouseListener(this); //enableEvents(AWTEvent.KEY_EVENT_MASK); } public Dimension getPreferredSize() { return new Dimension(100,100); } public void paint(Graphics g) { g.setColor(getBackground()); g.fillRect(0,0,getSize().width,getSize().height); } public void processKeyEvent(KeyEvent evt) { System.out.println("Key event "+evt); super.processKeyEvent(evt); } public void keyPressed(KeyEvent evt) {} public void keyReleased(KeyEvent evt) {} public void keyTyped(KeyEvent evt) { System.out.println("component "+this+": typed "+evt.getKeyChar()); } public void mouseClicked(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseExited(MouseEvent evt) {} public void mousePressed(MouseEvent evt) {} public void mouseReleased(MouseEvent evt) { requestFocus(); System.out.println("component "+this+": requestFocus()"); } }