Clicking two buttons at the same time
Hello. How i can make my application to handle two buttons clicking at the same time? Now i can move graphics only left or up, but can't move them obliquely left and up. Here is my code how im doing:
Code:
messageField.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT){
x -= 1;
messageField.setText("UPDATE:" + x + ":" + y);
senderr.sendMsg();
messageField.setText(null);
}
if(e.getKeyCode() == KeyEvent.VK_UP){
y -= 1;
messageField.setText("UPDATE:" + x + ":" + y);
senderr.sendMsg();
messageField.setText(null);
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent e) {}
});
Re: Clicking two buttons at the same time
When the key is pressed you need to note somewhere the key is currently pressed.
When it is realesed you can then reset the flag you're using.
That way you can tell if LEFT and UP are both pressed and therefore move diagonally.
Note that a KeyEvent only covers a single key, so the stuff I've said above only really works if you have a loop in which your graphic is moved depending on the button states. Looking at that code it appears to only change x or y on the actual button press.
Re: Clicking two buttons at the same time
Moved from New to Java.
Please don't call a key a button. The two are quite different entities.
db
Re: Clicking two buttons at the same time
Swing is designed to work with key bindings. Separate actions can be associated with the 'pressed' and 'released' states. Go through this tutorial: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
db