Automatic Clipboard Typer

 Β·  β˜• 3 min read
πŸ”–
  • Garden Shed
  • For when Copy & Paste just won’t cut it


    This script is designed to simulate keyboard key presses in order to type out (rather then paste) the current clipboard.

    The inspiration for this script came from a need to paste into fields that cannot normally be pasted into, e.g. an RDP session, particularly when asked for the windows password or resetting one.

    Usage:

    1. Copy the text you want to paste to the clipboard.
    2. Run the script (a command-line window will appear)
    3. Select the text field you want to paste in to
    4. After X seconds, the text will be typed out and the command-line window will close

    The duration between the script starting and the clipboard being typed can be customised with the WAIT_TIME variable below.
    This value should be long enough so that you can give focus to the text field you want it to type into after running the script.

    Creating a shortcut:

    It is possible to run the script via a keyboard shortcut.

    1. Create a shortcut (e.g. on the desktop)
    2. For the target: β€˜<path to Python executable> <path to this script>’
    3. For the shortcut key: Any key combination (the author uses β€˜Shift + Alt + P’)

    This script was developed for Windows-based machines and obviously requires python.

    Also, full disclosure: I cobbled this together from a number of Stack Overflow posts (that I have since lost the link to).


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    
    WAIT_TIME = 5  # Seconds
    
    try:
        # Python2
        import Tkinter as tk
    except ImportError:
        # Python3
        import tkinter as tk
    
    
    def getClipboardText():
        root = tk.Tk()
        # keep the window from showing
        root.withdraw()
        return root.clipboard_get()
    
    
    KEYEVENTF_UNICODE = 0x0004
    KEYEVENTF_KEYUP = 0x0002
    
    import ctypes
    import time
    
    SendInput = ctypes.windll.user32.SendInput
    
    PUL = ctypes.POINTER(ctypes.c_ulong)
    
    
    class KeyBdInput(ctypes.Structure):
        _fields_ = [
            ("wVk", ctypes.c_ushort), ("wScan", ctypes.c_ushort), ("dwFlags", ctypes.c_ulong),
            ("time", ctypes.c_ulong), ("dwExtraInfo", PUL)
        ]
    
    
    class HardwareInput(ctypes.Structure):
        _fields_ = [("uMsg", ctypes.c_ulong), ("wParamL", ctypes.c_short), ("wParamH", ctypes.c_ushort)]
    
    
    class MouseInput(ctypes.Structure):
        _fields_ = [
            ("dx", ctypes.c_long), ("dy", ctypes.c_long), ("mouseData", ctypes.c_ulong),
            ("dwFlags", ctypes.c_ulong), ("time", ctypes.c_ulong), ("dwExtraInfo", PUL)
        ]
    
    
    class Input_I(ctypes.Union):
        _fields_ = [("ki", KeyBdInput), ("mi", MouseInput), ("hi", HardwareInput)]
    
    
    class Input(ctypes.Structure):
        _fields_ = [("type", ctypes.c_ulong), ("ii", Input_I)]
    
    
    def PressKey(KeyUnicode):
        extra = ctypes.c_ulong(0)
        ii_ = Input_I()
        ii_.ki = KeyBdInput(0, KeyUnicode, KEYEVENTF_UNICODE, 0, ctypes.pointer(extra))
        x = Input(ctypes.c_ulong(1), ii_)
        ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
    
    
    def ReleaseKey(KeyUnicode):
        extra = ctypes.c_ulong(0)
        ii_ = Input_I()
        ii_.ki = KeyBdInput(
            0, KeyUnicode, KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, 0, ctypes.pointer(extra)
        )
        x = Input(ctypes.c_ulong(1), ii_)
        ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
    
    
    if __name__ == "__main__":
        # user32 = ctypes.windll.user32
        # user32.keybd_event(0x12, 0, 0, 0)  #Alt
        # time.sleep(0.01)
        # user32.keybd_event(0x09, 0, 0, 0)  #Tab
        # time.sleep(0.01)
        # user32.keybd_event(0x09, 0, 2, 0)  #~Tab
        # time.sleep(0.01)
        # user32.keybd_event(0x12, 0, 2, 0)  #~Alt
    
        print("typing in...")
    
        for sec in range(WAIT_TIME):
            print(WAIT_TIME - sec)
            time.sleep(1)
    
        clipboard = getClipboardText()
        letters = [i for i in clipboard]
        for letter in letters:
            PressKey(ord(letter))
            ReleaseKey(ord(letter))
            time.sleep(0.01)
    

    Kieran Goldsworthy
    WRITTEN BY
    Kieran Goldsworthy
    Cloud Engineer and Architect