From aa7cd18ee5b13ee4373db58dc8ab9157a348c72e Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 16:37:10 +0200 Subject: Implementation of the KeyboardEventHandler Class --- .../epfl/xblast/client/KeyboardEventHandler.java | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/ch/epfl/xblast/client/KeyboardEventHandler.java (limited to 'src/ch') 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 @@ +package ch.epfl.xblast.client; + +import ch.epfl.xblast.PlayerAction; + +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.util.Map; +import java.util.function.Consumer; + +/** + * @author Timothée FLOURE (257420) + */ +public class KeyboardEventHandler extends KeyAdapter{ + private final Map playerActionMap; + private final Consumer consumer; + + /** + * Instantiates a new KeyboardEventHandler. + * + * @param playerActionMap the map of key codes to PlayerAction. + * @param consumer consumer related to the EventHandler + */ + public KeyboardEventHandler(Map playerActionMap, Consumer consumer) { + this.playerActionMap = playerActionMap; + this.consumer = consumer; + } + + /** + * Executes the command related to the given keycode. + * + * @param e event (pressed key) + */ + @Override + public void keyTyped(KeyEvent e) { + if (playerActionMap.containsKey(e.getKeyCode())) + consumer.accept(playerActionMap.get(e.getKeyCode())); + } +} -- cgit v1.2.3