Question

I have this in my main class Board (extends JPanel):

 public class TAdapter extends KeyAdapter {

   public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if ((key == KeyEvent.VK_LEFT) && (!right)) {
            left = true;
            up = false;
            down = false;
        }
  }

I am trying to test if left becomes true when I press the left key:

 @Test
 public void testKeyPressed() throws AWTException {
    Board instance = new Board();
    Robot rob = new Robot();
    instance.setFocusable(true);
    instance.requestFocus();
    rob.keyPress(KeyEvent.VK_LEFT);
    System.out.println(instance.up);
}

Thanks!

Was it helpful?

Solution

Here is some code that can accomplish that:

KeyEvent key = new KeyEvent(instance, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0,  KeyEvent.VK_UP,'Z');
    instance.getKeyListeners()[0].keyPressed(key);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top