Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Key Events

 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jane..
thanx a lot for your precise and clear reply..

------------------
Hima
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic