Sunday, 12 May 2019

Key Event Handling in java - online help

Key Event Handling in java:

Key events in java are generated when a key is pressed, the key is typed and a key is released. The key Listener interface is handling key events.

Each of the key event handling methods takes a keyEvent object is its arguments.

A key event object contains information about the key event. The various methods available in the keyListener interface are as follows:

void keyPressed(keyEvent e)

The keyPressed ()method is used to call when a key is pressed.

void keyTyped(keyEvent e)

The keyTyped ()method is used to call when a key is typed.

void KeyReleased(keyEvent e)

The keyReleased()method is used to call when typed or continuously pressed a key is released.

getKeyChat()

The getKeyChat()method is used to return the character of the key from which the event is generated.

Sample Program for key event handling in java:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="impkey" width=200 height=300></applet>*/
public class impkey extends Applet implements KeyListener
{
TextField t
TextArea ta;
pulic void init()
{
t=new TextField(20);
ta=new TextArea();
add(t);
add(ta);
t.addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("key pressed");
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("keyPressed");
}
public void keyReleased(keyEvent ke)
{
if(ke.getSource()==t)
ta.append("Key Released");
}
public void keyTyped(KeyEvent ke)
{
if(ke.getSource()==t)
{
char c=ke.getKeyChar();
ta.append("KeyTyped"+c);
}
}


No comments:

Post a Comment