[Python] Python "keyboard" module: Release keys to allow copy/Ctrl+C

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to simulate an existing AutoHotKey script using the "keyboard" module in Python (since the deployment story of AHK scripts seems to be even worse than Python scripts).

    In this script, I use a keyboard shortcut (Left Windows key+F12), which should trigger the "copy" command, allowing me to retrieve the copied text via pyperclip or win32clipboard, modify it, and replace the selected text by typing new text

    Here's the win32clipboard version of the code:

    import keyboard
    import win32clipboard

    def on_triggered():
    keyboard.press_and_release('ctrl+c')
    win32clipboard.OpenClipboard()
    new_data = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    keyboard.write(new_data + ' duuuuude.')

    keyboard.add_hotkey('left windows+F12', on_triggered)


    I've simplified the script above, removing some of the housekeeping code (saving, clearing and restoring the existing clipboard contents).

    From what I can tell, when "ctrl+c" is triggered, "left windows+F12" is still held down, and the text never actually copies. Is there a way to tell the "keyboard" library to ignore all current keyboard input? Or am I taking the wrong approach?

    Continue reading...

Compartilhe esta Página