| | |
Measure or detect how long a button was help down
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator card chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited webservices windows






