key stroke time limit

hi experts,

I need a bit of code for an if statement that waits for a key press for 5 seconds.. and if a key is not pressed in that time it does something

thanks
SamooramadAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
gui or non-gui?

if its a gui then use a thread to include a delay before adding your keylistener

new Thread(new Runnable()
{
   public void run()
   {
      try
      {
         Thread.sleep(5000);
      }
      catch (Exception ex)
      {}
      comp.addKeyListener(listener);
   }
});
0
 
SamooramadAuthor Commented:
>> gui or non-gui?

whats the difference?
0
 
SamooramadAuthor Commented:
i do use a key listner if thats what you mean
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
> i do use a key listner if thats what you mean

then delay adding it for 5 seconds
0
 
SamooramadAuthor Commented:
how do I do that?
0
 
objectsCommented:
the code i posted above is one way to achieve it, you could also use a Timer.
0
 
SamooramadAuthor Commented:
objects, does your code add the listner more than once?
0
 
objectsCommented:
no, it just delays adding it for 5 seconds.
0
 
SamooramadAuthor Commented:
maybe I needed to be more clear..or Im not quite understanding this.
I want to add a keylistner to a component (thats done) then have the option of either pressing one key or pressing two.
so after the first key is pressed the program waits for 5 seconds and if another one hasnt been pressed then it is assumed that only one key was pressed and so on for other components. Does your code still apply to that?

sorry if I'm being a pain :)
0
 
objectsCommented:
in that case store the time the first key is pressed, and compare the difference when the second key is pressed.
0
 
nesnemisConnect With a Mentor Commented:
Hi

First you add a keylistener to your textfield, then put the following code in keyPressed:
if(t != null && t.isAlive())
   t.stop();
t = new Thread(new WaitForFunction());
t.start();

the class WaitForFunction is:
class WaitForFunction implements Runnable
{
   public void run()
   {
      try
      {
         Thread.sleep(5000);
         //Put your function here
      }
      catch(Exception e)
      {}
   }
}

Hope this helps :-)
0
 
nesnemisCommented:
the variable t must, of course, be global...
0
 
SamooramadAuthor Commented:
how objects?
thanks newnemis :) I'm working on your code will let u know if I get it working or not
0
 
SamooramadAuthor Commented:
nesnemis, how do I declare t?
Thread t;   ??
0
 
DrWarezzCommented:
Yes;  Thread t;
=)
[r.D]
0
 
SamooramadAuthor Commented:
how do you check if a key was pressed during the 5 seconds?
0
 
DrWarezzConnect With a Mentor Commented:
>"I need a bit of code for an if statement that waits for a key press for 5 seconds.. and if a key is not pressed in that time it does something"

Perhaps try this:

public class TestClass {
    boolean bPressed = false;
    int iTimeout = 5;

    public static void main ( String [] args ) {
        Thread t = new Thread ( new KeyListenClass() );
        t.start();
       
        try { Thread.sleep( iTimeout*1000 ); } catch (Exception e) {}
       
        t.stop();
       
        if ( bPressed ) {
            // A key has been pressed, do whatever here.
        } else {
            // A key has NOT been pressed, do whatever here.
        }
    }
   
    public class KeyListenClass extends Thread {
        KeyListenClass() {}
        public void run () {
           
            // Add keylistener to textfield here.
            // If a key is pressed, set bPressed = true;
           
        }
    }
   
}

Obviously, that won't just work on its' own; you have to add the textfield, and everything else. But, there's the syntax, I hope it's what you're after. =)

Best of luck,
[r.D]
0
 
DrWarezzCommented:
*woops, I think you need to declare the bPressed as public, like so:

public boolean bPressed = false;

=)
0
 
SamooramadAuthor Commented:
thanks everyone for the help
0
 
DrWarezzCommented:
8-)  ThanQ
0
All Courses

From novice to tech pro — start learning today.