Skip to content

Commit

Permalink
pause fater sleep and refac
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Dec 1, 2024
1 parent 7b0de39 commit 45e3abc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
5 changes: 3 additions & 2 deletions botable/botable.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ def on_press(key: keyboard.Key):
if loops > 1 and not loop_index:
collected_button_events.append(button_event)

while continue_ and paused_at:
time.sleep(PAUSE_SLEEP_INCREMENT)

if loop_index == 0 and offset > event_index:
continue
Expand Down Expand Up @@ -190,6 +188,9 @@ def on_press(key: keyboard.Key):

time.sleep(button_event.pre_sleep / rate)

while continue_ and paused_at:
time.sleep(PAUSE_SLEEP_INCREMENT)

if button_event.pressed:
ctrl.press(evaluated_button)
else:
Expand Down
43 changes: 27 additions & 16 deletions build/lib/botable/botable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
from pynput.keyboard import Key, KeyCode # type: ignore


RECORDING = False
PLAYING = False
PAUSE_SLEEP_INCREMENT = 0.5
POST_PLAY_SLEEP = 0.2


class ButtonEvent(NamedTuple):
button: str
pressed: bool
seconds_since_last_event: float
pre_sleep: float
coordinates: Optional[Tuple[int, int]]


def input_button_events() -> Iterator[ButtonEvent]:
def stdin_button_events() -> Iterator[ButtonEvent]:
with suppress(EOFError):
while event := input():
yield ButtonEvent(*eval(event))


def _to_key(key: str) -> Union[Key, KeyCode, str]:
def str_to_key(key: str) -> Union[Key, KeyCode, str]:
try:
return eval(f"Key.{key}")
except Exception:
Expand All @@ -33,10 +39,6 @@ def _to_key(key: str) -> Union[Key, KeyCode, str]:
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 All @@ -54,8 +56,8 @@ def record(exit_key: str = "f1", pause_key: str = "f2") -> Iterator[ButtonEvent]
button_events: Deque[ButtonEvent] = deque()
continue_ = True

exit_key_ = _to_key(exit_key)
pause_key_ = _to_key(pause_key)
exit_key_ = str_to_key(exit_key)
pause_key_ = str_to_key(pause_key)

def save_button_events(button: str, pressed: bool, position):
nonlocal paused_at, last_event_at
Expand Down Expand Up @@ -102,6 +104,10 @@ def on_click(x: int, y: int, button: mouse.Button, pressed: bool):
RECORDING = False


def add_noise(x: float) -> float:
return x * (1 + random.betavariate(2, 5) / 2)


def play(
button_events: Iterable[ButtonEvent],
exit_key: str = "f1",
Expand Down Expand Up @@ -134,8 +140,8 @@ def play(
mouse_ctrl = mouse.Controller()
keyboard_ctrl = keyboard.Controller()

exit_key_ = _to_key(exit_key)
pause_key_ = _to_key(pause_key)
exit_key_ = str_to_key(exit_key)
pause_key_ = str_to_key(pause_key)

def on_press(key: keyboard.Key):
nonlocal paused_at, continue_
Expand All @@ -156,7 +162,7 @@ def on_press(key: keyboard.Key):
collected_button_events.append(button_event)

while continue_ and paused_at:
time.sleep(0.5)
time.sleep(PAUSE_SLEEP_INCREMENT)

if loop_index == 0 and offset > event_index:
continue
Expand All @@ -174,10 +180,15 @@ def on_press(key: keyboard.Key):
else:
evaluated_button = eval(button_event.button)

sleep_duration = button_event.seconds_since_last_event / rate
if noise:
sleep_duration *= (1 + random.betavariate(2, 5) / 2)
time.sleep(sleep_duration)
button_event = ButtonEvent(
button=button_event.button,
pressed=button_event.pressed,
pre_sleep=add_noise(button_event.pre_sleep),
coordinates=button_event.coordinates,
)

time.sleep(button_event.pre_sleep / rate)

if button_event.pressed:
ctrl.press(evaluated_button)
Expand All @@ -191,6 +202,6 @@ def on_press(key: keyboard.Key):

button_events_ = collected_button_events

time.sleep(0.2)
time.sleep(POST_PLAY_SLEEP)
finally:
PLAYING = False

0 comments on commit 45e3abc

Please sign in to comment.