Advertisement



< Prev
Next >



KeyEvent and KeyListener





An event of type KeyEvent class is generated when a source such as, a key on the keyboard is pressed in a textfield or in a textarea.




Some methods of KeyEvent class


Method Description
public char getKeyChar()
Returns the character associated with the key pressed on the keyboard, which triggered the KeyEvent.

public int getKeyCode()
Returns an int key code associated with the key pressed on the keyboard.


public boolean isActionKey()
Returns true if key pressed was an "action" key, i.e. keys that don't generate a character, such as Cut, Copy, Paste, Page Up, Caps Lock, the arrow and function keys.






A class to listen and respond to a KeyEvent, must perform the next two steps -


Method Description
public void addKeyListener(KeyListener object)
where, object is an object of the class that has implemented KeyListener interface and wanted to register itself to listen and respond to ActionEvent generated when a key is pressed in this source.




Advertisement




Handling an KeyEvent by implementing KeyListener interface


In the upcoming code, we are going to create a class that will listen to an event of type KeyEvent, by implementing the KeyListener interface. In this code, the KeyEvent is generated when a key is pressed and released within a textfield.

import java.awt.*;
import java.awt.event.*;


public class KeyEx1 implements KeyListener
{
Label label1, label2;
TextField field1;
Frame jf;
String str;


KeyEx1()
{

jf = new Frame("Handling KeyEvent");

label1= new Label("Press any key on keyboad, to see the events it generates -", Label.CENTER);
label2= new Label();


field1 = new TextField(20); //calling TextField(String)

jf.setLayout(new FlowLayout());

jf.add(label1);
jf.add(field1);
jf.add(label2);


field1.addKeyListener(this); //As soon as button is clicked, data from all the textfields is read

jf.setSize(360,200);
jf.setVisible(true);
}


public void keyPressed(KeyEvent ke)
{
str= "KeyCode : " + ke.getKeyCode() + ",  -Key Pressed- ";
label2.setText(str);
jf.setVisible(true);
}

public void keyReleased(KeyEvent ke)
{
str+=" -Key Released- ";
label2.setText(str);
jf.setVisible(true);
str="";
}

public void keyTyped(KeyEvent ke)
{
str+=" -Key Typed- ";
label2.setText(str);
jf.setVisible(true);
}

public static void main(String... ar)
{
new KeyEx1();
}

}

When you run the code, you are presented a window shown in the window below -:

Figure 1


When you press a key on the keyboard, it triggers a KeyEvent, three methods defined in KeyEventEx1 are called in sequence - And you are presented a window showing the code of key a -

Figure 2


When you press SHIFT key on the keyboard, it triggers a KeyEvent, but this time two methods defined in KeyEventEx1 are called in sequence - Method keyTyped() is not called because SHIFT key doesn't generate or remove any character. You are presented a window showing the co de of key SHIFT -

Figure 3

When you press Backspace key on the keyboard, it triggers a KeyEvent, three methods defined in KeyEventEx1 are called in sequence - And you are presented a window showing the code of key Backspace-

Figure 4




Please share this article -





< Prev
Next >
< ActionListene
MouseListener >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement