time - How to know how many milliseconds Key is pressed in java -


i want find out time in milliseconds key pressed in java, result prints different number though pressed key less 1 second or more. example if pressed less 1 seconds shows 30, 45 or 98 , if pressed 3 seconds shows 35 , 50 , 120 tried code

long keypressedmillis; long keypresslength; 

.

public void keypressed(keyevent arg0) {     // todo auto-generated method stub     int codigo = arg0.getkeycode();     if(codigo == keyevent.vk_space)     {            keypressedmillis = system.currenttimemillis();                } } 

.

public void keyreleased(keyevent arg0) {     // todo auto-generated method stub     int codigo = arg0.getkeycode();     if(codigo == keyevent.vk_space)     {            keypresslength = system.currenttimemillis() -keypressedmillis;                       system.out.println(keypresslength);               } } 

keypressed can (and be) called repeatedly while key pushed. should put in flag can check see if repeated key event...

int lastkey = -1;  public void keypressed(keyevent arg0) {     // todo auto-generated method stub     int codigo = arg0.getkeycode();     if (codigo != lastkey) {         lastkey = codigo         if(codigo == keyevent.vk_space)         {            keypressedmillis = system.currenttimemillis();                    }     } } 

of course, in keyreleased event handler, should reset value don't end missing new key press ;)


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -