Measure or detect how long a button was help down

Reply

Join Date: Feb 2008
Posts: 36
Reputation: esy928 is an unknown quantity at this point 
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

Measure or detect how long a button was help down

 
0
  #1
Aug 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

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

 
0
  #2
Aug 23rd, 2008
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 36
Reputation: esy928 is an unknown quantity at this point 
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

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

 
0
  #3
Aug 23rd, 2008
hmm.. ive tried using them but can they measure how long you held a button down?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

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

 
0
  #4
Aug 23rd, 2008
I'm not sure how to do that. Timing how long between a keyPressed and a keyReleased might work. Maybe someone else will know.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

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

 
0
  #5
Aug 24th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

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

 
3
  #6
Aug 24th, 2008
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--

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 36
Reputation: esy928 is an unknown quantity at this point 
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

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

 
0
  #7
Aug 24th, 2008
cool!!! thanks edwards!! =)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC