Adv Java Lec-12 KeyEvent.b3fdcf8
Adv Java Lec-12 KeyEvent.b3fdcf8
The KeyListener interface contains three methods that are called when a
KeyEvent occurs.
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.
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 codes are indicated with a perfix of VK_, such as VK_A or
VK_SHIFT.
Virtual key codes correspond to keys on the keyboard.
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
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.
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
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);
}
Output:
Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260