File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Multiple Key Press Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple Key Press" Watch "Multiple Key Press" New topic
Author

Multiple Key Press

rob starteren
Greenhorn

Joined: Mar 30, 2009
Posts: 2
Trying to figure out the animation framework in Java and i managed to get a box to move around a JFrame.

I can move up down left and right using the arrow keys. How can i tell the box to move diagonally up without using a new key.

So how do i know if the user pressed the up and right key at the same time to make the box move diagonally up to the right?

if(evt.getKeyCode()==39)
{
posX ++;
direction="right";
}
else if((evt.getKeyCode()==38))
{
posY --;
direction="down";
}


else if((evt.getKeyCode()==40))
{
posY ++;
direction="up";
}
else if((evt.getKeyCode()==37))
{
posX --;
direction="left";
}

System.out.println(evt.getKeyCode());

Because if i hold up and right at the same time it just registers that as 2 single events. Any ideas?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You already found out that it is two events. So in between those two events, you should keep track of all relevant key presses.

In this case:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Key Press
 
Similar Threads
Play Lunar Lander (was: Lunar Lander Left and Right Movement)
what is need of super() in the foolowing line of code?
Please help with this program
C++ish Address &
Is there something better then event.pageX/Y?