1 package nl.tudelft.jpacman.ui;
2
3 import java.awt.event.KeyEvent;
4 import java.awt.event.KeyListener;
5 import java.util.Map;
6
7 /**
8 * A key listener based on a set of keyCode-action pairs.
9 *
10 * @author Jeroen Roosen
11 */
12 class PacKeyListener implements KeyListener {
13
14 /**
15 * The mappings of keyCode to action.
16 */
17 private final Map<Integer, Action> mappings;
18
19 /**
20 * Create a new key listener based on a set of keyCode-action pairs.
21 * @param keyMappings The mappings of keyCode to action.
22 */
23 PacKeyListener(Map<Integer, Action> keyMappings) {
24 assert keyMappings != null;
25 this.mappings = keyMappings;
26 }
27
28 @Override
29 public void keyPressed(KeyEvent event) {
30 assert event != null;
31 Action action = mappings.get(event.getKeyCode());
32 if (action != null) {
33 action.doAction();
34 }
35 }
36
37 @Override
38 public void keyTyped(KeyEvent event) {
39 // do nothing
40 }
41
42 @Override
43 public void keyReleased(KeyEvent event) {
44 // do nothing
45 }
46 }