Skip to content

Instantly share code, notes, and snippets.

@ethack
Last active October 31, 2024 06:34
Show Gist options
  • Save ethack/110f7f46272447828352768e6cd1c4cb to your computer and use it in GitHub Desktop.
Save ethack/110f7f46272447828352768e6cd1c4cb to your computer and use it in GitHub Desktop.
Scripts that simulate typing the clipboard contents. Useful when pasting is not allowed.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#NoTrayIcon ; Hide the tray icon
^+v::Send {Raw}%Clipboard%
#!/bin/bash
xclip -selection clipboard -out | tr \\n \\r | xdotool selectwindow windowfocus type --clearmodifiers --delay 25 --window %@ --file -

It "types" the contents of the clipboard.

Why can't you just paste the contents you ask? Sometimes pasting just doesn't work.

  • One example is in system password fields on OSX.
  • Sometimes you're working in a VM and the clipboard isn't shared.
  • Other times you're working via Remote Desktop and again, the clipboard doesn't work in password boxes such as the system login prompts.
  • Connected via RDP and clipboard sharing is disabled and so is mounting of local drives. If the system doesn't have internet access there's no easy way to get things like payloads or Powershell scripts onto it... until now.

Windows

The Windows version is written in AutoHotKey and easily compiles to an executable. It's a single line script that maps Ctrl-Shift-V to type the clipboard.

^+v::Send {Raw}%Clipboard%

Linux

The following should work on Linux, provided you have xdotool and xclip installed. This version lets you select the window you want to send the keystrokes to.

xclip -selection clipboard -out | tr \\n \\r | xdotool selectwindow windowfocus type --clearmodifiers --delay 25 --window %@ --file -

Explanation of the flags used:

  • xclip -selection clipboard gets the contents of the clipboard.
  • -out writes the text to stdout.
  • tr \\n \\r replaces newlines with carriage returns to ensure they don't get missed in some applications.
  • selectwindow allows you to pick a window to send text to. This means you don't have to have the window active when you run the command.
  • windowfocus focuses the selected window. Most apps I tried would ignore keystroke events if they weren't in focus.
  • --clearmodifiers makes sure that no modifier keys are pressed before typing.
  • --delay 25 was the best balance between speed and not missing keystrokes in the applications I was using. This shouldn't be noticable for short text, but makes a difference with longer text.
  • --window $@ means that keystrokes will only be sent to that window. If you focus a different window the typed keystrokes won't suddenly be sent to your new window.
  • --file - reads from stdin.

The following is a version that just "pastes" immediately to the active window.

xclip -selection clipboard -out | tr \\n \\r | xdotool type --clearmodifiers --delay 25 --file -

I saved this to a script and then mapped the script to a hotkey using Gnome's custom keyboard shortcuts.

OSX

The Mac version is writtern in AppleScript.

tell application "System Events" to keystroke the clipboard as text

The equivalent one-liner from the command line would be:

osascript -e 'tell application "System Events" to keystroke the clipboard as text'

To bind this to a keyboard shortcut you have several options. Sticking with builtin OSX utilities you can follow this guide.

Otherwise, you can use a third party program that lets you set custom hotkeys such as: BetterTouchTool, Keyboard Maestro, or Hammerspoon

Credits:

  • @Indigo744 for the suggestion to use {Raw} in the Windows version
  • @L3vi47h4N for the Linux version
  • @brabster for the --clearmodifiers suggestion in the Linux version
@ethack
Copy link
Author

ethack commented Sep 1, 2019

@haakonstorm if the special characters are in the clipboard they get typed correctly. For instance if I copy "text text2" and then use the script to type the clipboard it types it out as "text text2" (where the actual tab character is between the text). If you're looking to define a string with escape sequences like "\t" or "\n" in Applescript then I would have tried \t like you did as well. Or look at if you can define characters as hex codes or something.

@neillindberg
Copy link

I feel I've found knowledge around my mission - I just want to add a right-click option on my Windblows context menu that will paste my email as I need to type it a zillion times per day. I've gotten the item added, but how to tell this system to paste some certain text? I think I've even confused Dr. Google and his cousin Stack Overflow, M.D.

@brabster
Copy link

Suggest --clearmodifiers flag on the Linux version - I was getting random characters omitted from what was pasted without it.

