Solved

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

Posted on 2004-08-26
4
332 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 125 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now