| | |
Measure or detect how long a button was help down
![]() |
•
•
Join Date: Feb 2008
Posts: 36
Reputation:
Solved Threads: 0
hello!
is there a method or class that lets you assign events to the keys of your keyboard (spacebar to fire gun)? and is there a way to measure how long the key was held down? thanks!!! any help is greatly appreciated.. ive tried google-ing it but i had no luck in looking for a class/method that does those things =) tnx
is there a method or class that lets you assign events to the keys of your keyboard (spacebar to fire gun)? and is there a way to measure how long the key was held down? thanks!!! any help is greatly appreciated.. ive tried google-ing it but i had no luck in looking for a class/method that does those things =) tnx
KeyListener
Using keyListener
You need to implement it, and then add a listener with addKeyListener(this) and you need to use all of the methods provided because it is a interface (they are listed in the link I gave you.) The second link should help you learn how to use it. Using Google you could find the key codes for each of the keys on the keyboard.
Using keyListener
You need to implement it, and then add a listener with addKeyListener(this) and you need to use all of the methods provided because it is a interface (they are listed in the link I gave you.) The second link should help you learn how to use it. Using Google you could find the key codes for each of the keys on the keyboard.
Last edited by jasimp; Aug 23rd, 2008 at 12:13 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Why not store the current time in a data type (like a long) when the key is pressed and when the key is released subtract the new time from the old time?
Here's an example--
To make this work simply focus the pane (by clicking it) and key events will be listened for. Hold down a key then when you release it the seconds the key was held down will be displayed.
Here's an example--
java Syntax (Toggle Plain Text)
public class ImprovedClock{ private volatile long first = 0, second = 0; private volatile boolean startPressed = false, stopPressed = false; public void start(){ if(!startPressed){ synchronized(this){ first = System.currentTimeMillis(); startPressed = true; stopPressed = false; } } } public boolean hasStarted(){ return startPressed; } public void stop(){ if(!stopPressed){ synchronized(this){ second = System.currentTimeMillis(); startPressed = false; stopPressed = true; } } } public boolean hasStopped(){ return stopPressed; } public long getElapsedTime(){ long time = 0; synchronized(this){ time = second - first; first = 0; second = 0; } return time; } public double getSeconds(){ return ((double)getElapsedTime()/1000); } }
java Syntax (Toggle Plain Text)
import javax.swing.JFrame; import java.awt.event.KeyEvent; import java.awt.event.KeyAdapter; public class TimeMonitorTest extends JFrame{ public TimeMonitorTest(){ final ImprovedClock ic = new ImprovedClock(); addKeyListener( new KeyAdapter(){ @Override public void keyPressed(KeyEvent ke){ if(!ic.hasStarted()){ ic.start(); System.out.println("Clock started!"); } } @Override public void keyReleased(KeyEvent ke){ ic.stop(); System.out.println("Time elapsed: " + ic.getSeconds() + " seconds!"); } } ); setSize(300, 300); setLocation(500, 500); setVisible(true); } public static void main(String... args){ TimeMonitorTest tmt = new TimeMonitorTest(); } }
To make this work simply focus the pane (by clicking it) and key events will be listened for. Hold down a key then when you release it the seconds the key was held down will be displayed.
Last edited by Alex Edwards; Aug 24th, 2008 at 12:54 am.
![]() |
Other Threads in the Java Forum
- Previous Thread: c-quiz application
- Next Thread: a sample on what you can do with paint and draw
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows






