Solved

key stroke time limit

Posted on 2004-09-17
20
214 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Samooramad
  • 9
  • 5
  • 4
  • +1
20 Comments
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 12089250
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
 

Author Comment

by:Samooramad
ID: 12089350
>> gui or non-gui?

whats the difference?
0
 

Author Comment

by:Samooramad
ID: 12089355
i do use a key listner if thats what you mean
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Expert Comment

by:objects
ID: 12089364
> i do use a key listner if thats what you mean

then delay adding it for 5 seconds
0
 

Author Comment

by:Samooramad
ID: 12089404
how do I do that?
0
 
LVL 92

Expert Comment

by:objects
ID: 12089428
the code i posted above is one way to achieve it, you could also use a Timer.
0
 

Author Comment

by:Samooramad
ID: 12089465
objects, does your code add the listner more than once?
0
 
LVL 92

Expert Comment

by:objects
ID: 12089469
no, it just delays adding it for 5 seconds.
0
 

Author Comment

by:Samooramad
ID: 12089482
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
 
LVL 92

Expert Comment

by:objects
ID: 12089508
in that case store the time the first key is pressed, and compare the difference when the second key is pressed.
0
 
LVL 3

Assisted Solution

by:nesnemis
nesnemis earned 100 total points
ID: 12090618
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
 
LVL 3

Expert Comment

by:nesnemis
ID: 12090621
the variable t must, of course, be global...
0
 

Author Comment

by:Samooramad
ID: 12091472
how objects?
thanks newnemis :) I'm working on your code will let u know if I get it working or not
0
 

Author Comment

by:Samooramad
ID: 12091648
nesnemis, how do I declare t?
Thread t;   ??
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12091806
Yes;  Thread t;
=)
[r.D]
0
 

Author Comment

by:Samooramad
ID: 12091840
how do you check if a key was pressed during the 5 seconds?
0
 
LVL 9

Assisted Solution

by:DrWarezz
DrWarezz earned 100 total points
ID: 12091913
>"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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12091921
*woops, I think you need to declare the bPressed as public, like so:

public boolean bPressed = false;

=)
0
 

Author Comment

by:Samooramad
ID: 12093879
thanks everyone for the help
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12096591
8-)  ThanQ
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 58
Error trying to install RTMT Win7 5 60
MySqlDump not dumping triggers 1 31
Tomcat: Unable to run tomcat service. 2 18
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question