Add alternate option for alternating between clicks

This commit is contained in:
Magnus Walbeck 2023-05-21 20:25:21 +02:00
parent 8f297f5be7
commit f5f802e8bd
Signed by: mwalbeck
GPG key ID: CCB78CFF3F950769
3 changed files with 64 additions and 4 deletions

View file

@ -1 +1,3 @@
__version__ = "0.1.1"
from importlib.metadata import version
__version__ = version(__package__)

View file

@ -45,6 +45,13 @@ def setup_arguments():
action="store_true",
help="Don't send clicks to the select window when that window is focused.",
)
parser.add_argument(
"-a",
"--alternate",
choices=["1", "2", "3"],
nargs=2,
help="Alternate between two different types of clicks",
)
return parser
@ -69,9 +76,23 @@ def main():
else:
window = select_window()
click = {"click_id": args.type, "click_name": CLICK_NAMES[args.type]}
if args.alternate is None:
click = {"click_id": args.type, "click_name": CLICK_NAMES[args.type]}
do_click(window, click, args.interval, args.ignore_when_focused)
do_click(window, click, args.interval, args.ignore_when_focused)
else:
clicks = [
{
"click_id": args.alternate[0],
"click_name": CLICK_NAMES[args.alternate[0]],
},
{
"click_id": args.alternate[1],
"click_name": CLICK_NAMES[args.alternate[1]],
},
]
do_clicks(window, clicks, args.interval, args.ignore_when_focused)
def select_window():
@ -167,6 +188,43 @@ def do_click(window, click, interval, ignore_when_focused):
next_time += (time.time() - next_time) // interval * interval + interval
def do_clicks(window, clicks, interval, ignore_when_focused):
print(
f"Sending {clicks[0]['click_name']}s and {clicks[1]['click_name']}s to {window['window_name']}, alternating between them every {interval}ms."
)
interval = interval / 1000
next_time = time.time() + interval
current_click = 0
while True:
time.sleep(max(0, next_time - time.time()))
if ignore_when_focused:
active_window = (
subprocess.run(["xdotool", "getactivewindow"], capture_output=True)
.stdout.strip()
.decode("UTF-8")
)
logging.debug(f"active window id: {active_window}")
logging.debug(f"sending clicks to window id: {window['window_id']}")
if active_window == window["window_id"]:
next_time += (time.time() - next_time) // interval * interval + interval
logging.info("click skipped")
continue
send_click(window, clicks[current_click])
if current_click == 0:
current_click = 1
else:
current_click = 0
next_time += (time.time() - next_time) // interval * interval + interval
def send_click(window, click):
# Send mouse down and up separately to prevent wrong mouse inputs from being
# sent when those inputs are press on the actual mouse at the same time a click

View file

@ -1,6 +1,6 @@
[tool.poetry]
name = "autoklik"
version = "0.1.1"
version = "0.2.0"
description = "Auto clicker built around xdotool."
authors = ["Magnus Walbeck <mw@mwalbeck.org>"]
readme = "README.md"