JFreeman
JFreeman

Reputation: 1004

Java get simultaneos input from key listener

I am creating a simple game in which a keypressed event moves a JLabel a certain number of pixels in a JPanel.

It works without any errors and when i hit the right arrow it moves right, and so on. However, as soon as i press a second key it interrupts the original movement.

For example if i was holding down the left key (and the object was moving left) and then i hit the up arrow the object stops moving left. This is (of course) because i am using only one key event for all my key.

This obviously is not an acceptable situation because as soon as i add a second object (for another player) each player will be constantly stopping the other players movement.

So my question is this: How best can i incorporate multiple simultaneous key actions? (preferably without writing dozens of KeyAdapter's and KeyEvent's)

INFO: currently i have a second class which extends a KeyAdapter. This second class includes a KeyPressed function which then uses .getKeyCode(); to find out which key is pressed and act.

EDIT: any suggestions are welcome!

Upvotes: 1

Views: 408

Answers (1)

JFreeman
JFreeman

Reputation: 1004

The solution is actually quite simple. When a key pressed event is registered store a variable that records the movement. When a key released event is registered un-store that variable.

This then means that until you release you keys the movement will continue to be registered. The solution also requires a timer in order to loop repeatedly and check if your event is running or not. You could use a delay but a separate thread is the most effective for this type of task.

First you need a global variable that can store your action:

String action = "";

Then you need a key listener to record your input and when you stop inputting:

// create key listener
addKeyListener(new KeyListener()
    {
          @Override 
          public void keyPressed(KeyEvent e)
          {         
              int keyCode = e.getKeyCode();
              String event = KeyEvent.getKeyText(keyCode);


              if (event.equals("A")) {
                  // DO YOUR EVENT IF THE KEY HAS BEEN PRESSED
                  action = "A";
              }
          }

        @Override
        public void keyTyped(KeyEvent e) {}

        @Override
        public void keyReleased(KeyEvent e) {
              int keyCode = e.getKeyCode();
              String event = KeyEvent.getKeyText(keyCode);


              if (event.equals("A")) {
                  // STOP DOING YOUR EVENT
                  action = "";
              }

         }

    });

Lastly you will need a timer thread to loop and do your action:

    // CREATE A TIMER THREAD
       Timer timer = new Timer();
       timer.schedule(new TimerTask() {

           // this is what happens every time the timer runs
           @Override
           public void run() { 
             // check if your action should be preformed
             if (action.equals("A")) {
                 // ... do something
             }

           }

       }, 0, 1000);   // 1000 MILLESECONDS = 1 SECOND

       // note: you will need to run 'timer.cancel();' at some point to end your timer

Also it is worth looking at these other topics, including this about keylistener is games. And about timer tasks.

Upvotes: 0

Related Questions