Solved

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

Posted on 2004-08-26
4
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

622 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