Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

737 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