1

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 ?

JatSing
  • 4,857
  • 16
  • 55
  • 65

1 Answers1

4

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
Jos van Egmond
  • 2,370
  • 15
  • 19
  • The Dummy function is not needed though, for HotkeySet: Not specifying the second parameter will unset a previous hotkey. – Matt Nov 03 '11 at 08:03
  • 2
    I'm not trying to unset the previous hotkey. I'm assigning it to a dummy function so that when you press the key, it doesn't let the actual ctrl+v through. This is necessary when you hold the ctrl+v down longer so that it repeats itself. – Jos van Egmond Nov 04 '11 at 19:40