0

I'm trying to do something along the lines of this:

   #setup stuff
   from Tkinter import Tk
   clp=Tk()
   clp.withdraw()   #keep window from showing
   clp.clipboard_clear()
   stuff=['this','is','stuff'])
   count = 0

   while 1:
       if(clp.userPastesClipboard()):   #is there a some sort of function for this?
           if(count <= len(stuff)):
               clp.clipboard_append(stuff[count])
               count += 1
           else:
               exit()

I basically just want to be able to trigger an if/while statement when the user pastes from the clipboard (to any application). I don't have to use the Tkinter library, so if there's a better library to use for this let me know, though if I can do it with Tkinter that's even better. Using Python 2.7. I prefer to use standard libraries if possible, but will use 3rd party if absolutely necessary. Thanks.

Futuza
  • 144
  • 8
  • 1
    Are you asking when they paste into the tkinter app, or when they paste in any app? – Bryan Oakley Apr 30 '16 at 01:15
  • You can't do that with tkinter. – Bryan Oakley Apr 30 '16 at 02:28
  • Know another library I can use that'd get the same thing done and maybe an example of using it this way? – Futuza Apr 30 '16 at 02:28
  • There's definitely no standard library that handles this, but https://github.com/asweigart/pyperclip will probably get you pretty close. – Slater Victoroff Apr 30 '16 at 18:26
  • Users don't paste from the clipboard, not directly. The application requests the contents of the clipboard, normally in response to some action of the user. It's often in response to something like pressing CTRL-V or selecting Paste from a menu, but it's up to the application. To intercept and modify all application clipboard accesses is going to require low level OS-specific hacking. It would be easier to monitor new data added to the clipboard and then replace it with a modified version, but this doesn't seem to do what you want. – Ross Ridge Apr 30 '16 at 18:46
  • @Ross Ridge Hmmm, so my trigger in this case should be ctrl+v probably. So I'd be better off just monitoring keystrokes in the background, in your opinion? – Futuza Apr 30 '16 at 18:57
  • If you only want to intercept applications that only use CTRL-V for pasting, that don't support pasting any other way and don't use CTRL-V for anything else, then something along those lines might work. It's still low level OS-specific hacking. It's more than monitoring keystrokes. You need to intercept them, stopping the application from receiving and processing the CTRL-V keystroke before your program can change the the contents of clipboard. Only after that can you let the application see the CTRL-V. – Ross Ridge Apr 30 '16 at 19:53
  • @DarthFutuza You mean you copied a text and it will be saved in text file? –  Jun 29 '17 at 10:21
  • @mini No, just as a string, no need to involve an actual file. – Futuza Jul 05 '17 at 23:13

0 Answers0