diff --git a/botable/botable.py b/botable/botable.py index f6ff4f1..2cc29ca 100644 --- a/botable/botable.py +++ b/botable/botable.py @@ -3,10 +3,11 @@ from typing import Deque, Iterable, Iterator, List, Optional, Union from pynput import keyboard, mouse # type: ignore from pynput.mouse import Button # type: ignore -from pynput.keyboard import Key, KeyCode # type: ignore +from pynput.keyboard import Key, KeyCode # type: ignore from botable.button_event import ButtonEvent + def _to_key(key: str) -> Union[Key, KeyCode, str]: try: return eval(f"Key.{key}") @@ -18,9 +19,11 @@ def _to_key(key: str) -> Union[Key, KeyCode, str]: pass return KeyCode.from_char(key) + RECORDING = False PLAYING = False + def record(exit_key: str = "f1", pause_key: str = "f2") -> Iterator[ButtonEvent]: """ Launch the recording, yielding the keyboard and mouse click events as they occur. @@ -85,6 +88,7 @@ def on_click(x: int, y: int, button: mouse.Button, pressed: bool): finally: RECORDING = False + def play( button_events: Iterable[ButtonEvent], exit_key: str = "f1", @@ -153,7 +157,9 @@ def on_press(key: keyboard.Key): mouse_ctrl.position = button_event.coordinates ctrl = mouse_ctrl - if button_event.button.startswith("<") and button_event.button.endswith(">"): + if button_event.button.startswith("<") and button_event.button.endswith( + ">" + ): evaluated_button = KeyCode(int(button_event.button[1:-1])) else: evaluated_button = eval(button_event.button) @@ -164,7 +170,7 @@ def on_press(key: keyboard.Key): ctrl.press(evaluated_button) else: ctrl.release(evaluated_button) - + if not continue_: return diff --git a/botable/button_event.py b/botable/button_event.py index 4d77342..ae2750d 100644 --- a/botable/button_event.py +++ b/botable/button_event.py @@ -1,12 +1,14 @@ from contextlib import suppress from typing import Iterable, Iterator, NamedTuple, Optional, Tuple + class ButtonEvent(NamedTuple): button: str pressed: bool seconds_since_last_event: float coordinates: Optional[Tuple[int, int]] + class ButtonEventInput(Iterable[ButtonEvent]): def __iter__(self) -> Iterator[ButtonEvent]: with suppress(EOFError): diff --git a/setup.py b/setup.py index a493572..fbe66db 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="botable", - version="0.0.1", + version="0.0.2", packages=["botable"], url="http://github.com/ebonnal/botable", license="Apache 2.", @@ -11,4 +11,5 @@ description="[Python][CLI] Record and play keyboard and mouse clicks.", long_description=open("README.md").read(), long_description_content_type="text/markdown", + install_requires=["pynput"], ) diff --git a/tests/test.py b/tests/test.py index 5e02f05..a10bbbc 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,8 +1,6 @@ import unittest -from botable.button_event import * - - +from botable.button_event import * class Test(unittest.TestCase): def test(self):