0% found this document useful (0 votes)
60 views5 pages

Adv Java Lec-12 KeyEvent.b3fdcf8

The document discusses key events in Java. It describes the KeyListener interface which contains three methods - keyPressed, keyReleased, and keyTyped - that are called when a key event occurs. It explains the difference between a character typed and a virtual key code reported by key events. It provides examples to demonstrate listening for key events and drawing shapes based on different key presses.

Uploaded by

Neha Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Adv Java Lec-12 KeyEvent.b3fdcf8

The document discusses key events in Java. It describes the KeyListener interface which contains three methods - keyPressed, keyReleased, and keyTyped - that are called when a key event occurs. It explains the difference between a character typed and a virtual key code reported by key events. It provides examples to demonstrate listening for key events and drawing shapes based on different key presses.

Uploaded by

Neha Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Adv. Java Lec-12


Topic: KeyEvent
KeyEvent:
 KeyEvents are generated when the user presses or releases keys.

 The KeyListener interface contains three methods that are called when a
KeyEvent occurs.

 The adapter class for KeyListener is KeyAdapter.

 If you only care about one or two of the methods in KeyListener, you can
subclass the adapter and only override the methods that you are
interested in.

 For any component to listen for key events, it is necessary to call


addKeyListener() method.

Methods Of KeyListener:-

1) void keyPressed(KeyEvent e)
The keyPressed() method is called when a user presses a key.

2) void keyReleased(KeyEvent e)
The keyReleased() method is called when a user releases a key.

3) void keyTyped(KeyEvent e)
The keyTyped() method is called when a user types a key. The method
keyTyped() method reports the actual character typed. Action-oriented
keys, like function keys, do not trigger this method being called.

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Virtual Key Code :


 Java makes a distinction between characters and virtual key codes.

 Virtual key codes are indicated with a perfix of VK_, such as VK_A or
VK_SHIFT.
 Virtual key codes correspond to keys on the keyboard.

 For example, VK_A denotes the key marked A. There is no separate


lowercase virtual key code the keyboard does not have separate
lowercase keys.

 So, suppose that the user types an uppercase “A” in the usual way, by
pressing the SHIFT key along with the A key. Java reports five events in
response to this user action. Here are the actions and the associated
events.
Pressed the SHIFT key (keyPressed called for VK_SHIFT)
Pressed the A key (keyPressed called for VK_A)
Typed “A” (keyTyped called for an “A”)
Released the A key (keyReleased called for VK_A)
Released the SHIFT key (keyReleased called for VK_SHIFT)

 On the other hand, if the user typed a lowercase “a” by simply pressing
the A key, then there are only three events
Pressed the A key (keyPressed called for VK_A)
Typed “a” (keyTyped called for an “a”)
Released the A key (keyReleased called for VK_A)

 Thus, the keyTyped procedure reports the character that was typed
(“A” or “a”). whereas the keyPressed and keyReleased methods report
on the actual keys that the user pressed.

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Methods of KeyEvent class :


1) char getKeyChar()
returns the character that the user typed.

2) int getKeycode()
returns the virtual key code of this key event.

3) boolean isAltDown()
returns the true if Alt key is hold down, otherwise returns false.

4) boolean isControlDown()
returns the true if Control key is hold down, otherwise returns false.

5) boolean isShiftDown()
returns the true if Shift key is hold down, otherwise returns false.

1) Write a program to demonstrate KeyEvent


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

class KeyListenerFrame extends Frame


{
KeyListenerFrame()
{
setSize(600,500);
setTitle("Key Test");
addKeyListener( new KeyHandler());
}

class KeyHandler implements KeyListener


{
public void keyPressed(KeyEvent e)
{
System.out.println("Key Pressed");
}
public void keyTyped(KeyEvent e)
{
System.out.println("Key Typed");

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

}
public void keyReleased(KeyEvent e)
{
System.out.println("Key Released");
}
}
public static void main(String args[])
{
KeyListenerFrame f = new KeyListenerFrame();
f.setVisible(true);
}
} // end of frame class

2) Write a program to draw Line, Rectangle , Circle whenever L , R


or C key is pressed respectively.

import java.awt.*;
import java.awt.event.*;
class KeyDrawTest extends Frame
{
int code;
KeyDrawTest()
{
setSize(600,500);
setTitle("Key Test");
addKeyListener( new KeyHandler());
}
class KeyHandler extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
code=e.getKeyCode();
repaint();
}
}
public void paint(Graphics g)
{
if(code==KeyEvent.VK_R)
g.drawRect(50,50,300,200);
else

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

if(code==KeyEvent.VK_C)
g.drawOval(50,50,300,300);
else
if(code==KeyEvent.VK_L)
g.drawLine(50,50,300,200);
}

public static void main(String args[])


{
KeyDrawTest f = new KeyDrawTest();
f.setVisible(true);
}
} // end of frame class

Output:

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like