JatSing
JatSing

Reputation: 4977

convert clipboard to keystroke

My code :

HotKeySet("^v","ClipboardToKeystroke")

While 1
WEnd

Func ClipboardToKeystroke()
    Send(ClipGet(),1)
EndFunc

Unfortunately it doesn't behave like what I expect. For a single line, it works well but for multiple lines it send duplicate of enter. Eg :

Original text :

This is the 1st line
This is the 2nd line

After auto keystroke :

This is the 1st line

This is the 2nd line

And one more thing, there is also a problem with the Ctrl key after send the keystroke, it seems the Ctrl key is hold and I have to press the Ctrl key again to release it.

So is there a workaround ?

Upvotes: 1

Views: 2351

Answers (1)

Jos van Egmond
Jos van Egmond

Reputation: 2360

I kept busy on this until I got it working like you would expect. This is the final product:

There are explanations as to how and why things are happening in the code. I had to use a lot of little "tricks" I picked up over the years.

#include <Misc.au3>

HotKeySet("^v","ClipboardToKeystroke")

While 1
    Sleep(50) ; If you forget this, your program takes up max CPU
WEnd

Func ClipboardToKeystroke()
    HotKeySet("^v", "Dummy") ; This unregisters the key from this function, and sets it on a dummy function
    While _IsPressed("11") Or _IsPressed("56") ; Wait until both the ctrl and the v key are unpressed
        Sleep(50)
    WEnd
    $clipboard = ClipGet()
    $clipboard = StringStripCR($clipboard) ; A newline consists of 2 characters in Windows: CR and LF.
                                           ;If you type a CR, Windows understands it as an attempt to type CRLF.
                                           ; If you type LF, same thing. So if you type CR and then LF, it is interpreter as CRLFCRLF. Thus two newlines.
    Send($clipboard, 1)
    HotKeySet("^v", "ClipboardToKeystroke")
EndFunc

Func Dummy()
    ; Do nothing, this prevents the hotkey to calling the ClipboardToKeystroke function a lot when you hold the key down too long
EndFunc

Upvotes: 4

Related Questions