Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Multiple Event Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple Event Handling" Watch "Multiple Event Handling" New topic
Author

Multiple Event Handling

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

How do you handle multiple events at the same time? For example, I have a game. I want to push the right arrow along with the up key. How would I be able to receive the input at the same time?

Looks something like this :


Thanks,
John Price


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I figured it out thanks. What I did to "fix" the problem was to not stop the thread until the keyReleased method was set off by that key (an additional switch statement in my case with "VK_RIGHT" and "VK_LEFT")

Thanks,
John Price

EDIT :
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Usually something like this is the approach to go for. On key pressed you store the keys somewhere, and on key released you remove them from the storage. All keys in the storage are then the currently pressed keys.

However, moving = false may be premature. What if I still have UP pressed? Aren't I then still moving?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

For pressing up, it activates another variable, left and right are on the "moving" variable and up is on a separate variable.

John Price
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Event Handling
 
Similar Threads
Movement of user causes AI to lag
Please critique!
Some help needed to find an event stopping condition
Gray box where my applet should be
Java KeyListener - can't detect multiple pressed buttons