Page 1 of 1

KeyListeners An introduction to KeyListener Rate Topic: ***** 1 Votes

#1 dragon-slayer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 06-May 07

Posted 08 June 2007 - 10:49 AM

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.

import java.awt.event.*;



To use KeyListener we must implement it like this:
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:
	 //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.

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
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
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.

Is This A Good Question/Topic? 3
  • +

Replies To: KeyListeners

#2 Guest_Ariunbold*


Reputation:

Posted 30 March 2010 - 03:56 AM

Thank you for the post! It helped me understand KeyListener. I have been Googling about KeyListener a lot and finally found this.
Was This Post Helpful? 1

Page 1 of 1