Autoit hotkeyset not working

≯℡__Kan透↙ 提交于 2019-12-25 05:09:56

问题


I'm trying to make some kind of simple clipboard controller , where the user could have several items copied to the clipboard when using a hotkey (Ctrl+Shift+Q) , instead of only one item when using Ctrl+C , and would be able to paste them all at once (Ctrl+Shift+W) , or paste any of the first 10 items directly (Ctrl+Shift+1...9), another option is to clear the clipboard (Ctrl+Shift+'-').

The problem is that it works fine just for several copy and pastes , but then trying to make a copy operation nothing is added to the buffer.. I'm trying to figure it out , but couldn't find a reason for this..

here is the code :

Note : the problem should be in the addToClipboard() or getAll() I believe

#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

回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/10505664/autoit-hotkeyset-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!