diff --git a/README.md b/README.md index 288a7290..48b2d3da 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,52 @@ ahk.mouse_move(x=100, y=100, speed=10) # blocks until mouse finishes moving print(ahk.mouse_position) # (100, 100) ``` +You can also opt for a non-blocking interface, so you can do other stuff while AHK scripts run. + +```python +import time +from ahk import AHK +ahk = AHK() +ahk.mouse_position = (200, 200) # moves the mouse instantly to the position +start = time.time() +ahk.mouse_move(x=100, y=100, speed=30, blocking=False) +while True: + t = round(time.time() - start, 4) + position = ahk.mouse_position + print(t, position) + if position == (100, 100): + break +``` + +You should see an output something like + +``` +0.032 (187, 187) +0.094 (173, 173) +0.137 (164, 164) +0.1769 (156, 156) +0.2209 (151, 150) +0.2648 (144, 144) +0.3068 (137, 138) +0.3518 (131, 134) +0.3928 (124, 129) +0.4338 (117, 126) +0.4828 (110, 122) +0.534 (104, 119) +0.579 (100, 117) +0.621 (100, 114) +0.663 (100, 112) +0.704 (100, 109) +0.745 (100, 106) +0.788 (100, 103) +0.831 (100, 101) +0.873 (100, 100) +``` + +You should also take note that communication with ahk takes a *little* bit of time; about 0.05 seconds in +the case of `mouse_position`. YYMV. +This is subject to improvement as the implementation changes. + # Installation ``` diff --git a/ahk/mouse.py b/ahk/mouse.py index c1a62143..842c02e9 100644 --- a/ahk/mouse.py +++ b/ahk/mouse.py @@ -39,7 +39,7 @@ def mouse_position(self, position): x, y = position self.mouse_move(x=x, y=y, speed=0, relative=False) - def _mouse_move(self, x=None, y=None, speed=None, relative=False, mode=None): + def _mouse_move(self, x=None, y=None, speed=None, relative=False, mode=None, persistent=True, blocking=True): if x is None and y is None: raise ValueError('Position argument(s) missing. Must provide x and/or y coordinates') if speed is None: @@ -61,9 +61,10 @@ def _mouse_move(self, x=None, y=None, speed=None, relative=False, mode=None): script = make_script(f''' CoordMode Mouse, {mode} MouseMove, {x}, {y} , {speed}{relative} - ''') + ''', persistent=persistent, blocking=blocking) return script def mouse_move(self, *args, **kwargs): + blocking = kwargs.get('blocking') script = self._mouse_move(*args, **kwargs) - self.run_script(script_text=script) + self.run_script(script, blocking=blocking) diff --git a/ahk/script.py b/ahk/script.py index 36850112..423446bd 100644 --- a/ahk/script.py +++ b/ahk/script.py @@ -4,7 +4,7 @@ from contextlib import suppress from shutil import which from ahk.utils import logger - +import time class ScriptEngine(object): def __init__(self, executable_path: str='', keep_scripts: bool=False, **kwargs): @@ -21,13 +21,19 @@ def __init__(self, executable_path: str='', keep_scripts: bool=False, **kwargs): self.executable_path = executable_path def _run_script(self, script_path, **kwargs): + blocking = kwargs.pop('blocking', True) runargs = [self.executable_path, script_path] decode = kwargs.pop('decode', False) - result = subprocess.run(runargs, stdin=None, stderr=None, stdout=subprocess.PIPE, **kwargs) - if decode: - return result.stdout.decode() + if blocking: + result = subprocess.run(runargs, stdin=None, stderr=None, stdout=subprocess.PIPE, **kwargs) + if decode: + return result.stdout.decode() + else: + return result.stdout else: - return result.stdout + p = subprocess.Popen(runargs, stdout=subprocess.PIPE, **kwargs) + p.stdout.readline() # give script a chance to read the script or else we'll delete it too quick + return p def run_script(self, script_text: str, delete=None, decode=True, **runkwargs): if delete is None: diff --git a/ahk/utils.py b/ahk/utils.py index 018b08a6..22d692da 100644 --- a/ahk/utils.py +++ b/ahk/utils.py @@ -14,7 +14,7 @@ logger.setLevel(logging.ERROR) -def make_script(body, directives=None, persistent=True): +def make_script(body, directives=None, persistent=True, blocking=True): """ Convenience function to dedent script body as well as add #Persistent directive and Exit/ExitApp :param body: body of the script @@ -39,4 +39,6 @@ def make_script(body, directives=None, persistent=True): {body} {exit_} ''') + if not blocking: + script = 'FileAppend, "`r`n", *\n' + script return script