943,633 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 795
  • Java RSS
Aug 23rd, 2008
0

Measure or detect how long a button was help down

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008
Aug 23rd, 2008
0

Re: Measure or detect how long a button was help down

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.
Last edited by jasimp; Aug 23rd, 2008 at 12:13 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Aug 23rd, 2008
0

Re: Measure or detect how long a button was help down

hmm.. ive tried using them but can they measure how long you held a button down?
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008
Aug 23rd, 2008
0

Re: Measure or detect how long a button was help down

I'm not sure how to do that. Timing how long between a keyPressed and a keyReleased might work. Maybe someone else will know.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Aug 24th, 2008
0

Re: Measure or detect how long a button was help down

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?
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 24th, 2008
3

Re: Measure or detect how long a button was help down

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--

java Syntax (Toggle Plain Text)
  1. public class ImprovedClock{
  2.  
  3. private volatile long first = 0, second = 0;
  4. private volatile boolean startPressed = false, stopPressed = false;
  5.  
  6. public void start(){
  7. if(!startPressed){
  8. synchronized(this){
  9. first = System.currentTimeMillis();
  10. startPressed = true;
  11. stopPressed = false;
  12. }
  13. }
  14. }
  15.  
  16. public boolean hasStarted(){
  17. return startPressed;
  18. }
  19.  
  20. public void stop(){
  21. if(!stopPressed){
  22. synchronized(this){
  23. second = System.currentTimeMillis();
  24. startPressed = false;
  25. stopPressed = true;
  26. }
  27. }
  28. }
  29.  
  30. public boolean hasStopped(){
  31. return stopPressed;
  32. }
  33.  
  34. public long getElapsedTime(){
  35. long time = 0;
  36. synchronized(this){
  37. time = second - first;
  38. first = 0;
  39. second = 0;
  40. }
  41. return time;
  42. }
  43.  
  44. public double getSeconds(){
  45. return ((double)getElapsedTime()/1000);
  46. }
  47. }

java Syntax (Toggle Plain Text)
  1. import javax.swing.JFrame;
  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.KeyAdapter;
  4.  
  5. public class TimeMonitorTest extends JFrame{
  6.  
  7.  
  8. public TimeMonitorTest(){
  9. final ImprovedClock ic = new ImprovedClock();
  10. addKeyListener(
  11. new KeyAdapter(){
  12. @Override public void keyPressed(KeyEvent ke){
  13. if(!ic.hasStarted()){
  14. ic.start();
  15. System.out.println("Clock started!");
  16. }
  17. }
  18.  
  19. @Override public void keyReleased(KeyEvent ke){
  20. ic.stop();
  21. System.out.println("Time elapsed: " + ic.getSeconds() + " seconds!");
  22. }
  23. }
  24. );
  25. setSize(300, 300);
  26. setLocation(500, 500);
  27. setVisible(true);
  28. }
  29.  
  30. public static void main(String... args){
  31. TimeMonitorTest tmt = new TimeMonitorTest();
  32. }
  33. }

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 24th, 2008
0

Re: Measure or detect how long a button was help down

cool!!! thanks edwards!! =)
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: c-quiz application
Next Thread in Java Forum Timeline: a sample on what you can do with paint and draw





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC