File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Key Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Key Events" Watch "Key Events" New topic
Author

Key Events

Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
Hi all..
pls take a look at the following piece of code..
import java.awt.*;
import java.awt.event.*;
public class KeyTest implements KeyListener{
Frame f;
String s;
TextField tf;

public KeyTest()
{
f=new Frame("Key Test");
f.setLayout(new FlowLayout());
tf=new TextField();
tf.addKeyListener(this);
f.add(tf);
f.setSize(300,300);
f.setVisible(true);
}

public static void main(String args[])
{
KeyTest kt=new KeyTest();
}
public void keyTyped(KeyEvent e)
{
s=tf.getText();
System.out.println("KeyTyped String : " + s + " and its length is " + s.length());
}

public void keyPressed(KeyEvent e)
{
s=tf.getText();
System.out.println(" KeyPressed String : " + s + " and its length is " + s.length());
}

public void keyReleased(KeyEvent e)
{
s=tf.getText();
System.out.println(" KeyReleased String : " + s + " and its length is " + s.length());
}
}

Now .. if i successively type characters in the text field(say H, I, M, A) , the output is..
After Pressing H
KeyPressed String : and its length is 0
KeyTyped String : and its length is 0
KeyReleased String : r and its length is 1
After Pressing I
KeyPressed String : H and its length is 1
KeyTyped String : H and its length is 1
KeyReleased String : HI and its length is 2
After Pressing M
KeyPressed String : HI and its length is 2
KeyTyped String : HI and its length is 2
KeyReleased String : HIM and its length is 3
After Pressing A
KeyPressed String : HIM and its length is 3
KeyTyped String : HIM and its length is 3
KeyReleased String : HIMA and its length is 4
Now, my ques is :
When does the KeyTyped Event get fired? Is it when the key is aboutt to be released, just after KeyPressed and just before KeyReleased? and why does KeyTyped does not take into account the most recent key typed?
Pls let me know the answer.. soon.. :-)
------------------
Hima


Hima<BR>Sun Certified Java Programmer
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Hima,
The The Java Class Libraries Second Edition, Volume 2 gives the following info about KEY_TYPED:

KEY_TYPED events are delivered after KEY_PRESSED events but before KEY_RELEASED events ... and its key code property is always VK_UNDEFINED. The event's key modifiers are captured a the time the character was pressed.

And for VK_UNDEFINED:

Constant specifying an undefined key code. When the key code in a key evernt does not represent a key code, the event's key code property is set to VK_UNDEFINED. The key code in all KEY_TYPED events is VK_UNDEFINED.


Hope that helps.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi Jane..
thanx a lot for your precise and clear reply..

------------------
Hima
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Key Events
 
Similar Threads
removing listeners
using Applet to speed up hiding of elements
Issues when popup a dialog from keyReleased event
JTextField getText() no Enter pressed
Unable to add Labels with Image to Panel thru KeyListener.