Solved

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

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 66
Java Timer (static) 9 43
runtime exception 2 28
jboss wildfly 10.1 10 87
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

770 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