Jump to content

_ControlPaste() UDF


dbzfanatic
 Share

Recommended Posts

Well I've seen one too many "how do I avoid character by character sending to a control?" questions. Now people can simply point the new people to this topic.

Func _ControlPaste($sCTitle,$sCText,$sCID,$sText)
ClipPut($sText)
ControlFocus($sCTitle,$sCText,$sCID)
Send("^{V}")
EndFunc

Was that really so hard people? I mean seriously,come on...

Link to comment
Share on other sites

Well I've seen one too many "how do I avoid character by character sending to a control?" questions. Now people can simply point the new people to this topic.

Func _ControlPaste($sCTitle,$sCText,$sCID,$sText)
ClipPut($sText)
ControlFocus($sCTitle,$sCText,$sCID)
Send("^{V}")
EndFunc

Was that really so hard people? I mean seriously,come on...

Time for a rewrite: you didn't provide for the case where there is something already on the clipboard that needs preserving :mellow: Edited by ResNullius
Link to comment
Share on other sites

Fine how's this? :mellow:

Func _ControlPaste($sCTitle,$sCText,$sCID,$sText,$iPreserve)
If $iPreserve = 1 Then
$sCBText = _ClipBoard_GetData()
Endif
ClipPut($sText)
ControlFocus($sCTitle,$sCText,$sCID)
Send("^{V}")
If $sCBText <> "" Then
ClipPut($sCBText)
Endif
EndFunc

Edit: Used _ClipBoard_GetData() to allow non-text clipboard preservation.

Edited by dbzfanatic
Link to comment
Share on other sites

Fine how's this? :mellow:

Func _ControlPaste($sCTitle,$sCText,$sCID,$sText,$iPreserve)
If $iPreserve = 1 Then
$sCBText = _ClipBoard_GetData()
Endif
ClipPut($sText)
ControlFocus($sCTitle,$sCText,$sCID)
Send("^{V}")
If $sCBText <> "" Then
ClipPut($sCBText)
Endif
EndFunc

Edit: Used _ClipBoard_GetData() to allow non-text clipboard preservation.

Better, but if you're making it a UDF, then for the nubes you need

#include <Clipboard.au3>

And I think I'd make $iPreserve default to 1

#include-once
#include <Clipboard.au3>

Func _ControlPaste($sCTitle, $sCText, $sCID, $sText, $iPreserve = 1)
    Local $sOldCBData = ""
    If $iPreserve = 1 Then
        $sOldCBData = _ClipBoard_GetData()
    EndIf
    ClipPut($sText)
    ControlFocus($sCTitle, $sCText, $sCID)
    ControlSend($sCTitle, $sCText, $sCID, "+{INSERT}")
    If $sOldCBData <> "" Then
        ClipPut($sOldCBData)
    EndIf
EndFunc

Also I changed to ControlSend from plain Send just for a added measure of reliability, and I send the Shift+Insert keycombo because sometimes sending Ctrl+V can be unpredicatable. :(

Edited by ResNullius
Link to comment
Share on other sites

Yeah sure just take over my project lol. Thanks for the help/suggestions though. My brain's been a bit fried lately (my girlfriend's parents went ballistic on us) so I appreciate the help, honestly.

Edit: Did some testing (my head cleared a bit :mellow: plus I'm bored) and I noticed that it does not preserve the data on the clipboard. I think I may revert it to saving only text instead of using _ClipBoard_GetData(), which I now see returns only handles and not the data itself.

Edit 2: Here is the modified function that preserves clipboard text.

#include-once
#include <Clipboard.au3>

Func _ControlPaste($sCTitle, $sCText, $sCID, $sText, $iPreserve = 1)
    If $iPreserve = 1 Then
        $sCBText = ClipGet()
    EndIf
    ClipPut($sText)
    ControlFocus($sCTitle, $sCText, $sCID)
    ControlSend($sCTitle, $sCText, $sCID, "+{INSERT}")
    If $sCBText <> "" Then
        ClipPut($sCBText)
    EndIf
EndFunc

Here is an example of the usage.

_ControlPaste("Untitled - ", "", "Edit1","Some text for Notepad.")

Edit 3: Did a bit more reading and it seems ClipPut() clears the clipboard and then places the specified data onto it. I tried using _ClipBoard_SetData() but that requires a handle to the data,not the data itself. Retrieving the data with _ClipBoard_GetData() would return a handle but as soon as ClipPut() is called the handle is useless. I will work on this a bit and see if I can create a structure for the handle to the actual text, that way the handle to previous clipboard data remains.

Edited by dbzfanatic
Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...