sh -c 'sleep 1.0; xdotool type --clearmodifiers "$(xclip -o -selection clipboard)"'

@FloPinguin
Copy link

Thanks for the great ahk version! My usecase: Some websites are blocking pasting in input fields (For example credit card information). And I don't have time to unblock it with developer tools or typing it manually.

@TrevCan
Copy link

TrevCan commented Jul 24, 2021

Thanks for making this!, I will add an option (maybe -p or something) to be able to use a password manager such as pass ( https://www.passwordstore.org/) to type out my password, i know pass already has a script that uses it but it's too long (passmenu --type)!

@TrevCan
Copy link

TrevCan commented Jul 24, 2021

Hey @ethack , I have added some new things to my fork (https://gist.github.com/TrevCan/8af5e1f8ff15f76d8f1b0ea71ecaa64e), including support for the pass password manager, --help usage section, as well as a proper license. You might want to check it out in case you'd like to merge.

@clydeevans393
Copy link

I can't get this to type anything that requires SHIFT. On a mac, it will work perfectly. When going from Mac clipboard to PC (windows 10) It doesn't "hit" SHIFT for capitals. Is there something I'm missing?

@641i130
Copy link

641i130 commented Mar 31, 2022

Adding this after the Linux one liner helped me with a ton of headache. (Only modifier keys would feel like they're being held down still).

sleep 0.2; xdotool keyup Control_L Control_R Shift_L Shift_R Meta_L Meta_R

@clydeevans393
Copy link

sleep 0.2; xdotool keyup Control_L Control_R Shift_L Shift_R Meta_L Meta_R

Im operating from a Mac. So I'm using tell application "System Events" to keystroke (the clipboard as text)

I dont think this will work together unless Im missing something. Im new to scripts. Thanks for the reply!

@kevin-david
Copy link

kevin-david commented Jan 8, 2023

On Mac, if it's not obvious - you want a new "Quick Action" in Automator. I guess things have changed a bit in the past 13 years for that app!

It also took me a lot of searching to figure out the right Security & Privacy settings to get this working. I ended up adding the following services to the Accessibility section:

  • AppleScript Utility (see https://apple.stackexchange.com/a/401262/337265, Cmd + Shift + . to show hidden files was another useful Finder shortcut I learned along the way)
  • Automator
  • Script Editor
  • Firefox (the app where I was actually trying to paste)

Strangely, it's at least (temporarily) working when I turn them all off again... I really don't understand how/why these settings don't take effect immediately.

@remka10
Copy link

remka10 commented Mar 1, 2023

Is it possible to have a random delay between inputs? Иecause in my program if you type too fast you get thrown out, I guess this is a protection against such scripts...

@tatarize
Copy link

tatarize commented Apr 7, 2023

Rather than having somebody use Autohotkey or install anything there's a somewhat more generic solution. Often using tab delimited text to tab through fields (since they are keyboard sent). Keys like control wouldn't work for this method, but tab and enter and some others would if they have an Ascii version.

Procedure.

  1. We drag and dropped the text file on to the script
  2. We have three seconds to select the correct field we need
  3. It'll send that data over the keyboard.

VBScript Text-To-Keyboard

Dim WshShell, file_name, fso, ts, contents

WScript.Sleep 3000

file_name = WScript.Arguments(0)

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(file_name)
contents = ts.ReadAll
ts.Close

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys contents

Python: Text-To-Keyboard

import time
import sys
import pyautogui

time.sleep(3)

file_name = sys.argv[1]

with open(file_name, 'r') as f:
    contents = f.read()

pyautogui.typewrite(contents)

The python version requires pyautogui but is going to be much more cross platform.

AppleScript: Text-To-Keyboard

Untested applescript version, running by my mac guy to check but mostly asked ChatGPT for this super basic workflow.

-- Pause for three seconds to allow time to switch to the correct application
delay 3

-- Get the path to the dropped file
on open dropped_items
    set file_path to (POSIX path of dropped_items)
end open

-- Read the contents of the file
set file_data to read file_path as «class utf8»

-- Split the data into rows
set row_data to paragraphs of file_data

-- Remove the header row if present
if item 1 of row_data contains tab then
    set row_data to items 2 thru -1 of row_data
end if

-- Loop through the rows and send keystrokes to the system
repeat with this_row in row_data
    -- Split the row into individual fields
    set field_data to words of this_row
    
    -- Enter each field using the "keystroke" command
    repeat with this_field in field_data
        keystroke this_field
        keystroke tab
    end repeat
    
    -- Press "return" to submit the data
    keystroke return
end repeat

Also See:
https://gist.github.com/tatarize/875b870ff5cf96073bc9d895e8bf1b9f

@user52839307
Copy link

Mac OSX

An AppleScript that sets the clipboard contents to a variable clipboardText, then uses a repeat loop to iterate over each character in the variable and output them as keystrokes with a delay of 0.1 seconds using the delay command.

You can adjust the delay time to be shorter or longer by changing the value in the delay command.

-- Get the clipboard contents as text
set clipboardText to the clipboard as text

-- Use a repeat loop to iterate over each character in the clipboard text
tell application "System Events"
	repeat with i from 1 to length of clipboardText
		-- Output the current character as a keystroke
		keystroke (character i of clipboardText)
		
		-- Add a delay of 0.1 seconds between each keystroke
		delay 0.1
	end repeat
end tell

This version slows the output to simulate actual typing more accurately. It can be bound to a keyboard shortcut as described previously. I use the BetterTouchTool.

@tatarize
Copy link

OSX is a bit more locked down letting one script modify the actions of another program or whatever.

If without the right permissions:

  1. Click on the Apple menu in the top left corner of the screen and select "System Preferences".
  2. Click on "Security & Privacy".
  3. Select the "Privacy" tab.
  4. Scroll down to "Accessibility" in the left-hand column and click on it.
  5. Click the lock icon in the bottom left corner of the window to make changes.
  6. Enter your administrator password when prompted.
  7. Check the box next to the application you want to allow to control your computer using System Events (in this case, it would be your AppleScript editor or the "Script Editor" application).
  8. If the application you want to allow doesn't appear in the list, click the "+" button at the bottom of the window and select it from the Applications folder.
  9. Once you've made your selection, close the window and the changes should take effect immediately.
    Note that if you're using macOS Catalina or later, you may need to grant full disk access instead of just accessibility access, depending on the specific use case.

Not sure if that's the issue but I was told this was the issue when I asked about my version and mac script. Only the most ancient things seem to allow this without additional permissions.

@mrandreastoth
Copy link

Not a script but a really simple solution under Windows...

https://apps.microsoft.com/detail/9P5R4JK7R8H5?hl=en-us&gl=US

@mrandreastoth
Copy link

The following doesn't play ball with version 2.x of AutoHotKey...

I would suggest you to use

^+v::Send {Raw}%Clipboard%

Otherwise some expression will be interpreted... Like {Enter} to Enter, ^c to CTRL+C... See https://autohotkey.com/docs/commands/Send.htm

@peanutcracker1
Copy link

Is there any way to have this script only take over the ctrl+shift+v for certain window names? I only want it to do this behavior in certain programs. I'm on Windows by the way.

@brz
Copy link

brz commented Apr 11, 2024

The following doesn't play ball with version 2.x of AutoHotKey...

I would suggest you to use

^+v::Send {Raw}%Clipboard%

Otherwise some expression will be interpreted... Like {Enter} to Enter, ^c to CTRL+C... See https://autohotkey.com/docs/commands/Send.htm

The following will work with AHK 2.x:

#Requires AutoHotkey v2.0

^+v::
{
    SendText A_Clipboard
}

@DanielHGimenez
Copy link

You can use clipboard-paste-keyboard as an easy way.

@swizzyswizzy
Copy link

swizzyswizzy commented Oct 31, 2024

The following doesn't play ball with version 2.x of AutoHotKey...

I would suggest you to use

^+v::Send {Raw}%Clipboard%

Otherwise some expression will be interpreted... Like {Enter} to Enter, ^c to CTRL+C... See https://autohotkey.com/docs/commands/Send.htm

The following will work with AHK 2.x:

#Requires AutoHotkey v2.0

^+v::
{
    SendText A_Clipboard
}

This will also work with AutoHotKey 2.0 and prevent problems with special characters:

#Requires AutoHotkey v2.0
^+v::SendEvent "{Raw}" A_Clipboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment