Sending Strings Queue to Clipboard in python
Question: I am writing a program that runs in background and check's for file changes in
a folder if any new Image file arrives into that folder it will read text from
that Image with the help of tesseract OCR Engine.Images contains Adresses of
Employees.python program splits that Adress into individual list.

I want to put each address section into clipboard one after other.So If I
press Ctrl+V First Section will pasted.Next time if i press Ctrl+v Next
section will pasted like wise.

Here is the Code.

    
    
    #!/usr/bin/python
    import commands,os
    global vdir,outfile
    global prev
    vdir="Vilvin"
    out="Output"
    a=os.listdir(vdir)
    prev=len(a)
    whcount=0
    stat_dict={'NEW HRMPSHIRE': 'NEW HAMPSHIRE', 'VERMONT': 'VERMONT', 'LOUISIRNR': 'LOUISIANA', 'CRLIFORNIR': 'CALIFORNIA', 'MISSISSIPPI': 'MISSISSIPPI', 'PENNSYLVRNIR': 'PENNSYLVANIA', 'MONTRNR': 'MONTANA', 'GEORGIR': 'GEORGIA', 'WRSHINGTON': 'WASHINGTON', 'NEW YORK': 'NEW YORK', 'MRRYLRND': 'MARYLAND', 'IOWR': 'IOWA', 'SOUTH DRKOTR': 'SOUTH DAKOTA', 'VIRGINIR': 'VIRGINIA', 'FLORIDR': 'FLORIDA', 'MRINE': 'MAINE', 'NEBRRSKR': 'NEBRASKA', 'RLRSKR': 'ALASKA', 'ILLINOIS': 'ILLINOIS', 'CONNECTICUT': 'CONNECTICUT', 'TENNESSEE': 'TENNESSEE', 'NEW MEXICO': 'NEW MEXICO', 'COLORRDO': 'COLORADO', 'DELRWRRE': 'DELAWARE', 'HRWRII': 'HAWAII', 'NORTH CRROLINR': 'NORTH CAROLINA', 'UTRH': 'UTAH', 'RLRBRMR': 'ALABAMA', 'MICHIGRN': 'MICHIGAN', 'RRKRNSRS': 'ARKANSAS', 'NEW JERSEY': 'NEW JERSEY', 'MISSOURI': 'MISSOURI', 'OREGON': 'OREGON', 'WYOMING': 'WYOMING', 'OHIO': 'OHIO', 'WISCONSIN': 'WISCONSIN', 'MINNESOTR': 'MINNESOTA', 'KRNSRS': 'KANSAS', 'RHODE ISLRND': 'RHODE ISLAND', 'WEST VIRGINIR': 'WEST VIRGINIA', 'IDRHO': 'IDAHO', 'OKLRHOMR': 'OKLAHOMA', 'KENTUCKY': 'KENTUCKY', 'RRIZONR': 'ARIZONA', 'NEVRDR': 'NEVADA', 'INDIRNR': 'INDIANA', 'MRSSRCHUSETTS': 'MASSACHUSETTS', 'SOUTH CRROLINR': 'SOUTH CAROLINA', 'NORTH DRKOTR': 'NORTH DAKOTA', 'TEXRS': 'TEXAS'}
    while True:
        instant=os.listdir(vdir)
        if(len(instant)>prev):
            print "File Change Detected...."
            r=commands.getoutput('ls -ct1 '+vdir+' | head -1')
            print "Most recent file = %s " %(r)
            r=r.replace("(","\(")
            r=r.replace(")","\)")
            r=r.replace(" ","\ ")
            os.system("tesseract "+vdir+"/"+r+" "+out+"/"+"Output")
            result=commands.getoutput("awk -F: '{ print $2 $3 }' "+out+"/"+"Output.txt")
            res=result.split("\n")
            state=res[0].split("State")
            profile=res[1].split("Pro?ile")
            applicant=state[0].strip().replace("R","A")
            state=state[1].strip()
            state=stat_dict[state]
            sid=profile[0].strip()
            profile=profile[1].strip().replace("R","A")
            sec=res[3].strip().replace("R","A")
            a=commands.getoutput("echo \""+applicant+"\" | xclip -verbose -selection clipboard")
            b=commands.getoutput("echo \""+state+"\" | xclip -verbose -selection clipboard")
            c=commands.getoutput("echo \""+sid+"\" | xclip -verbose -selection clipboard")
            d=commands.getoutput("echo \""+profile+"\" | xclip -verbose -selection clipboard")
            e=commands.getoutput("echo \""+sec+"\" | xclip -verbose -selection clipboard")
            print "Applicant : "+applicant+"\nState : "+state+"\nStaff ID : "+sid+"\nProfile : "+profile+"\nSEC : "+sec+"\n"
            prev=len(instant)
        else:
            whcount=whcount+1
            print "While Loop Count : "+str(whcount)+"\n"
            os.system("sleep 2")
    

One thing I forgot is this program always runs in background & the terminal
windows is minimised so we have to get Key presses on whole Xsession & GUI
Apps..whenever Ctrl+V triggered in any application we should detect that
...Thanks in Advance


Answer: Ok, so, here is how this goes:

    
    
    import time,os,win32api
    from msvcrt import getch
    
    def addToClipBoard(text):
        command = 'echo ' + text.strip() + '| clip'
        os.system(command)
    
    
    def testpress(key):
        return (win32api.GetKeyState(key) & (1 << 7)) != 0
    
    key = 17 #ctrl key
    key2= ord('V')
    copy=1
    
    while True:
        keydown = testpress(key)
        key2down = testpress(key2)
        if keydown and key2down:
            print 'CtrlV pressed!'
            if copy==1:
              addToClipBoard('Foo')
            elif copy==2:
                addToClipBoard('Shoo')
            elif copy==3:
                addToClipBoard('THA END')
            if copy>3:
                exit(1)
            copy+=1
        time.sleep(0.10)
    

I got the code for testing the keypress using win32api from another answer,
then put it all together to do what you wanted it to :)

