Skip to content

Commit

Permalink
cd ok / dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Nov 30, 2024
1 parent 3d43d18 commit b3edc66
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
12 changes: 9 additions & 3 deletions botable/botable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -164,7 +170,7 @@ def on_press(key: keyboard.Key):
ctrl.press(evaluated_button)
else:
ctrl.release(evaluated_button)

if not continue_:
return

Expand Down
2 changes: 2 additions & 0 deletions botable/button_event.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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"],
)
4 changes: 1 addition & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest

from botable.button_event import *


from botable.button_event import *

class Test(unittest.TestCase):
def test(self):
Expand Down

0 comments on commit b3edc66

Please sign in to comment.