Use non-blocking (asynchronous) mode
mainBy default, all ahk methods are blocking, meaning the Python script waits for the AHK command to finish. To run commands in the background while your Python script continues, use blocking=False.
Key Characteristics of Non-blocking calls:
- They return a
FutureResultobject. - They are isolated in a new AHK process that terminates after completion.
- They do not inherit previous global state changes (like
set_coord_mode). - You can wait for completion using
future_result.result(timeout=N).
Example: Moving mouse while tracking position
import time
from ahk import AHK
ahk = AHK()
ahk.mouse_position = (200, 200)
# Start moving mouse in background
future = ahk.mouse_move(x=100, y=100, speed=30, blocking=False)
# Python continues immediately
while True:
pos = ahk.mouse_position
print(f"Current position: {pos}")
if pos == (100, 100):
break
# Ensure the background task is finished
future.result(timeout=10)from ahk import AHK
ahk = AHK()
future_result = ahk.mouse_move(100, 100, speed=40, blocking=False)
# ... do other work ...
future_result.result(timeout=10)