aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2024-11-17 17:11:39 +1300
committerTom Ryder <tom@sanctum.geek.nz>2024-11-17 17:19:00 +1300
commit1364fafee00ec00370c80f85a27c65863d536ad2 (patch)
tree15a3a3b508bea3ea534514bcac519714b0fa35a1
parentSimplify/clarify editor selection logic (diff)
downloadvixf-1364fafee00ec00370c80f85a27c65863d536ad2.tar.gz
vixf-1364fafee00ec00370c80f85a27c65863d536ad2.zip
Refactor keyboard automation
-rw-r--r--vixf.py323
1 files changed, 11 insertions, 12 deletions
diff --git a/vixf.py3 b/vixf.py3
index a2c171f..d9e791c 100644
--- a/vixf.py3
+++ b/vixf.py3
@@ -13,7 +13,7 @@ import subprocess
import tempfile
import time
-import pynput
+from pynput.keyboard import Key, Controller
import pyperclip
SELF = 'vixf'
@@ -22,19 +22,18 @@ SLEEP = 0.5
EDITOR = 'vi'
-keyboard = pynput.keyboard.Controller()
+keyboard = Controller()
time.sleep(SLEEP)
-ctrl = pynput.keyboard.Key.ctrl
+# Select all
+with keyboard.pressed(Key.ctrl):
+ keyboard.type('a')
-keyboard.press(ctrl)
-keyboard.type('a')
-keyboard.release(ctrl)
+# Copy
+with keyboard.pressed(Key.ctrl):
+ keyboard.type('c')
-keyboard.press(ctrl)
-keyboard.type('c')
-keyboard.release(ctrl)
with tempfile.NamedTemporaryFile(mode='w', delete_on_close=False) as tf:
tf.write(pyperclip.paste())
@@ -66,6 +65,6 @@ pyperclip.copy(content)
time.sleep(SLEEP)
-keyboard.press(pynput.keyboard.Key.ctrl)
-keyboard.type('v')
-keyboard.release(pynput.keyboard.Key.ctrl)
+# Paste
+with keyboard.pressed(Key.ctrl):
+ keyboard.type('v')