0

I wanna to program a 2-player game, where one of them play with wsad and the other one with yghj. If one of the players keeps key pressed down, KeyListener can't listen to other key. How should I do it so that both of them can be listened for?

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == 39)
        player1.setBounds((dim_player1.width += 5), dim_player1.height, 80, 120);
    else if (e.getKeyCode() == 37)
        player1.setBounds((dim_player1.width -= 5), dim_player1.height, 80, 120);
    else if (e.getKeyCode() == 40)
        player1.setBounds(dim_player1.width, (dim_player1.height += 5), 80, 120);
    else if (e.getKeyCode() == 38)
        player1.setBounds(dim_player1.width, (dim_player1.height -= 5), 80, 120);

}
Moein Hosseini
  • 4,309
  • 15
  • 68
  • 106
  • 1
    possible duplicate of [Swing's KeyListener and multiple keys pressed at the same time.](http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time) – Sean Patrick Floyd Jun 29 '11 at 08:38
  • I saw it,but I can't understand what should I do if (pressed.size() > 1) { // More than one key is currently pressed. // Iterate over pressed to get the keys. } e.g I wanna to getKeyChar of both of key ,how should I do it it this? – Moein Hosseini Jun 29 '11 at 08:44
  • Possible duplicate of [Swing's KeyListener and multiple keys pressed at the same time](https://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time) –  Sep 25 '19 at 00:03

1 Answers1

0

Erm. It's 6 am in my TZ, so I might be wrong, but - threading. Store the keyboard state in a thread-safe set/map, update the set/map on press event, use the state every game tick to execute the results.

Or just use a bool array. Will work as well in most cases. For example:

static public enum camera_action {
  ESCAPE, // e.g. exit to main menu, terminate mouse capture etc
  MV_FORWARD, MV_BACKWARD, MV_STRAFE_LEFT, MV_STRAFE_RIGHT, MV_JUMP, MV_CROUCH, // etc
}

final protected SC_HashMap<Integer, camera_action> keymap = new SC_HashMap.linked<>();
protected boolean[] action_state = new boolean[camera_action.values().length];

final protected void set_default_keymap() {
  keymap.put( KeyEvent.VK_SHIFT, camera_action.MV_JUMP );
  keymap.put( KeyEvent.VK_CONTROL, camera_action.MV_CROUCH );
  keymap.put( KeyEvent.VK_W, camera_action.MV_FORWARD );
  keymap.put( KeyEvent.VK_S, camera_action.MV_BACKWARD );
  keymap.put( KeyEvent.VK_A, camera_action.MV_STRAFE_LEFT );
  keymap.put( KeyEvent.VK_D, camera_action.MV_STRAFE_RIGHT );
  keymap.put( KeyEvent.VK_ESCAPE, camera_action.ESCAPE );
}

protected void set_action_state( camera_action a, boolean new_state ) {
  action_state[a.ordinal()] = new_state;
}

protected boolean get_action_state( camera_action a ) {
  return action_state[a.ordinal()];
}

public void reset_action_state() {
  action_state = new boolean[camera_action.values().length];
}

@Override
public void keyPressed( KeyEvent e ) {
  camera_action a = keymap.get( e.getKeyCode() );
  if ( a != null )
    set_action_state( a, true );
}

@Override
public void keyReleased( KeyEvent e ) {
  camera_action a = keymap.get( e.getKeyCode() );
  if ( a != null )
    set_action_state( a, false );
}

I know the Oracle's policy on Enum.ordinal() - I don't give a heck about it. If EnumSet can use it, so can I. The contract of ordinal() is solid enough.

And - yeah. possible dupe of Swing's KeyListener and multiple keys pressed at the same time.

Community
  • 1
  • 1