fmt + script
This commit is contained in:
5 files changed
+85
-6
No files matched your search
@@ -0,0 +1,71 @@
|
||||
# /// script
|
||||
# dependencies = [
|
||||
# "pynput",
|
||||
# "pyautogui",
|
||||
# ]
|
||||
# ///
|
||||
|
||||
# uv likes this block
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
import pynput
|
||||
|
||||
controller = pynput.keyboard.Controller()
|
||||
|
||||
|
||||
type Key = pynput.keyboard.Key | pynput.keyboard.KeyCode | None
|
||||
type ListenerFn = Callable[[Key], bool | None]
|
||||
|
||||
|
||||
def h() -> tuple[ListenerFn, ListenerFn]:
|
||||
a_first = True
|
||||
held = False
|
||||
|
||||
A = pynput.keyboard.KeyCode.from_char("a")
|
||||
D = pynput.keyboard.KeyCode.from_char("d")
|
||||
TRIGGER = pynput.keyboard.KeyCode.from_char("\\")
|
||||
FLIP = pynput.keyboard.KeyCode.from_char("/")
|
||||
QUIT = pynput.keyboard.KeyCode.from_char("'")
|
||||
|
||||
def order(do):
|
||||
if a_first:
|
||||
do(A)
|
||||
do(D)
|
||||
else:
|
||||
do(D)
|
||||
do(A)
|
||||
|
||||
def funny(key: Key, press: bool) -> bool | None:
|
||||
nonlocal a_first, held
|
||||
|
||||
if key == TRIGGER:
|
||||
if press and not held:
|
||||
order(controller.press)
|
||||
held = True
|
||||
|
||||
elif not press and held:
|
||||
order(controller.release)
|
||||
held = False
|
||||
|
||||
elif key == FLIP and press:
|
||||
a_first = not a_first
|
||||
print(f"flipped a_first to {a_first}")
|
||||
|
||||
elif key == QUIT and press:
|
||||
return False
|
||||
|
||||
return (
|
||||
lambda key: funny(key, True),
|
||||
lambda key: funny(key, False),
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
press, release = h()
|
||||
with pynput.keyboard.Listener(on_press=press, on_release=release) as listener:
|
||||
listener.join()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user