| | |
How do I deal with multiple button presses with KeyListener?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Well, it won't wait but you can keep track of it for yourself with something like this
java Syntax (Toggle Plain Text)
// class level long lastKeyPress = System.currentTimeMillis(); int lastKey=0; // whichever keys you need final static int KEY_1 = KeyEvent.VK_A; final static int KEY_2 = KeyEvent.VK_B; // whatever interval you need for "at the same time" // 50ms seems to work ok final static int TIME_LIMIT = 50; // wherever you set up the KeyListener component.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e){ if (System.currentTimeMillis() - lastKeyPress < TIME_LIMIT && ((lastKey|e.getKeyCode())==(KEY_2|KEY_1)) ){ // within time limit doSpecial(); } lastKeyPress = System.currentTimeMillis(); lastKey = e.getKeyCode(); } });
Last edited by Ezzaral; Aug 7th, 2007 at 5:11 pm.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
Well that kind of does the job, but there's one issue. So lets say a and b are the keys I'm looking at. The issue comes in when each button has a job. I want each to do their job when individually pressed, but when pressed together do a different job without ever doing the individual ones. So if a is pressed before b but within the given time, I don't want job 'a' to take place before 'a'+'b' , but at same time I want a to take place if 'a'+'b' was NOT pressed in time.
Let's say I create a class called ButtonInterpretation and it recieves the key press and time. I want something where when a is pressed it will suspend for the given time and if b is pressed in the given time it will interupt the suspend and return a+b otherwise after the given time a will be returned and it will suspend the next a or b and repeat process.
So my question is there a suspend function or way to suspend that has a way to be interrupted and does not suspend the entire program.
The way it's going to work is the KeyListener will send the key and time to my Character class that has a KeyInterpreter in it. The information will be sent to KeyInterpreter. I want to avoid preventing any method in my character class that doesn't deal with KeyInterpreter to continue functioning. Do I have to deal with threads?
Let's say I create a class called ButtonInterpretation and it recieves the key press and time. I want something where when a is pressed it will suspend for the given time and if b is pressed in the given time it will interupt the suspend and return a+b otherwise after the given time a will be returned and it will suspend the next a or b and repeat process.
So my question is there a suspend function or way to suspend that has a way to be interrupted and does not suspend the entire program.
The way it's going to work is the KeyListener will send the key and time to my Character class that has a KeyInterpreter in it. The information will be sent to KeyInterpreter. I want to avoid preventing any method in my character class that doesn't deal with KeyInterpreter to continue functioning. Do I have to deal with threads?
Last edited by Mr.UNOwen; Aug 7th, 2007 at 7:04 pm.
I'd say yes, you're probably going to have to deal with threads for that. It sounds like you have a decent handle on how to proceed with it via the KeyInterpreter. If you queue the received keys and process them in a timer event you can dispatch to the appropriate actions. Of course, tuning that dispatch to prevent lagging of the individual A and B actions or allowing the transitions such as A to AB or B to AB may be the tricky part.
It sounds like the transition states are what you are wanting to avoid, so I would think if you keep the timer event short enough you can dispatch to the appropriate method based on the queued keys.
It sounds like the transition states are what you are wanting to avoid, so I would think if you keep the timer event short enough you can dispatch to the appropriate method based on the queued keys.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
Well acctualy I'm clueless with threads, I just happen to be using one off a tutorial for my animation. I guess at this point I just have to do some research on threads. By the way do you think 50 is faster than humanly possible to double tap a key?
If you have the time, mind answering this question...
I was following this example: ( http://www.javaworld.com/jw-03-1996/...le1Applet.html ) and what confuses me is the start function. Being that the new thread is "this" and in the start function, start is called in the new thread; how's that not a looping effect?
If you have the time, mind answering this question...
I was following this example: ( http://www.javaworld.com/jw-03-1996/...le1Applet.html ) and what confuses me is the start function. Being that the new thread is "this" and in the start function, start is called in the new thread; how's that not a looping effect?
Last edited by Mr.UNOwen; Aug 7th, 2007 at 7:41 pm.
•
•
•
•
Well acctualy I'm clueless with threads, I just happen to be using one off a tutorial for my animation. I guess at this point I just have to do some research on threads. By the way do you think 50 is faster than humanly possible to double tap a key?
•
•
•
•
If you have the time, mind answering this question...
I was following this example: ( http://www.javaworld.com/jw-03-1996/...le1Applet.html ) and what confuses me is the start function. Being that the new thread is "this" and in the start function, start is called in the new thread; how's that not a looping effect?
Thread(Runnable target). Because they have defined the applet to implement the Runnable interface and provided a run() method in the class, they can pass the class object "this" to the Thread constructor. They then start that thread running with the animator.start() call. The fact that it was done in the applet start() method does make it a bit awkward to read if you aren't used to threads. I was playing around with a timer-based key processor and I think you are going to have issues with key listener. Here is the processor I wrote I then started that up and attached a KeyListener to feed the key queue It will process the A+B state fine just once if the key combo comes in within the time slice, but if the keys are held down then only the last key pressed gets repeated, so the state will change either A or B. Now, I am running this on a Windows XP machine so maybe it's a system specific thing with how repeated keypresses are handled, but it is obviously an issue on Windows.
I haven't had to do anything like this with KeyListeners before, so maybe there is some other technique to listen for both of those keys, but keyPressed() isn't receiving both key code.
java Syntax (Toggle Plain Text)
class KeyProcessor extends javax.swing.Timer { List keyQueue = new ArrayList(); final static int KEY_1 = KeyEvent.VK_A; final static int KEY_2 = KeyEvent.VK_B; public KeyProcessor(int delay, ActionListener listener) { super(delay, null); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (keyQueue.contains(KEY_1) && keyQueue.contains(KEY_2)) lblState.setText("State A+B"); else if (keyQueue.contains(KEY_1)) lblState.setText("State A"); else if (keyQueue.contains(KEY_2)) lblState.setText("State B"); keyQueue.clear(); } }); }
java Syntax (Toggle Plain Text)
keyProcessor = new KeyProcessor(200,null); txtInput.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e){ keyProcessor.queueKey(e.getKeyCode()); } }); keyProcessor.start();
I haven't had to do anything like this with KeyListeners before, so maybe there is some other technique to listen for both of those keys, but keyPressed() isn't receiving both key code.
Last edited by Ezzaral; Aug 7th, 2007 at 8:25 pm.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
•
•
•
•
It will process the A+B state fine just once if the key combo comes in within the time slice, but if the keys are held down then only the last key pressed gets repeated, so the state will change either A or B. Now, I am running this on a Windows XP machine so maybe it's a system specific thing with how repeated keypresses are handled, but it is obviously an issue on Windows.
I haven't had to do anything like this with KeyListeners before, so maybe there is some other technique to listen for both of those keys, but keyPressed() isn't receiving both key code.
How about if you double tap a b? Does it still detect a b at some point of time for both taps?
By the way I'm still reading up on threads.
Last edited by Mr.UNOwen; Aug 7th, 2007 at 8:53 pm.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
hey Ezzaral if I call a method of a thread while it's sleeping, will it still function?
In other words if my KeyInterpreter object is sleeping can I still call a method of that particular object.
In other words if my KeyInterpreter object is sleeping can I still call a method of that particular object.
Last edited by Mr.UNOwen; Aug 7th, 2007 at 9:24 pm.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
What am I doing wrong.... shouldn't main be on a different thread? It stops at "Sleeping", Shouldn't it continue.
•
•
•
•
public class Dem extends Thread
{
private int a;
public Dem(int in)
{
a = in;
}
public void run()
{
try
{
System.out.println("sleeping");
sleep(100000);
}
catch (InterruptedException e)
{
System.out.println("interupt");
}
System.out.println("done with Dem");
}
public int geta()
{
return a;
}
public void seta(int in)
{
a = in;
}
}
•
•
•
•
public class ThTstser
{
public static void main(String[] args)
{
Dem v = new Dem(4);
Thread f = new Thread(v);
System.out.println(v.geta());
f.run();
System.out.println("running");
System.out.println(v.geta());
System.out.println(v.geta());
System.out.println(v.geta());
v.seta(5);
System.out.println(v.geta());
System.out.println(v.geta());
System.out.println(v.geta());
System.out.println("tst done");
}
}
Last edited by Mr.UNOwen; Aug 7th, 2007 at 10:03 pm.
![]() |
Similar Threads
- Project Ideas (C++)
- MS Access vs. MySQL (MS Access and FileMaker Pro)
- Pointers (archived tutorial) (C++)
- Dynamic Connection String Using ADO (Visual Basic 4 / 5 / 6)
- Please help with a script (Visual Basic 4 / 5 / 6)
- Pointers (C++)
- problem:on click button pagr refresh again (VB.NET)
- Dr Scheme example (Computer Science)
Other Threads in the Java Forum
- Previous Thread: Window closes too quickly
- Next Thread: shifting the contents
Views: 4784 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






