0

My clipboard controller could have several items copied to the clipboard when using a hotkey (CTRL + SHIFT + Q), instead of only one item, and pastes all at once (CTRL + SHIFT + W), or paste any of the first 10 items directly (CTRL + SHIFT + 19). Another option is to clear the clipboard (CTRL + SHIFT + -).

It works just for several copy and pastes, but then trying to make a copy operation nothing is added to the buffer. I couldn't find a reason for this.

Code (problem should be in the addToClipboard() or getAll()) :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <array.au3>

Global $clipBoard[50]=[""]
Global $counter = 0

HotKeySet("^+q","addToClipboard")
HotKeySet("^+-","emptyAll")
HotKeySet("^+w","getAll")
HotKeySet("^+1","get1")
HotKeySet("^+2","get2")
HotKeySet("^+3","get3")
HotKeySet("^+4","get4")
HotKeySet("^+5","get5")
HotKeySet("^+6","get6")
HotKeySet("^+7","get7")
HotKeySet("^+8","get8")
HotKeySet("^+9","get9")

$hGUI = GuiCreate("Clipboard Controller", 100, 100,Default,Default,$WS_SIZEBOX)
GUISetState()

Func addToClipboard()
    Send ("^c")
    $copied = ClipGet()
    $clipBoard[Mod($counter,50)] = $copied
    $counter +=1
EndFunc

Func getByIndex($i)
    $statement = $clipBoard[$i]
    ClipPut($statement)
    Send("^v")
EndFunc

Func getAll()
    $statement =""
    For $i In $clipBoard
        If $i <> "" Then
            $statement &= $i & @CRLF
        EndIf
    Next
    ClipPut($statement)
    Send("^v")
EndFunc

Func emptyAll()
    For $i=0 To 49
        $clipBoard[$i]=""
    Next
    ClipPut("")
EndFunc

Func get1()
    getByIndex(0)
EndFunc

Func get2()
    getByIndex(1)
EndFunc

Func get3()
    getByIndex(2)
EndFunc

Func get4()
    getByIndex(3)
EndFunc

Func get5()
    getByIndex(4)
EndFunc

Func get6()
    getByIndex(5)
EndFunc

Func get7()
    getByIndex(6)
EndFunc

Func get8()
    getByIndex(7)
EndFunc

Func get9()
    getByIndex(8)
EndFunc

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
user4157124
  • 2,809
  • 13
  • 27
  • 42
a.u.r
  • 1,253
  • 2
  • 21
  • 32

3 Answers3

2

Problem is an old trap...

It takes a small amount of time to copy to the clip board especially large items..try a sleep after the Send

Func addToClipboard()
Send ("^c")
sleep(1000) ; try different values
    $copied = ClipGet()
$clipBoard[Mod($counter,50)] = $copied
$counter +=1
EndFunc

anyway like your script..idea

ozmike
  • 2,738
  • 1
  • 33
  • 40
1

The problem is the code for addToClipboard is running while the user still has the keys pressed down. As a result, the Send command designed to send just Ctrl+C is in effect sending Ctrl+Shift+C, so the text is never copied.

The solution is to wait for the user to raise those keys, using the _IsPressed function, then once all keys are released, execute the code. It may also be wise to disable the hotkey when you enter the function (and re-enable when you leave) so that holding the hotkey down for a long time doesn't keep triggering the function.

An alternative would be to send the WM_COPY message directly to the control with focus. This is not guaranteed to work for every control (though I'd be very surprised if it didn't). This would be a far more reliable method.

Matt
  • 7,100
  • 3
  • 28
  • 58
  • having understood your reply , and guided by [this-post](http://stackoverflow.com/questions/7936831/convert-clipboard-to-keystroke) I wrote the following [code](http://codepad.org/mqtT58Rn) but didn't work either.. – a.u.r May 08 '12 at 21:56
  • @a.u.r, it is possible to completely unset a hotkey by passing "" instead of a function, rather than using a dummy. I will have to look a bit more to see what exactly you are doing wrong there. – Matt May 09 '12 at 06:44
  • Great idea with the `_IsPressed` is awesome, it totally helped a lot. I use `_IsPress` as a condition of the while loop to wait until the user releases the key. Thank you! – Lê Quang Duy Feb 17 '19 at 16:43
1

hope this is the end of the problem , I found another way to set/get data from clipboard , functions : _ClipBoard_SetData () & _ClipBoard_GetData() from library <Clipboard.au3> , after trying them it worked well , after all it seems like the problem was in setting and getting data from the clipboard.. will come later isA to assure whether its finally correct or not

a.u.r
  • 1,253
  • 2
  • 21
  • 32