From 9eb40df251cc618113963da0c4933584ff4b3a78 Mon Sep 17 00:00:00 2001 From: Emily Hunt Date: Sat, 18 Jan 2025 23:47:36 +0100 Subject: [PATCH] Add new user input system --- src/sk8plotlib/input.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/sk8plotlib/input.py diff --git a/src/sk8plotlib/input.py b/src/sk8plotlib/input.py new file mode 100644 index 0000000..800b62b --- /dev/null +++ b/src/sk8plotlib/input.py @@ -0,0 +1,30 @@ +class UserInput: + def __init__(self, fig): + self.fig = fig + self._held_keys = set() + fig.canvas.mpl_connect("key_press_event", self.key_press_handler) + fig.canvas.mpl_connect("key_release_event", self.key_release_handler) + + def key_press_handler(self, event): + self._held_keys.add(event.key) + + def key_release_handler(self, event): + self._held_keys.remove(event.key) + + def left_right_input(self): + """Calculates the difference in left/right input.""" + output = 0 + if "right" in self._held_keys: + output += 1 + if "left" in self._held_keys: + output -= 1 + return output + + def up_down_input(self): + """Calculates the difference in left/right input.""" + output = 0 + if "up" in self._held_keys: + output += 1 + if "down" in self._held_keys: + output -= 1 + return output -- 2.54.0