convert clipboard to keystroke

こ雲淡風輕ζ 提交于 2019-12-11 07:01:35

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/7936831/convert-clipboard-to-keystroke

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