Hello,This tutorial is generally aimed at beginners to Java.If you read this I hope you will leave with the ability of being able to implement key listeners to your apps.
Lets get started first me must import the java.awt.event.*; package as that is where the key Listener is located.
CODE
import java.awt.event.*;
To use KeyListener we must implement it like this:
CODE
import java.awt.event.*;
public class Key1 implements KeyListener{
public static void main(String[] args){
}
}
}
If you try compiling this you should get an error , This is because we have to overide the abstract methods in the java.awt.even.KeyListener package.And this is how we do it.
We add this to our code:
CODE
//Called when the key is pressed down.
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed!!!");
}
//Called when the key is released
public void keyReleased(KeyEvent e){
System.out.println("Key Released!!!");
}
//Called when a key is typed
public void keyTyped(KeyEvent e){
}
it will compile know but still you should find that when you type nothing will happen,This is because we have to add an action listener in this example we shall add it to a JTextField.The Application will have a function where if you press a key it will display the keycode in a text box.A keycode is a integer that repesents a key.
CODE
import java.awt.event.*;
import javax.swing.*;
public class Key1 extends JFrame implements KeyListener{
JTextField KeyCodeT = new JTextField("Key Code:");//A Text Field that will display the key code.
public Key1(){
KeyCodeT.addKeyListener(this);//Listens for key inputs in the text field
KeyCodeT.setEditable(false);//disallow user input into the Text field.
add(KeyCodeT);//add the text field to the screen
setSize(300,300);//set the screen ssize
setVisible(true);//show the window on screen.
}
//Called when the key is pressed down.
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed!!!");
}
//Called when the key is released
public void keyReleased(KeyEvent e){
System.out.println("Key Released!!!");
KeyCodeT.setText("Key Code:" + e.getKeyCode());//displays the key code in the text box
}
//Called when a key is typed
public void keyTyped(KeyEvent e){
}
public static void main(String[] args){
Key1 key = new Key1();
}
}
Thats all fine but what if I want it so when I press Esc the app will close.Well thats easy if we open our current application and press Esc we can find that the key code for ESC is 27.
So we can add
CODE
if(e.getKeyCode()==27) {//check if the Keycode is 27 which is esc
JOptionPane.showMessageDialog(null,"Good Bye");//display a good bye messege
System.exit(0);//exit
}
To the keyPressed void.
So the final code becomes
CODE
import java.awt.event.*;
import javax.swing.*;
public class Key1 extends JFrame implements KeyListener{
JTextField KeyCodeT = new JTextField("Key Code:");//A Text Field that will display the key code.
public Key1(){
KeyCodeT.addKeyListener(this);//Listens for key inputs in the text field
KeyCodeT.setEditable(false);//disallow user input into the Text field.
add(KeyCodeT);//add the text field to the screen
setSize(300,300);//set the screen ssize
setVisible(true);//show the window on screen.
}
//Called when the key is pressed down.
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed!!!");
if(e.getKeyCode()==27) {//check if the Keycode is 27 which is esc
JOptionPane.showMessageDialog(null,"Good Bye");//display a good bye messege
System.exit(0);//exit
}
}
//Called when the key is released
public void keyReleased(KeyEvent e){
System.out.println("Key Released!!!");
KeyCodeT.setText("Key Code:" + e.getKeyCode());//displays the key code in the text box
}
//Called when a key is typed
public void keyTyped(KeyEvent e){
}
public static void main(String[] args){
Key1 key = new Key1();
}
}
Try changing the key code to something else.