• Post Reply Bookmark Topic Watch Topic
  • New Topic

capture the time differeence of keyevents  RSS feed

 
sondus darek
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi THERE
I wrote a small program to capture the keystroke timing event which are hold time: time from pressing a key and release it
flight time: time from pressing one key and pressing the next key.. and the total time to type a string ...I've tried to make it simple but there is some sort of inconveniences where it it is giving me the time to type the whole string is smaller than some other and i got an execption i don't how to deal with that
here is the code






import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
public class TKEY extends JFrame implements KeyListener,ActionListener {
private JTextField t=new JTextField(10);
private JTextField tt=new JTextField(10);
private JTextField ttt=new JTextField(10);
private JTextField tttt=new JTextField(10);
private JLabel l1=new JLabel("enter the key");
private JLabel l2=new JLabel("Keypressed");
private JLabel l3=new JLabel("Keyreleased");
private JLabel l4=new JLabel("Keytyped");
private JPanel p=new JPanel(new FlowLayout());
private JButton n=new JButton("click");
int i[]=new int[10];
long tstart;
long start = 0;
long end = 0;
long delay = 0;
int length=3;
long a[]=new long[(2*length)-1];
// int kr,v;
//i//nt kt;
//Calendar c;


public TKEY(){
super("wlcome");
this.setSize(200,300);
p.add(l1);
p.add(t);
p.add(l2);
p.add(tt);
p.add(l3);
p.add(ttt);
p.add(l4);
p.add(tttt);
p.add(n);

this.setContentPane(p);
this.setVisible(true);
t.addKeyListener(this);
n.addActionListener(this);



}


/**
* @param args
*/
public static void main(String[] args) {
new TKEY();
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent arg0) {
// c=Calendar.getInstance();
//kp=c.get(Calendar.MILLISECOND);
//tt.setText(""+kp);
// TODO Auto-generated method stub
if(t.getText().length()==1)
tstart = System.currentTimeMillis();
delay = start;
start = System.currentTimeMillis();



}

@Override
public void keyReleased(KeyEvent arg0) {
//c=Calendar.getInstance();
// kr=c.get(Calendar.MILLISECOND);
//ttt.setText(""+kr);
// TODO Auto-generated method stub
end = System.currentTimeMillis();
int pos=(t.getText().length() - 1) * 2;
if(t.getText().length() <= length) {
a[pos] = end - start;
if (pos != 0) {
a[pos - 1] = start - delay;

}


}
if (t.getText().length() == length){
// button.setEnabled(true);
long tend = System.currentTimeMillis();
a[length * 2-1] = tend-tstart;
}




}

// @Override
/* public void keyTyped(KeyEvent arg0) {
c=Calendar.getInstance();
kt=c.get(Calendar.MILLISECOND);
tttt.setText(""+kt);
// TODO Auto-generated method stub

}*/


@Override
public void actionPerformed(ActionEvent arg0) {
for(int i=0;i<a.length;i++)
//String t=" "+a[i]+"\t";
//JOptionPane.showMessageDialog(null, "difference:"+a[i]);
System.out.println(a[i]+"\n");
// TODO Auto-generated method stub

}


@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}




and the exception i got is
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5
at TKEY.keyReleased(TKEY.java:96)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/awt-swing/59592-keystroke-event-program-help.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!