Java Example Program/ Sample Source Code
import java.awt.*;
import java.awt.event.*;
public class GetExtendedKeyCodeForCharExample {
Label label;
public GetExtendedKeyCodeForCharExample() {
Frame frame = new Frame();
TextField textField = new TextField();
frame.add(textField, BorderLayout.NORTH);
label = new Label();
frame.add(label, BorderLayout.CENTER);
frame.setSize(450, 400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
textField.addKeyListener(new KeyAdapter() {
/**
* When you type the character "a" into the text field you will see
* an information dialog box
*/
public void keyTyped(KeyEvent ke) {
char keyChar = ke.getKeyChar();
if (keyChar == 'a') {
System.out.println("You typed 'a'");
}
}
/**
* When you type the character "b" into the text field you will see
* an information dialog box
*/
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if (keyCode == 66) {
System.out.println("You Typed b");
}
}
/**
* When you type the character "c" into the text field you will see
* an information dialog box
*/
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if (keyCode == 67) {
System.out.println("You Typed c");
}
}
});
}
public static void main(String[] args) {
new GetExtendedKeyCodeForCharExample();
}
} |
|
|