How do I deal with multiple button presses with KeyListener?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

How do I deal with multiple button presses with KeyListener?

 
0
  #1
Aug 7th, 2007
Hello,

I'm creating a game and in this game if you press two particular buttons at once, a particular move is done. How do I go about doing this with KeyListener? Is there a way to get it to wait for a fraction of a second to see if there's another button press?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #2
Aug 7th, 2007
Well, it won't wait but you can keep track of it for yourself with something like this
  1. // class level
  2. long lastKeyPress = System.currentTimeMillis();
  3. int lastKey=0;
  4. // whichever keys you need
  5. final static int KEY_1 = KeyEvent.VK_A;
  6. final static int KEY_2 = KeyEvent.VK_B;
  7. // whatever interval you need for "at the same time"
  8. // 50ms seems to work ok
  9. final static int TIME_LIMIT = 50;
  10.  
  11. // wherever you set up the KeyListener
  12. component.addKeyListener(new KeyAdapter() {
  13. public void keyPressed(KeyEvent e){
  14. if (System.currentTimeMillis() - lastKeyPress < TIME_LIMIT && ((lastKey|e.getKeyCode())==(KEY_2|KEY_1)) ){
  15. // within time limit
  16. doSpecial();
  17. }
  18.  
  19. lastKeyPress = System.currentTimeMillis();
  20. lastKey = e.getKeyCode();
  21. }
  22. });
Last edited by Ezzaral; Aug 7th, 2007 at 5:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #3
Aug 7th, 2007
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?
Last edited by Mr.UNOwen; Aug 7th, 2007 at 7:04 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #4
Aug 7th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #5
Aug 7th, 2007
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?
Last edited by Mr.UNOwen; Aug 7th, 2007 at 7:41 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #6
Aug 7th, 2007
Originally Posted by Mr.UNOwen View Post
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?
Not sure, System.out.println(System.currentTimeMillis() - lastTime) is a decent way to find out though.

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?
Yes,I can see where it would be a bit confusing in that both threads and applets have start() methods. In the applet start() method, they are creating a new Thread with the constructor 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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #7
Aug 7th, 2007
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
  1. class KeyProcessor extends javax.swing.Timer {
  2. List keyQueue = new ArrayList();
  3.  
  4. final static int KEY_1 = KeyEvent.VK_A;
  5. final static int KEY_2 = KeyEvent.VK_B;
  6.  
  7. public KeyProcessor(int delay, ActionListener listener) {
  8. super(delay, null);
  9. addActionListener(new ActionListener() {
  10. public void actionPerformed(ActionEvent evt) {
  11. if (keyQueue.contains(KEY_1) && keyQueue.contains(KEY_2))
  12. lblState.setText("State A+B");
  13. else if (keyQueue.contains(KEY_1))
  14. lblState.setText("State A");
  15. else if (keyQueue.contains(KEY_2))
  16. lblState.setText("State B");
  17. keyQueue.clear();
  18. }
  19. });
  20. }
I then started that up and attached a KeyListener to feed the key queue
  1. keyProcessor = new KeyProcessor(200,null);
  2. txtInput.addKeyListener(new KeyAdapter() {
  3. public void keyPressed(KeyEvent e){
  4. keyProcessor.queueKey(e.getKeyCode());
  5. }
  6. });
  7. keyProcessor.start();
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.
Last edited by Ezzaral; Aug 7th, 2007 at 8:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #8
Aug 7th, 2007
Originally Posted by Ezzaral View Post
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 release? I think I may have a solution if it reports release for both buttons (doesn't matter if it is or not at the same time).

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #9
Aug 7th, 2007
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.
Last edited by Mr.UNOwen; Aug 7th, 2007 at 9:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

Re: How do I deal with multiple button presses with KeyListener?

 
0
  #10
Aug 7th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum


Views: 4784 | Replies: 13
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC