Reputation: 99
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!
Upvotes: 2
Views: 4536
Reputation: 99
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);
Upvotes: 3