This is a Java program to handle KeyBoardEvent.
We have to write a program in Java such that it demonstrates the event actions associated with the keyboard. The program should demonstrate various keyboard events such as key typed event, key pressed event and key released event by using alphabets, digits and non aplha numeric keys.
For handling KeyboardEvent, we can have the following 5 different sets of input and output.
1. To test keyTyped: When a key representing an alphabet is stroked on the keyboard and it is the first entry of input field.
For example:
If the key 'h' is the stroked/typed key on the keyboard then the expected output is "Key Typed : h"
2. To test keyTyped: When a key representing an alphabet is stroked on the keyboard after multiple entries in input field.
For example:
If the key 'o' is the last stroked/typed key on the keyboard then the expected output is "Key Typed : o"
3. To test keyTyped: When a key representing a digit is stroked on the keyboard after multiple entries in input field.
For example:
If the key '1' is the last stroked/typed key on the keyboard then the expected output is "Key Typed : 1"
4. To test keyTyped: When a key representing a digit is stroked on the keyboard after multiple entries in input field.
For example:
If the key '7' is the last stroked/typed key on the keyboard then the expected output is "Key Typed : 7"
5. To test keyPressed: When a non aplha numeric key is kept pressed on the keyboard.
For example:
If the key 'alt' is the kept pressed on the keyboard then the expected output is "Key Pressed : 18" Note: 18 is the unicode of the alt key.
6. To test keyReleased: When a non aplha numeric key is pressed and released on the keyboard.
For example:
If the key 'alt' is pressed and released on the keyboard then the expected output is "Key Released : 18" Note: 18 is the unicode of the alt key.
1. The program uses the interfaces KeyListener and ActionListener of the java.awt package.
a) The KeyListener interface has three member methods :
i) public void keyReleased(KeyEvent): This method gives the Unicode of the key released and its character equivalent if the key pressed is a letter.
ii) public void keyPressed(KeyEvent): This method gives the Unicode of the key pressed and its character equivalent if the key pressed is a letter.
iii) public void keyTyped(KeyEvent): This method gives the character equivalent of the key pressed provided it is a valid character.
b) The ActionListener interface has member method :
public void actionPerformed(ActionEvent): This method works on the click of the exit button and the functions closes the frame.
2. @Override is a keyword used to override any method on the parent class. When the sub class has any method defined in parent/super class with same name and parameters, the Override keyword is used. This overrides the function of super class and executed the function defined in the sub class.
3. Create a class that implements the two required interfaces – KeyListener and ActionListener.
4. Create a frame, two text fields for input & output and an exit button with required dimensions. After positioning the co-ordinates of the text fields and button, add them to the frame.
5. Associate KeyListener with the input text field and ActionListener with the exit button.
6. Display the frame.
7. The functions getKeyCode() and getKeyChar() give the unicode and character representation of the key pressed respectively. Use them to display the necessary message in the output text box.
Here is source code of the Java Program to handle the keyboard events. The program is successfully compiled and tested using BlueJ on Windows 10 and javac compiler on Fedora 30.
/* Java Program to demonstrate the event actions associated with a keyboard */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Keyboard_Event implements KeyListener,ActionListener
{
static JFrame frame;
static JTextField output;
static JTextField input;
//Driver function
public static void main(String args[])
{
//Create a frame
frame=new JFrame("Keyboard Event");
frame.setBackground(Color.white);
frame.setSize(500,500);
frame.setLayout(null);
//Create a text field for output
output=new JTextField();
output.setBounds(0,0,500,50);
frame.add(output);
//Create a text field for input
input=new JTextField();
input.setBounds(0,400,500,50);
frame.add(input);
//Create an exit button
JButton exit=new JButton("Exit");
exit.setBounds(220,200,60,30);
frame.add(exit);
//Create an object of the class
Keyboard_Event obj=new Keyboard_Event();
//Associate KeyListener with input
input.addKeyListener(obj);
//Associate ActionListener with exit
exit.addActionListener(obj);
frame.setVisible(true);
}
//function to dispose the frame when exit button is clicked
@Override
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
/*function to display the unicode of key released
and the character if it is a letter or a digit*/
@Override
public void keyReleased(KeyEvent e)
{
output.setText("");
output.setText("Key Released : "+e.getKeyCode());
if(Character.isLetter(e.getKeyChar()))
keyTyped(e);
if(Character.isDigit(e.getKeyChar()))
keyTyped(e);
}
/*function to display the unicode of key pressed
and the character if it is a letter or a digit*/
@Override
public void keyPressed(KeyEvent e)
{
output.setText("");
output.setText("Key Pressed : "+e.getKeyCode());
if(Character.isLetter(e.getKeyChar()))
keyTyped(e);
if(Character.isDigit(e.getKeyChar()))
keyTyped(e);
}
//function to display the character of the key typed
@Override
public void keyTyped(KeyEvent e)
{
output.setText("");
output.setText("Key Typed : "+e.getKeyChar());
}
}
1. Input and Output are the two text fields for input of string and output of the key action respectively.
2. The program demonstrates the functions of the KeyListener interface.
a) When the key is released, the keyReleased function is called which displays the unicode of the key and if the key is a valid letter then calls the method keyTyped.
b) When the key is pressed, the keyPressed function is called which displays the unicode of the key and if the key is a valid letter then calls the method keyTyped.
c) When the key is typed, the keyTyped function is called which displays the character representation of the key pressed.
3. The @Override keyword overrides the functions of the parent class and executes the functions of sub class.
Here’s the run time test cases for keyboard events.
Test case 1 – Here’s the runtime output of the key typed operation with key “h”, where “h” is first entry of the input field.
Test case 2 – Here’s the runtime output of the multiple entries in key typed operation by using alphabets in the input field. For example, here the entered key is “hello”, where “o” is the last entry of the input field. The program displays that the key typed is o.
Test case 3 – Here’s the runtime output of the multiple entries in key typed operation by using alphabets and digits in the input field. For example, here the entered key is “hello 1”, where “1” is the last entry of the input field. The program displays that the key typed is 1.
Test case 4 – Here’s the runtime output of the multiple entries in key typed sequence operation by using alphabets and digits in the input field. For example, here the entered key is “hello 187”, where “7” is the last entry of the input field. The program displays that the key typed is 7.
Test case 5 – Here’s the runtime output of the special key pressed operation with key “alt”. After pressing alt key, it displays the unicode of the alt key which is 18.
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Apply for Java Internship
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Programming Books