Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Robust copy and paste routine (function)


  • Please log in to reply
10 replies to this topic
ObiWanKenobi
  • Members
  • 83 posts
  • Last active: Oct 17 2015 05:37 PM
  • Joined: 21 Aug 2012

I always wanted a copy and paste routine that should work very robust in any program.

Now i figured it out, here is my code:

ClipCopy(piMode := 0)
{
    clpBackup := ClipboardAll

    Clipboard=

    if (piMode == 1)
        sCopyKey := "vk58sc02D" ; Cut
    else
        sCopyKey := "vk43sc02E" ; Copy

    SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%sCopyKey% Down}
    ClipWait, 0.25
    SendInput, {%sCopyKey% Up}{Ctrl Up}

    sRet := Clipboard

    Clipboard := clpBackup

    return sRet
}

ClipPaste(ByRef psText)
{
    if (psText != "")
    {
        clpBackup := ClipboardAll

        sPasteKey := "vk56sc02F" ; Paste

        Clipboard := psText

        SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%sPasteKey% Down}

        ; wait for clipboard is ready
        iStartTime := A_TickCount
        Sleep, % 100
        while (DllCall("GetOpenClipboardWindow") && (A_TickCount-iStartTime<1400)) ; timeout = 1400ms
            Sleep, % 100
        
        SendInput, {%sPasteKey% Up}{Ctrl Up}

        Clipboard := clpBackup
    }
}

Usage:

 

sMyVar := ClipCopy() ; will copy selected text via control + c

sMyVar := ClipCopy(1) ; will cut selected text via control + x

ClipPaste(sMyVeryLongTextVar) ; will paste a text immediately with control + v

 

Copy & Paste without headaches ... even if modifiers shift or / and control are pressed during copy and paste.

 

Have fun!



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
Well done.

Can you explain why this is needed? What makes this more 'robust'? How/Why is this needed? What programs would a normal Send, ^c or ^v fail on? etc

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

Is it only for the modifiers?



ObiWanKenobi
  • Members
  • 83 posts
  • Last active: Oct 17 2015 05:37 PM
  • Joined: 21 Aug 2012

Hello guest, i have many hotkeys with control+alt or with shift+control and sometimes this pressed keys interact with copy routine (ctr+c) and i had strange behavior.

_waitClipReady() is needed because sometimes Clipboard is not filled for ctr+v action.

This solution works much better now.



beeski
  • Members
  • 158 posts
  • Last active: Mar 06 2016 08:21 PM
  • Joined: 22 Sep 2011

the real issue i've found is when a program actually stores the "clipboard" contents elsewhere in memory.

 

i have a program in which i thought i could use ahk to store clipboards for later retrieval. it indeed does store some information in the clipboard, but apparently it stores crucial info elsewhere. merely storing the clipboard info retrieved the standard way, and then moving it back to the clipboard at a later time and sending [ctrl+v], does not work.

 

i wonder if anyone ever tried some memory hacking to get around this issue.

 

i suppose most software does not behave this way so it may not have become an issue to many users...



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

indeed i have had issues with ^c and couldn't figure it out! this seems to address this problem nicely. i am testing it now but it looks like a winner. will report back for final results after more testing... ;)



ObiWanKenobi
  • Members
  • 83 posts
  • Last active: Oct 17 2015 05:37 PM
  • Joined: 21 Aug 2012

Hello, found this today ....

http://www.autohotke.../BlockInput.htm

... Disables or enables the user's ability to interact with the computer via keyboard and mouse.

BlockInput On
; ... send keystrokes and mouse clicks ...
BlockInput Off

*Updated my code in first post*



ObiWanKenobi
  • Members
  • 83 posts
  • Last active: Oct 17 2015 05:37 PM
  • Joined: 21 Aug 2012

Strange or no behavior, removed it again; first post updated.



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

What is your test suite / protocol ? Exactly what are you doing to detect "strange or no behavior" ?



ObiWanKenobi
  • Members
  • 83 posts
  • Last active: Oct 17 2015 05:37 PM
  • Joined: 21 Aug 2012

Hello guest,

i tried:

BlockInput On
; ... sending my copy key combination
SendInput, ^c
Sleep 5000 ;for test
BlockInput Off

And sometimes I could still type characters in this Sleep 5000 loop and in some applications I could not type at all after execution of this routine ... (i had to exit script)

Unconsinstent (strange) behaivior, perhaps dependend on windows (Win 7, WIn 8, WIn XP) version.



ObiWanKenobi
  • Members
  • 83 posts
  • Last active: Oct 17 2015 05:37 PM
  • Joined: 21 Aug 2012

Initial post updated!

(after much testing)