In this section, you will learn about the handling key press event in java. Key Press is the event is generated when you press any key to the specific component. This event is performed by the KeyListener. When you press or release keys for the writing purpose then key events are fired by the KeyListener objects which generates the KeyEvent environment for the component.
In this program, you will see how operations are performed when you press the key to the specific component. Here, the KeyPress is a constructor of the main class. This constructor creates a frame and set text field to the panel. Then the panel and the label have been set to the frame in the program. When you enter the character in the text field through the keyboard then your entered data will be displayed in the label. There are various method have been used to do the required are given :
This is the method of the KeyEvent class which determines the character that has been entered by you. This method returns the character associated the KeyEvent.
This is the class is used to receive the keyboard events. It creates the keyListener objects using the addKeyListener() method. The generated event is passed to every KeyListener objects that receives such types of events using the addKeyListener() method of the object.
This method has been used in the program which receives the generated event when you press any key to the object. Above method also sets the text of the source of the event to the label.
Here is the code of program:
Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles