Java Example Program / Sample Source Code
import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class AWTEventListenerExample implements AWTEventListener {
public AWTEventListenerExample() {
Frame frame = new Frame("AWTEventListenerExample");
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
TextField textField = new TextField("Type something here ");
frame.add(textField);
frame.pack();
frame.setSize(new Dimension(350, 250));
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
new AWTEventListenerExample();
}
@Override
public void eventDispatched(AWTEvent ae) {
if (ae instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent) ae;
if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
System.out.println("Key Event: " + ae);
}
}
}
} |
|
|