#!/usr/bin/env python3 """ 1/2 Lens VAR -- physical button listener. Turns a press on a paired Bluetooth remote (or any USB button that types a key) into a "save the last 30 seconds" replay. The button is JUST a button -- it sends a keystroke; this script hears that keystroke and calls the app's replay endpoint. press button -> keystroke -> this script -> GET /api/replay -> 30s clip -------------------------------------------------------------------- STEP 1 - find out which key your remote sends (run once): python3 button_listener.py --learn ...then press the remote. It prints the key name. Copy it. STEP 2 - run it for real with that key (example: the remote sends "enter"): python3 button_listener.py --key enter Leave it running. Every press now makes a clip. -------------------------------------------------------------------- Install (one time): pip install pynput --break-system-packages """ import argparse, sys, time, json, urllib.request def fire(url, timeout=8): try: with urllib.request.urlopen(url, timeout=timeout) as r: data = json.loads(r.read().decode() or "{}") watch = data.get("watchUrl") or data.get("url") or "" print(f" ✔ clip saved {watch}") except Exception as e: print(f" ✘ could not reach the app ({e}). Is it running on this machine?") def keyname(key): """A stable, lowercase name for a pressed key (works for letters and special keys).""" try: if hasattr(key, "char") and key.char is not None: return key.char.lower() except Exception: pass return str(key).replace("Key.", "").lower() def main(): ap = argparse.ArgumentParser() ap.add_argument("--url", default="http://localhost:4000/api/replay", help="the app's replay endpoint (default: this machine, port 4000)") ap.add_argument("--key", default="enter", help="the key your remote sends (see --learn). e.g. enter, space, media_volume_up") ap.add_argument("--debounce", type=float, default=2.0, help="seconds to ignore repeat presses (stops double clips)") ap.add_argument("--learn", action="store_true", help="just print the name of every key you press, then quit") args = ap.parse_args() try: from pynput import keyboard except ImportError: sys.exit("Missing package. Run: pip install pynput --break-system-packages") if args.learn: print("LEARN MODE — press your remote's button (Ctrl+C to stop)...") def on_press(key): print(f" key = {keyname(key)!r} (use: --key {keyname(key)})") with keyboard.Listener(on_press=on_press) as l: l.join() return target = args.key.lower() last = [0.0] print(f"1/2 Lens VAR button ready. Listening for key '{target}'.") print(f"Replay endpoint: {args.url}") print("Press the button to make a 30s clip. (Ctrl+C to stop.)") def on_press(key): if keyname(key) == target: now = time.time() if now - last[0] < args.debounce: return last[0] = now print(f"[{time.strftime('%H:%M:%S')}] button pressed -> making replay...") fire(args.url) with keyboard.Listener(on_press=on_press) as l: l.join() if __name__ == "__main__": main()