Wednesday, January 1, 2025

clipboard queue

Create a python code so that when I select a text and click on ctrl and m keys, it should add to a queue. each time I clicked ctrl and m keys at a time, it should add that text to queue and when I pressed ctrl and j at a time, it should paste from that queue and remove that entry from queue.


import keyboard
from collections import deque
import pyperclip
import time

# Initialize the queue
text_queue = deque()

def add_to_queue():
    """Add selected text to queue"""
    # Get the current clipboard content
    current_clipboard = pyperclip.paste()
    
    # Small delay to allow the copy operation to complete
    time.sleep(0.1)
    
    # Simulate Ctrl+C to copy selected text
    keyboard.send('ctrl+c')
    
    # Small delay to ensure the clipboard is updated
    time.sleep(0.1)
    
    # Get the selected text from clipboard
    selected_text = pyperclip.paste()
    
    # Add to queue if text is not empty
    if selected_text:
        text_queue.append(selected_text)
        print(f"Added to queue: {selected_text}")
    
    # Restore original clipboard content
    pyperclip.copy(current_clipboard)

def paste_from_queue():
    """Paste and remove text from queue"""
    if text_queue:
        # Get and remove the first item from queue
        text = text_queue.popleft()
        
        # Store current clipboard content
        current_clipboard = pyperclip.paste()
        
        # Copy text to clipboard
        pyperclip.copy(text)
        
        # Simulate Ctrl+V to paste
        keyboard.send('ctrl+v')
        
        # Small delay before restoring clipboard
        time.sleep(0.1)
        
        # Restore original clipboard content
        pyperclip.copy(current_clipboard)
        
        print(f"Pasted and removed from queue: {text}")
    else:
        print("Queue is empty!")

def main():
    # Register keyboard shortcuts
    keyboard.add_hotkey('ctrl+m', add_to_queue)
    keyboard.add_hotkey('ctrl+y', paste_from_queue)
    
    print("Queue System Started!")
    print("Use Ctrl+M to add selected text to queue")
    print("Use Ctrl+Y to paste and remove from queue")
    
    # Keep the script running
    keyboard.wait('esc')  # Press ESC to exit the script

if __name__ == "__main__":
    main()

No comments:

Post a Comment