944,153 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8873
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 7th, 2007
0

How do I deal with multiple button presses with KeyListener?

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
Mr.UNOwen is offline Offline
127 posts
since Oct 2006
Aug 7th, 2007
0

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

Well, it won't wait but you can keep track of it for yourself with something like this
java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 7th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Mr.UNOwen is offline Offline
127 posts
since Oct 2006
Aug 7th, 2007
0

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

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 7th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Mr.UNOwen is offline Offline
127 posts
since Oct 2006
Aug 7th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by Mr.UNOwen ...
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.

Quote ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 7th, 2007
0

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

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
java Syntax (Toggle Plain Text)
  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
java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 7th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Mr.UNOwen is offline Offline
127 posts
since Oct 2006
Aug 7th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Mr.UNOwen is offline Offline
127 posts
since Oct 2006
Aug 7th, 2007
0

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

What am I doing wrong.... shouldn't main be on a different thread? It stops at "Sleeping", Shouldn't it continue.

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

}
Quote ...

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Mr.UNOwen is offline Offline
127 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Window closes too quickly
Next Thread in Java Forum Timeline: shifting the contents





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC