Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Clicking two buttons at the same time

  1. #1
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default 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:
    Java 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) {}
    		});

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default 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.
    Please do not ask for code as refusal often offends.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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
    Rylogy likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 02-29-2012, 10:07 AM
  2. Replies: 3
    Last Post: 12-07-2011, 07:00 AM
  3. making a Time class but Universal time always says 0
    By Bravid2001 in forum New To Java
    Replies: 2
    Last Post: 10-24-2011, 07:55 AM
  4. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  5. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •