I have something in my clipboard, and I'd like to run a python script that invokes CTRL+V as if it was pressed on the keyboard, and pastes the clipboard's content to the current focused window (say chrome).
Any idea how to do that?

- 97
- 7
-
What type of system are you on? – JoshKopen Aug 10 '17 at 19:33
-
@JoshKopen operating system? windows. – Dina Benyamin Aug 10 '17 at 19:33
-
1https://stackoverflow.com/questions/101128/how-do-i-read-text-from-the-windows-clipboard-from-python – JoshKopen Aug 10 '17 at 19:34
-
@JoshKopen If I understood correctly, this allows me to get the data in the clipboard, not paste it to another open notepad or chrome for instance. – Dina Benyamin Aug 10 '17 at 19:35
-
Haven't tried this myself but have you looked at [this](https://pypi.python.org/pypi/PyAutoGUI)? – bergerg Aug 10 '17 at 19:36
-
Well I mean that gives you the clipboard data, the next step would be to look up how to post to a different place. For example, if you were going to post to a notepad text file: https://stackoverflow.com/questions/5214578/python-print-string-to-text-file – JoshKopen Aug 10 '17 at 19:37
-
This is going to be very hard to do. It raises all kinds of security concerns and generally things like windows and chrome are rather hostile when it come to letting you do that type of thing. Its possible -- but probably much harder than it sounds. If a program doesn't open an API to input data, you have to spoof mouse and keyboard, and windows intentionally makes spoofing mouse and keybaord very hard to do. Chrome doesnt give much programti access to its inner workings because it would be a security target -- I think you have an [X Y Problem](http://xyproblem.info/). – gbtimmon Aug 10 '17 at 19:40
1 Answers
You have an X-Y problem.
What you want to accomplish is programmatically take data from one program (where you hit cntrl-V) and place it into another arbitrary program (chrome).
There are two ways to do that:
First
You can either set the programs up to have a data exchange mechanism such as a system pipe, or a network connection. This requires some API for data exchange to be already included in the program or access to the source so you might add one. There are very specific channels for cross program data exchange and you wont do well to try to circumvent them. Program A cant just say
get_program_b().get_text_box().add(clip_board);
That would be a violation of Process Isolaton and an OS like windows is written expressly to make it impossible. Some programs are designed to take input from other programs.
popen.open('mysql -e "ISNERT INTO table (a) VALUES ('4')")
Chrome is not one of those programs, chrome avoids allowing programs from doing this because it would be a target for programs to do things like, get the saved password or credit card data out of chrome. Or use save password to login to someone account and buy things in someone elses name.
Second
You could try to spoof user input and input the data exactly like a user would so chrome wont know the difference. But spoofing a user is hard to do and intentionally so because it prevents malicious scripts from taking control of a computer and doing bad things. The makers of windows are accutely aware that spoofing input is a method to circumvent allowed data exchange channels. So the makers of windows made it hard to do. You need to have access to a lot of system assets that most programs wont be given. At a minimum a program has to run as admin on windows to accomplish this, then there are libs that will let you do it. Even then Im willing to bet there are easier way to get the job done. On a machine where you have access to anything and everything it is possible. If you don't have admin access, it should be downright impossible without knowing some unpatched exploit in the system.
Therefore
What you are trying to do goes against what the computer was designed to let you do. If we had more information on what you want to accomplish maybe some of the wonderful people here could help. Getting to the end result you want shouldnt be that hard. But you way of doing it is like trying to run across the ocean, when you just need a boat. As it is my answer is -- dont do it, that's not how windows was designed to work.

- 4,238
- 1
- 21
- 36