Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

While pressing a key, pressing another key and wanting the first key press event to fire

Posted on 2004-08-26
4
Medium Priority
?
340 Views
Last Modified: 2010-03-31
I want to create a situation like a car game:
When the UP arrow is pressed the car moves forward.
While the UP key is pressed, RIGHT \ LEFT can move the car sideways but the car is still moving forward (Because the UP arrow wasn't released).

In my app, While pressing UP, when the RIGHT \ LEFT key is pressed, it moves the car sideways but stops moving forward (The UP key is not considered anymore).

Thanks (Can use any JDK needed).
0
Comment
Question by:ElishaMoshe
4 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 11908632
Hi Friend;

1st of all you need JDK for you to compile and run your codes. Now after that you need to study the key event in Java :

http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

To download Java, Go here :
http://java.sun.com/j2se/1.4.2/download.html

Hope that helps . . .
Javatm
0
 
LVL 14

Expert Comment

by:existenz2
ID: 11908910
I think using threads is an solution to your problem. This will enable you to keep your car going forward while the left/right key is pressed. It will also go left or right depending the chosen key.

I suggest to make 1 thread which constantly updates the car's movement (left,right,forwards,backwards) and with the keypress event you will be able to manupulate the movement of the car.

Info about threading:
http://java.sun.com/docs/books/tutorial/essential/threads/
0
 
LVL 18

Accepted Solution

by:
armoghan earned 375 total points
ID: 11910038
You can keep a variable like upKeyPressed who's value you can set as soon as the UP key is pressed and change its value when Up key is released. Now you can check its value for deciding, that you want to stop or move side ways on RIGHT/LEFT button press.

I hope it will solve the problem with out creating threads
0
 
LVL 35

Expert Comment

by:girionis
ID: 11910526
You will need a thread running. Here is an applet that deals with two key presses at once: http://javaboutique.internet.com/tutorials/Java_by_Example/section6_5.html
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

824 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