aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothée Floure2016-05-09 16:37:10 +0200
committerTimothée Floure2016-05-09 16:37:10 +0200
commitaa7cd18ee5b13ee4373db58dc8ab9157a348c72e (patch)
treea1ea64130b8cf4a571c8af0fece86285db6a78e4
parentc12373535ca703ac2fbbd1d1775547b142d3970c (diff)
downloadxblast-aa7cd18ee5b13ee4373db58dc8ab9157a348c72e.tar.gz
Implementation of the KeyboardEventHandler Class
-rw-r--r--src/ch/epfl/xblast/client/KeyboardEventHandler.java38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/client/KeyboardEventHandler.java b/src/ch/epfl/xblast/client/KeyboardEventHandler.java
new file mode 100644
index 0000000..bebc4a5
--- /dev/null
+++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java
@@ -0,0 +1,38 @@
1package ch.epfl.xblast.client;
2
3import ch.epfl.xblast.PlayerAction;
4
5import java.awt.event.KeyAdapter;
6import java.awt.event.KeyEvent;
7import java.util.Map;
8import java.util.function.Consumer;
9
10/**
11 * @author Timothée FLOURE (257420)
12 */
13public class KeyboardEventHandler extends KeyAdapter{
14 private final Map<Integer,PlayerAction> playerActionMap;
15 private final Consumer consumer;
16
17 /**
18 * Instantiates a new KeyboardEventHandler.
19 *
20 * @param playerActionMap the map of key codes to PlayerAction.
21 * @param consumer consumer related to the EventHandler
22 */
23 public KeyboardEventHandler(Map<Integer,PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) {
24 this.playerActionMap = playerActionMap;
25 this.consumer = consumer;
26 }
27
28 /**
29 * Executes the command related to the given keycode.
30 *
31 * @param e event (pressed key)
32 */
33 @Override
34 public void keyTyped(KeyEvent e) {
35 if (playerActionMap.containsKey(e.getKeyCode()))
36 consumer.accept(playerActionMap.get(e.getKeyCode()));
37 }
38}