Skip to content

Instantly share code, notes, and snippets.

@ghamarian
Created October 7, 2023 13:28
Show Gist options
  • Select an option

  • Save ghamarian/1e71140ce87742d67578078d462df62e to your computer and use it in GitHub Desktop.

Select an option

Save ghamarian/1e71140ce87742d67578078d462df62e to your computer and use it in GitHub Desktop.
from pynput import keyboard
import pyperclip
import time
# Initialize
with open("your_file.txt", "r") as f:
lines = f.readlines()
# Remove newline characters
lines = [line.strip() for line in lines]
index = 0
def on_activate():
global index
if index < len(lines):
time.sleep(0.2) # Introduce a 200ms delay
pyperclip.copy(lines[index])
index += 1
# Set up clipboard with the first line
pyperclip.copy(lines[0])
index = 1
# The key combination to check (Command + V)
COMBINATION = {keyboard.Key.cmd, keyboard.KeyCode.from_char("v")}
# The currently active modifiers
current_keys = set()
def on_press(key):
if key in COMBINATION:
current_keys.add(key)
if all(k in current_keys for k in COMBINATION):
on_activate()
current_keys.clear() # Clear the set after activation
def on_release(key):
try:
current_keys.remove(key)
except KeyError:
pass # Key was not in the set, ignore
# Function to start listening for Command + V
def start_listener():
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
# Function to stop listener when F1 is pressed
def on_press_trigger(key):
if key == keyboard.Key.f1:
return False # Stop listener
# Wait for a trigger key (e.g., F1) to start the listener
print("Press F1 to start listening for Command + V")
with keyboard.Listener(on_press=on_press_trigger) as listener:
listener.join()
# Start main listener
start_listener()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment